android中的本地定时推送到通知栏
一、使用系统定义的Notification
以下是使用示例代码:
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
- private NotificationManager manager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_mian_water);
- //获取到通知管理器
- manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_wf_back:
- // 定义Notification的各种属性
- int icon = R.drawable.button_login; //通知图标
- CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示
- long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
- Notification myNotify = new Notification(icon,tickerText,when);
- Context context = getApplicationContext(); //上下文
- CharSequence contentTitle = "My Notification"; //通知栏标题
- CharSequence contentText = "Hello World!"; //通知栏内容
- Intent notificationIntent = new Intent(this,WaterActivity.class); //点击该通知后要跳转的Activity
- PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
- myNotify.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
- manager.notify(0x00000008, myNotify);
- //如果想要更新一个通知,只需要在设置好notification之后,再次调用 setLatestEventInfo(),然后重新发送一次通知即可,即再次调用notify()。
- break;
- }
- }
- }
二、使用自定义的 Notification
要创建一个自定义的Notification,可以使用RemoteViews。
要定义自己的扩展消息,首先 要初始化一个RemoteViews对象,然后将它传递给Notification的contentView字段,再把PendingIntent传递给 contentIntent字段。
以下示例代码是完整步骤:
1、创建一个自 定义的消息布局 my_notification.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="#ffffff"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/text_content"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20sp" />
- </LinearLayout>
2、 在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
- RemoteViews rv = new RemoteViews(getPackageName(), R.layout.my_notification);
- rv.setTextViewText(R.id.text_content, "hello wrold!");
- myNotify.contentView = rv;
3、 为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要 setLatestEventInfo()方法)
- Intent intent = new Intent(Intent.ACTION_MAIN);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);
- myNotify.contentIntent = contentIntent;
4、发送通知
- manager.notify(0x00000008, myNotify);
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.widget.RemoteViews;
- public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
- private NotificationManager manager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_mian_water);
- //获取到通知管理器
- manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_wf_back:
- Notification myNotify = new Notification();
- myNotify.icon = R.drawable.button_login;
- myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";
- myNotify.when = System.currentTimeMillis();
- //myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
- RemoteViews rv = new RemoteViews(getPackageName(), R.layout.my_notification);
- rv.setTextViewText(R.id.text_content, "hello wrold!");
- myNotify.contentView = rv;
- Intent intent = new Intent(Intent.ACTION_MAIN);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);
- myNotify.contentIntent = contentIntent;
- manager.notify(0x00000008, myNotify);
- //如果想要更新一个通知,只需要在设置好notification之后,再次调用 setLatestEventInfo(),然后重新发送一次通知即可,即再次调用notify()。
- break;
- }
- }
- }
6.清除
- manager.cancel(2);
参数属性:
- // 定义Notification的各种属性
- Notification notification =new Notification(R.drawable.icon,
- "测试", System.currentTimeMillis());
- //FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
- //FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
- //FLAG_ONGOING_EVENT 通知放置在正在运行
- //FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
- notification.flags |= Notification.FLAG_ONGOING_EVENT;
- // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
- notification.flags |= Notification.FLAG_NO_CLEAR;
- // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
- notification.flags |= Notification.FLAG_SHOW_LIGHTS;
- //DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
- //DEFAULT_LIGHTS 使用默认闪光提示
- //DEFAULT_SOUNDS 使用默认提示声音
- //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限
- notification.defaults = Notification.DEFAULT_LIGHTS;
- //叠加效果常量
- //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
- notification.ledARGB = Color.BLUE;
- notification.ledOnMS =5000; //闪光时间,毫秒
- // 设置通知的事件消息
- CharSequence contentTitle ="标题"; // 通知栏标题
- CharSequence contentText ="内容"; // 通知栏内容
- //如果需要跳转到指定的Activity,则需要设置PendingIntent
- Intent notificationIntent =new Intent(A.this, B.class);
- // 点击该通知后要跳转的Activity
- notificationIntent.putExtra("date","需要传递的参数");
- // FLAG_UPDATE_CURRENT 更新数据,如果有多个PendingIntent,且requestCode相同,则会替换为最新extra数据
- //如果需要通过不同的extra数据,进行处理,就需要requestCode不相同
- int requestCode = new Random().nextInt();
- PendingIntent contentItent = PendingIntent.getActivity(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);
- // 把Notification传递给NotificationManager
- notificationManager.notify(0, notification);
注意:
new Intent(this,this.getClass())保证了点击通知栏里的通知可以回到该Activity
但是,假如该Activity还在后台运行,并没有运行,通知事件响应后,系统会自动结束该Activity,然后再重新启动Activity,这不是我们要的。
解决方法为:在manifest.xml文件中找到该Activity,添加属性android:launchMode="singleTask“。这个属性很明显,就是只允许有一个该Activity运行,如果正在运行,则只能切换到当前运行的Activity,而不能重新启动Activity。
- import java.util.Timer;
- import java.util.TimerTask;
- public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
- private Timer mTimer = null;
- private TimerTask mTimerTask = null;
- private int isPause = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_mian_water);
- }
- private void startMyTimer(){
- if (mTimer == null) {
- mTimer = new Timer();
- }
- if (mTimerTask == null) {
- mTimerTask = new TimerTask() {
- @Override
- public void run() {
- do {
- try {
- if(isPause == 0) isPause = 1;
- else isPause = 0;
- Message message = new Message();
- message.what = isPause;
- handler.sendMessage(message);
- } catch (IllegalStateException e) {
- }
- } while (false);
- }
- };
- }
- if(mTimer != null && mTimerTask != null )
- mTimer.schedule(mTimerTask, 0, 500);
- }
- private void stopMyTimer(){
- if (mTimer != null) {
- mTimer.cancel();
- mTimer = null;
- }
- if (mTimerTask != null) {
- mTimerTask.cancel();
- mTimerTask = null;
- }
- }
- }
android中的本地定时推送到通知栏的更多相关文章
- 在Unity3D中实现安卓平台的本地通知推送
[前言] 对于手游来说,什么时候需要推送呢?玩过一些带体力限制的游戏就会发现,我的体力在恢复满后,手机会收到一个通知告诉我体力已完全恢复了.这类通知通常是由本地的客户端发起的,没有经过服务端. 在安卓 ...
- Android本地消息推送
项目介绍:cocos2dx跨平台游戏 项目需求:实现本地消息推送,需求①:定点推送:需求②:根据游戏内逻辑实现推送(比如玩家体力满时,需要计算后到点推送):需求③:清理后台程序或重启后依然能够实现本地 ...
- Android 基于Netty的消息推送方案之Hello World(一)
消息推送方案(轮询.长连接) 轮询 轮询:比较简单的,最容易理解和实现的就是客户端去服务器上拉信息,信息的及时性要求越高则拉信息的频率越高.客户端拉信息的触发可以是一些事件,也可以是一个定时器,不断地 ...
- IOS 本地通知推送消息
在现在的移动设备中,好多应用性的APP都用到了推送服务,但是有好多推送的内容,比如有的只是单纯的进行推送一个闹钟类型的,起了提醒作 用,有的则是推送的实质性的内容,这就分为推送的内容来区别用什么推送, ...
- Android 基于Netty的消息推送方案之对象的传递(四)
在上一篇文章中<Android 基于Netty的消息推送方案之字符串的接收和发送(三)>我们介绍了Netty的字符串传递,我们知道了Netty的消息传递都是基于流,通过ChannelBuf ...
- Android 基于Netty的消息推送方案之字符串的接收和发送(三)
在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...
- Android 基于Netty的消息推送方案之概念和工作原理(二)
上一篇文章中我讲述了关于消息推送的方案以及一个基于Netty实现的一个简单的Hello World,为了更好的理解Hello World中的代码,今天我来讲解一下关于Netty中一些概念和工作原理的内 ...
- Git总结笔记3-把本地仓库推送到github
说明:此笔记在centos 7 上完成 1.配置公钥 [root@kangvcar ~]# ssh-keygen -t rsa -C "kangvcar@126.com" [roo ...
- WebSocket(4)---实现定时推送比特币交易信息
实现定时推送比特币交易信息 实现功能:跟虚拟币交易所一样,时时更新当前比特币的价格,最高价,最低价,买一价等等...... 提示:(1)本篇博客是在上一遍基础上搭建,上一篇博客地址:[WebSocke ...
随机推荐
- 第36讲 activityForResult
第36讲 activityForResult activityForResult的作用是利用下一个activity给当前的activity传值(前一讲是利用当前activity给下一个activity ...
- maven编写主代码与测试代码
3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目主代码位于src/main/ja ...
- swift 自定义导航栏颜色
func setNavigationApperance(){ //自定义导航栏颜色 [self.navigationController?.navigationBar.barTintColor = U ...
- python中os模块常用方法
#!/usr/bin/python## os module test import os print 'os.name: ', os.nameprint 'os.getcwd(): ', os.get ...
- (转载)iOS Framework: Introducing MKNetworkKit
This article is available in Serbo-Croatian, Japanese and German. (Translations in Serbo-Croatian b ...
- Laravel-表单篇-零散信息
1.asset('path'):用于引入静态文件,包括css.js.img 2.分页,调用模型的paginate(每页显示的行数)方法, 如$student = Student::paginate(2 ...
- 奔五的人学IOS:swift练手与csdn,最近学习总结
早在五月份就准备開始学习ios开发,当时还是oc,学习了几天,最终不得其法.到了ios8开放,再加swift的出现.从10月份開始.最终找到了一些技巧,学习起来还算略有心得. 今天把我在学习swift ...
- 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位 准备篇: 一.系统约定 软件源代码包存放位 ...
- Web Api Session开启会话支持
1.WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 //代码如下 public override voi ...
- SQL Server -ISNULL()函数
SQL中有多种多样的函数,下面将为您介绍SQL中的ISNULL函数,包括其语法.注释.返回类型等,供您参考,希望对您学习SQL能够有所帮助. ISNULL 使用指定的替换值替换 NULL. 语法ISN ...