一、使用系统定义的Notification
以下是使用示例代码:

  1. import android.app.Notification;
  2. import android.app.NotificationManager;
  3. import android.app.PendingIntent;
  4. import android.content.Context;
  5.  
  6. public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
  7. private NotificationManager manager;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_mian_water);
  13.  
  14.   //获取到通知管理器
  15.   manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  16. }
  17.  
  18. @Override
  19. public void onClick(View v) {
  20. switch (v.getId()) {
  21. case R.id.btn_wf_back:
  22.       // 定义Notification的各种属性
  23.       int icon = R.drawable.button_login; //通知图标
  24.       CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示
  25.       long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
  26.       Notification myNotify = new Notification(icon,tickerText,when);
  27.  
  28.       Context context = getApplicationContext(); //上下文
  29.       CharSequence contentTitle = "My Notification"; //通知栏标题
  30.       CharSequence contentText = "Hello World!"; //通知栏内容
  31.       Intent notificationIntent = new Intent(this,WaterActivity.class); //点击该通知后要跳转的Activity
  32.       PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
  33.       myNotify.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  34.       manager.notify(0x00000008, myNotify);
  35.       //如果想要更新一个通知,只需要在设置好notification之后,再次调用 setLatestEventInfo(),然后重新发送一次通知即可,即再次调用notify()。
  36.       break;  
  37.      }
  38.   }
  39. }
二、使用自定义的 Notification
要创建一个自定义的Notification,可以使用RemoteViews。
要定义自己的扩展消息,首先 要初始化一个RemoteViews对象,然后将它传递给Notification的contentView字段,再把PendingIntent传递给 contentIntent字段。
以下示例代码是完整步骤:
1、创建一个自 定义的消息布局 my_notification.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:background="#ffffff"
  6. android:orientation="vertical" >
  7.  
  8. <TextView
  9. android:id="@+id/text_content"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:textSize="20sp" />
  13.  
  14. </LinearLayout>
2、 在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
  1. RemoteViews rv = new RemoteViews(getPackageName(), R.layout.my_notification);
  2. rv.setTextViewText(R.id.text_content, "hello wrold!");
  3. myNotify.contentView = rv;
3、 为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要 setLatestEventInfo()方法)
  1. Intent intent = new Intent(Intent.ACTION_MAIN);
  2. PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);
  3. myNotify.contentIntent = contentIntent;
4、发送通知
  1. manager.notify(0x00000008, myNotify);
5.完整代码
  1. import android.app.Notification;
  2. import android.app.NotificationManager;
  3. import android.app.PendingIntent;
  4. import android.content.Context;
  5. import android.widget.RemoteViews;
  6.  
  7. public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
  8. private NotificationManager manager;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_mian_water);
  14.  
  15.   //获取到通知管理器
  16.   manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  17. }
  18.  
  19. @Override
  20. public void onClick(View v) {
  21. switch (v.getId()) {
  22. case R.id.btn_wf_back:
  23. Notification myNotify = new Notification();
  24.        myNotify.icon = R.drawable.button_login;
  25. myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";
  26. myNotify.when = System.currentTimeMillis();
  27. //myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
  28. RemoteViews rv = new RemoteViews(getPackageName(), R.layout.my_notification);
  29. rv.setTextViewText(R.id.text_content, "hello wrold!");
  30. myNotify.contentView = rv;
  31. Intent intent = new Intent(Intent.ACTION_MAIN);
  32. PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);
  33. myNotify.contentIntent = contentIntent;
  34.        manager.notify(0x00000008, myNotify);
  35.        //如果想要更新一个通知,只需要在设置好notification之后,再次调用 setLatestEventInfo(),然后重新发送一次通知即可,即再次调用notify()。
  36.        break;  
  37.      }
  38.   }
  39. }

6.清除

  1. manager.cancel(2);

