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 ...
随机推荐
- Entify Framewrok - Join的使用方法
问题:有2个表,使用id相连,如何用Join语法将其连接起来? 如下代码 List<tblAssociation> assoList = dataContext.tblAssociatio ...
- 漫话Unity3D(一)
前言 使用Unity已经有将近半年时间了,尽管项目还仅仅是个半成品,可是Unity差点儿相同玩熟了.这里把使用过程中碰到的问题梳理一遍.不会涉及到太过详细的功能和代码,可是假设开发一个网游又都会涉及到 ...
- 用递归翻转一个栈 Reverse a stack using recursion
明白递归语句之前的语句都是顺序运行,而递归语句之后的语句都是逆序运行 package recursion; import java.util.Stack; public class Reverse_a ...
- js jsp 时间 日期 控件 插件 简单 实用
js时间控件一般都是找网上的用,这东西平常很少涉及到,一用到找起来却烦死人,不是没用就是太复杂,今天向大家推荐一个简单实用的控件,该控件在不断更新,而且有专门的网站对它进行维护,所以值得一看. 先说它 ...
- VMware SphereESXi上安装虚拟机
VMware SphereESXi上安装虚拟机 创建新虚拟机 此处以CentOS为例 注意:配置上传的系统文件位置及启动项
- LB集群
LB集群 1. LB.LVS介绍LB集群是load balance 集群的简写,翻译成中文就是负载均衡集群LVS是一个实现负载均衡集群的开源软件项目 LVS架构从逻辑上可分为调度层(Directo ...
- 理清fineuploader无刷新上传的一些事
1.fineuploader是一款不依赖与jquery的异步无刷新上传组件,fineuploader采用ajax方式实现对文件上传,返回值都是以json的格式,对后台服务器操作和前端dom对象一些操作 ...
- Visual Studio 2015 开发MVC4出现错误
在Visual Studio 2015(以下简称VS2015)中开发MVC4项目时,编译报错"当前上下文中不存在ViewBag",一直无法编译,这个是否是VS2015的Bug? 本 ...
- EasyUI layout动态设置Split属性
switch (rowData.ISREGISTERTASK) { case 0: 显示north分割线 $('#RightContent').la ...
- 大家来找茬-SpringMVC中Tomcat正常启动,始终访问不了Controller,出404错
创建了一个空的SpringMVC项目,Tomcat可以正常启动,但是运行的时候,始终进不了Controller,并且报404错误. 百度各种查,结果也是查不到原因.各个群里面各种求,各种贴源码,也没有 ...