Android RemoteView的应用 二 通知栏

来源:互联网 发布:mac怎么打dota2 编辑:程序博客网 时间:2024/05/17 21:49

这一次我们通过使用RemoteView来制作一个通知栏
这个通知栏由一个ImageView和TextView和Button构成,点击Button会跳转到TwoActivity,点击到其他位置会跳转到MainActivity。
1. 先创建一个Notification
Notification notification = new Notification();
notification.icon =R.drawable.sample;
notification.tickerText = “hello world”;
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;

2。 创建RemoteView并引入Notification,这里可以看出这个PendingIntent使id为open_activity2具有点击后能跳转到TwoActivity的能力

RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.widget);
remoteViews.setTextViewText(R.id.msg, “chapter_5”);
remoteViews.setImageViewResource(R.id.icon1, R.drawable.sample2);
PendingIntent openActivity2PendingIntent = PendingIntent.getActivity(this,0,
new Intent(this,TwoActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.open_activity2,openActivity2PendingIntent);
notification.contentView = remoteViews;

3。给Notification本身添加点击回馈,并发送通知
Intent intent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = pendingIntent;
NotificationManager manager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);

manager.notify(2,notification);

这上面可以看出这个程序并不复杂,主要还是人家给RemoteView集成了
代码下载链接如下:
RemoteView制作自定义Notification - 下载频道 - CSDN.NET
http://download.csdn.net/detail/z979451341/9809506

下期详细讲PendingIntent,这个为何如此厉害

0 0