参数属性:

  1. // 定义Notification的各种属性
  2. Notification notification =new Notification(R.drawable.icon,
  3. "测试", System.currentTimeMillis());
  4. //FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
  5. //FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
  6. //FLAG_ONGOING_EVENT 通知放置在正在运行
  7. //FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
  8. notification.flags |= Notification.FLAG_ONGOING_EVENT;
  9. // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
  10. notification.flags |= Notification.FLAG_NO_CLEAR;
  11. // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
  12. notification.flags |= Notification.FLAG_SHOW_LIGHTS;
  13. //DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
  14. //DEFAULT_LIGHTS 使用默认闪光提示
  15. //DEFAULT_SOUNDS 使用默认提示声音
  16. //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限
  17. notification.defaults = Notification.DEFAULT_LIGHTS;
  18. //叠加效果常量
  19. //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
  20. notification.ledARGB = Color.BLUE;
  21. notification.ledOnMS =5000; //闪光时间,毫秒
  22.  
  23. // 设置通知的事件消息
  24. CharSequence contentTitle ="标题"; // 通知栏标题
  25. CharSequence contentText ="内容"; // 通知栏内容
  26.  
  27. //如果需要跳转到指定的Activity,则需要设置PendingIntent
  28.  
  29. Intent notificationIntent =new Intent(A.this, B.class);
  30. // 点击该通知后要跳转的Activity
  31.  
  32. notificationIntent.putExtra("date","需要传递的参数");
  33.  
  34. // FLAG_UPDATE_CURRENT 更新数据,如果有多个PendingIntent,且requestCode相同,则会替换为最新extra数据
  35. //如果需要通过不同的extra数据,进行处理,就需要requestCode不相同
  36. int requestCode = new Random().nextInt();
  37. PendingIntent contentItent = PendingIntent.getActivity(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  38.  
  39. notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);
  40.  
  41. // 把Notification传递给NotificationManager
  42. notificationManager.notify(0, notification);

注意:

new Intent(this,this.getClass())保证了点击通知栏里的通知可以回到该Activity

但是,假如该Activity还在后台运行,并没有运行,通知事件响应后,系统会自动结束该Activity,然后再重新启动Activity,这不是我们要的。

解决方法为:在manifest.xml文件中找到该Activity,添加属性android:launchMode="singleTask“。这个属性很明显,就是只允许有一个该Activity运行,如果正在运行,则只能切换到当前运行的Activity,而不能重新启动Activity。

三、创建定时器
源代码如下:
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3.  
  4. public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
  5. private Timer mTimer = null;
  6. private TimerTask mTimerTask = null;
  7. private int isPause = 0;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_mian_water);
  13. }
  14.  
  15. private void startMyTimer(){
  16. if (mTimer == null) {
  17. mTimer = new Timer();
  18. }
  19.  
  20. if (mTimerTask == null) {
  21. mTimerTask = new TimerTask() {
  22. @Override
  23. public void run() {
  24. do {
  25. try {
  26. if(isPause == 0) isPause = 1;
  27. else isPause = 0;
  28.  
  29. Message message = new Message();
  30. message.what = isPause;
  31. handler.sendMessage(message);
  32.  
  33. } catch (IllegalStateException e) {
  34. }
  35. } while (false);
  36. }
  37. };
  38. }
  39.  
  40. if(mTimer != null && mTimerTask != null )
  41. mTimer.schedule(mTimerTask, 0, 500);
  42.  
  43. }
  44.  
  45. private void stopMyTimer(){
  46.  
  47. if (mTimer != null) {
  48. mTimer.cancel();
  49. mTimer = null;
  50. }
  51.  
  52. if (mTimerTask != null) {
  53. mTimerTask.cancel();
  54. mTimerTask = null;
  55. }
  56. }
  57. }

