最近写了一个notification的demo,搞了半天怎么都弹不出自己的想要的activity,最后发现竟然是在manifest.xml里面没有注册这个activity,崩溃。。。。。。!
notification其实挺简单的,照着固定模式写就行了,也许是我研究的不够深入吧。
notification有两种表现方式,第一个就是以默认的布局展示:
//定义一个notificationmanager,它的作用就是显示和取消notification。。easy
NotificationManager m_NotificationManager=(NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
Notification m_Notification=new Notification();
//设置提醒icon 文字 声音
m_Notification.icon=R.drawable.notice;
m_Notification.tickerText=message;
m_Notification.defaults=Notification.DEFAULT_SOUND;
//点击notification时候发送的打开acitivity的intent
Intent m_Intent=new Intent(context,ShowWeb.class);
//需要打开一个新的activity时候,必须要设定Intent.FLAG_ACTIVITY_NEW_TASK
m_Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(context, 0, m_Intent, PendingIntent.FLAG_CANCEL_CURRENT);
//下面这行是打开setting的
//PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent("android.settings.SETTINGS"), PendingIntent.FLAG_CANCEL_CURRENT);
m_Notification.setLatestEventInfo(context, "foolfrog", "this is a test",pi );
//下面这行是设定自动消失,即点击notification之后,就消失
//m_Notification.flags |= Notification.FLAG_AUTO_CANCEL;
//弹出。。。第一个参数是自己设定的一个ID,取消的时候也用到这个ID。比如没有设定自动消失,可以在打开的acitivity里面
写:m_NotificationManager.cancel(0);
m_NotificationManager.notify(0,m_Notification);
第二个就是自定义了:(偷懒,以下是网摘)
如何使自己的Notification像Android QQ一样能出现在 “正在运行的”栏目下面
其实很简单,只需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。
如何改变 Notification 在“正在运行的”栏目下面的布局
创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便可以了,如:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews rv =
new
RemoteViews(Main.
this
.getPackageName(), R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text,
"Hello,there,I'm john."
);
n.contentView = rv;
n.contentIntent = contentIntent;
nm.notify(R.string.app_name, n);
|
本文详细介绍了Notification的两种表现方式及实现步骤,并分享了如何使Notification像AndroidQQ一样出现于‘正在运行的’栏目下的技巧。
5684

被折叠的 条评论
为什么被折叠?



