ios notificationn.setLatestEventInfo;过时怎么替代

Android_Notification的使用方法_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
文档贡献者
评价文档:
Android_Notification的使用方法
大小:3.13KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢Android用户界面之notification(状态栏通知)_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Android用户界面之notification(状态栏通知)
来源:Linux社区&
作者:kuangc2008
对于通知,应先明白:通知概要 通知标题 通知内容 通知图标 通知时间
首先,看界面。状态栏上的是:图标和概要。
将状态栏往下拉,会出来标题、内容和时间。
通知除了状态栏的图标外,还可以 开打设备上的LED灯,发送声音、震动来提醒用户
通知可以告诉用户在后台发生了某事,所以经常在广播接受者和服务中使用;
例子开始的界面如下:点击发送后,会发送通知。
主要用到的方法有:
1、得到通知管理者:通过getSystemService(String).来得到NotificationManager,在该类中调用cancel(int)来清除通知。
2、指定通知的最新信息:notification.setLatestEventInfo(this, title, content, pendingIntent);
3、 PendingIntent类解析:
带有特定标记(flag)的的intent,由静态方法getActivity(Context, int, Intent, int flag), getBroadcast(Context, int, Intent, int flag), getService(Context, int, Intent, int flag)来创建。
相关资讯 & & &
& (08/11/:27)
& (05/06/:13)
& (04月17日)
& (05/31/:15)
& (04/02/:07)
图片资讯 & & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款android 浅谈notification - CSDN博客
,俗称通知,是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。
二:讲解和实现
1.Notification:在Android4.x中,用Notification.Builder(this).builder()创建,在3.0之前用new Notification()来创建
Notification notify =&new&Notification.Builder(this)
&&&&&&&&&&&&.setAutoCancel(true)
&&&&&&&&&&&&.setTicker(&有新消息&)
&&&&&&&&&&&&.setSmallIcon(R.drawable.ic_launcher)
&&&&&&&&&&&&.setContentTitle(&新通知&)
&&&&&&&&&&&&.setContentText(&加薪了&)
&&&&&&&&&&&&.setWhen(System.currentTimeMillis())
&&&&&&&&&&&&.setContentIntent(PendingIntent);
&&&&&&&&&&&&.build();
2.NotificationManager:是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象。
在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3.PendingIntent:要得到一个pendingIntent对象,使用方法类的静态方法&,,
Context:上下文,一般为当前对象,this
Intent:就是一个Intent
最后一个参数:
FLAG_CANCEL_CURRENT:如果构建的PendingIntent已经存在,则取消前一个,重新构建一个。
FLAG_NO_CREATE:如果前一个PendingIntent已经不存在了,将不再构建它。
FLAG_ONE_SHOT:表明这里构建的PendingIntent只能使用一次。
FLAG_UPDATE_CURRENT:如果构建的PendingIntent已经存在,则替换它,常用。
StatusService.java
package com.jiajia.
import android.annotation.SuppressL
import android.app.IntentS
import android.app.N
import android.app.NotificationM
import android.app.PendingI
import android.content.I
public class StatusService extends IntentService {
public StatusService() {
super(&StatusService&);
protected void onHandleIntent(Intent intent) {
System.out.println(&开始下载。。。&);
showNotification(false);
Thread.sleep(10000);
showNotification(true);
}catch(Exception e){
e.printStackTrace();
@SuppressLint(&NewApi&)
@SuppressWarnings(&deprecation&)
private void showNotification(boolean f){
@SuppressWarnings(&deprecation&)
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
* 4.x用这个
// notification = new Notification.Builder(this).build();
// .setAutoCancel(true)
// .setTicker(&有新消息&)
// .setSmallIcon(R.drawable.ic_launcher)
// .setContentTitle(&新通知&)
// .setContentText(&加薪了&)
// .setWhen(System.currentTimeMillis())
// .setContentIntent(pi)
if(f == false){
* new Notification() 已过时
notification = new Notification(R.drawable.xiazai, &开始下载&, System.currentTimeMillis());
notification.setLatestEventInfo(this, &下载&, &正在下载中&, contentIntent);
notification = new Notification(R.drawable.xiazai, &下载完成&, System.currentTimeMillis());
notification.setLatestEventInfo(this, &下载完成&, &完成下载&, contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(R.id.home, notification);
1:notification中when干吗的
System.currentTimeMillis();
获取当前的毫秒数
可以用来测试程序的运行时间
读取数据的时候来计算时间的!
通知栏 设置 提示的间隔时间 ==s
通知栏的三个参数中,图标内容,和发生时间。
Notification(icon, tickerText,when);
如果同一个通知栏有新的内容要更新根据这个时间来判断更新。
持续更新。。。1587人阅读
两种通知方式:
* Toast 事例
* API文档中写道:
* Note: Do not use the public constructor for a Toast unless you are going to define the layout with setView(View).
* If you do not have a custom layout to use, you must use makeText(Context,int, int) to create the Toast.
* 翻译:不要使用Toast的构造函数创建对象,除非你准备用setView定义布局
* 如果你没有一个自定义的布局使用,你必须用makeTest(Context, int,int)创建一个Toast
privatevoid toastTest() {
Toast toast = Toast.makeText(this,&test&, Toast.LENGTH_LONG);
Toast toast = new Toast(this); //这样是错的
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show();
* 自定义Toast 事例
* API文档中写道:
* Note: Do not use the public constructor for a Toast unless you are going to define the layout with setView(View).
* If you do not have a custom layout to use, you must use makeText(Context,int, int) to create the Toast.
* 翻译:不要使用Toast的构造函数创建对象,除非你准备用setView定义布局
* 如果你没有一个自定义的布局使用,你必须用makeTest(Context, int,int)创建一个Toast
privatevoid customToastTest() {
//用getLayoutInflater()或者(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)得到LayoutInflater对象,如下面两行
LayoutInflater li = getLayoutInflater();
LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = li.inflate(R.layout.toast_layout,null);
Toast toast = new Toast(this);
// Toast toast = Toast.makeText(this, &test&, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
二、notification
privatevoid notificationTest() {
// 1、得到一个NotivicationManager对象
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 2、实例化Notification
int icon = R.drawable.ic_
CharSequence tickerText =&Hello&;
long when = System.currentTimeMillis();
// Notification notification = new Notification();
Notification.Builder nb =new Notification.Builder(this);
nb.setSmallIcon(icon);
nb.setTicker(tickerText);
nb.setWhen(when);
Notification noti = nb.getNotification();
//定义notification的消息和PendingIntent
nb.setContentText(&Hello world!&);
nb.setContentTitle(&My notification&);
Intent notificationIntent =new Intent(this, IntentToActivity.class);
Bundle b = new Bundle();
finalint HELLO_ID = 1;
b.putInt(&id&, HELLO_ID);
b.putSerializable(&a&, (Serializable)nm);
notificationIntent.putExtra(&aa&, b);
PendingIntent pd = PendingIntent.getActivity(this, 0, notificationIntent, 0);
nb.setContentIntent(pd);
Notification noti = nb.getNotification();
//把 Notification传到NotificationManager中
nm.notify(HELLO_ID, noti);
// 由于调用notify后,通知栏中有通知了,但点击通知后,通知不消失
目前打算点击通知栏中的通知后让通知消失,于是做了一下操作
//下面两行是将通知的id和NotificationManager对象传到启动的
// activity(IntentToActivity.class,在此类中定义了两个变量:
// private static NotificationM和p,//并且提供了setter,getter方法对外暴力接口)中,然后在
// IntentToActivity.class中的oncreate方法中调用nm.cancel(id);用//来取消通知栏中的通知
IntentToActivity.setId(HELLO_ID);
IntentToActivity.setNm(nm);
注意:文档中写道,Notification notification = new Notification(icon, tickerText, when);和
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
都已经过时,建议用Notification.Builder
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:61651次
排名:千里之外
原创:12篇
转载:31篇
评论:12条
(2)(1)(1)(1)(1)(1)(1)(1)(2)(9)(13)(7)(2)

我要回帖

更多关于 notification 的文章

 

随机推荐