android中的本地定时推送到通知栏的更多相关文章

  1. 在Unity3D中实现安卓平台的本地通知推送

    [前言] 对于手游来说,什么时候需要推送呢?玩过一些带体力限制的游戏就会发现,我的体力在恢复满后,手机会收到一个通知告诉我体力已完全恢复了.这类通知通常是由本地的客户端发起的,没有经过服务端. 在安卓 ...

  2. Android本地消息推送

    项目介绍:cocos2dx跨平台游戏 项目需求:实现本地消息推送,需求①:定点推送:需求②:根据游戏内逻辑实现推送(比如玩家体力满时,需要计算后到点推送):需求③:清理后台程序或重启后依然能够实现本地 ...

  3. Android 基于Netty的消息推送方案之Hello World(一)

    消息推送方案(轮询.长连接) 轮询 轮询:比较简单的,最容易理解和实现的就是客户端去服务器上拉信息,信息的及时性要求越高则拉信息的频率越高.客户端拉信息的触发可以是一些事件,也可以是一个定时器,不断地 ...

  4. IOS 本地通知推送消息

    在现在的移动设备中,好多应用性的APP都用到了推送服务,但是有好多推送的内容,比如有的只是单纯的进行推送一个闹钟类型的,起了提醒作 用,有的则是推送的实质性的内容,这就分为推送的内容来区别用什么推送, ...

  5. Android 基于Netty的消息推送方案之对象的传递(四)

    在上一篇文章中<Android 基于Netty的消息推送方案之字符串的接收和发送(三)>我们介绍了Netty的字符串传递,我们知道了Netty的消息传递都是基于流,通过ChannelBuf ...

  6. Android 基于Netty的消息推送方案之字符串的接收和发送(三)

    在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...

  7. Android 基于Netty的消息推送方案之概念和工作原理(二)

    上一篇文章中我讲述了关于消息推送的方案以及一个基于Netty实现的一个简单的Hello World,为了更好的理解Hello World中的代码,今天我来讲解一下关于Netty中一些概念和工作原理的内 ...

  8. Git总结笔记3-把本地仓库推送到github

    说明:此笔记在centos 7 上完成 1.配置公钥 [root@kangvcar ~]# ssh-keygen -t rsa -C "kangvcar@126.com" [roo ...

  9. WebSocket(4)---实现定时推送比特币交易信息

    实现定时推送比特币交易信息 实现功能:跟虚拟币交易所一样,时时更新当前比特币的价格,最高价,最低价,买一价等等...... 提示:(1)本篇博客是在上一遍基础上搭建,上一篇博客地址:[WebSocke ...

随机推荐

  1. 第36讲 activityForResult

    第36讲 activityForResult activityForResult的作用是利用下一个activity给当前的activity传值(前一讲是利用当前activity给下一个activity ...

  2. maven编写主代码与测试代码

    3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目主代码位于src/main/ja ...

  3. swift 自定义导航栏颜色

    func setNavigationApperance(){ //自定义导航栏颜色 [self.navigationController?.navigationBar.barTintColor = U ...

  4. python中os模块常用方法

    #!/usr/bin/python## os module test import os print 'os.name: ', os.nameprint 'os.getcwd(): ', os.get ...

  5. (转载)iOS Framework: Introducing MKNetworkKit

    This article is available in Serbo-Croatian,  Japanese and German. (Translations in Serbo-Croatian b ...

  6. Laravel-表单篇-零散信息

    1.asset('path'):用于引入静态文件,包括css.js.img 2.分页,调用模型的paginate(每页显示的行数)方法, 如$student = Student::paginate(2 ...

  7. 奔五的人学IOS:swift练手与csdn,最近学习总结

    早在五月份就准备開始学习ios开发,当时还是oc,学习了几天,最终不得其法.到了ios8开放,再加swift的出现.从10月份開始.最终找到了一些技巧,学习起来还算略有心得. 今天把我在学习swift ...

  8. CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27

    CentOS 6.8编译安装httpd2.2.31+MySQL5.6.31+PHP5.3.27   说明:   操作系统:CentOS 6.8 32位 准备篇: 一.系统约定    软件源代码包存放位 ...

  9. Web Api Session开启会话支持

        1.WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型           //代码如下 public override voi ...

  10. SQL Server -ISNULL()函数

    SQL中有多种多样的函数,下面将为您介绍SQL中的ISNULL函数,包括其语法.注释.返回类型等,供您参考,希望对您学习SQL能够有所帮助. ISNULL 使用指定的替换值替换 NULL. 语法ISN ...