您的位置:首页 > 其它

RemoteViews 实现简单自定义通知栏

2016-04-05 16:58 204 查看

RemoteViews

作用:远程view,可跨进程更新界面。主要用于自定义通知栏及桌面小组件自定义通知栏:

Notification notification2 =new Notification();
notification2.icon=R.drawable.ic_launcher;
notification2.tickerText="ticker";
notification2.when=System.currentTimeMillis();
notification2.flags=Notification.FLAG_AUTO_CANCEL;
Intent it2=new Intent(this,ViewActivity.class);
PendingIntent pt=PendingIntent.getActivity(getBaseContext(), 1, it2, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.item_notifiction);
SimpleDateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd,HH-mm-ss");
remoteViews.setTextViewText(R.id.txt, dFormat.format(System.currentTimeMillis()));
remoteViews.setImageViewResource(R.id.img, R.drawable.ic_launcher);
remoteViews.setImageViewResource(R.id.img2, R.drawable.ic_launcher);
PendingIntent openActivity2pPendingIntent =PendingIntent.getActivity(getBaseContext(),0,it2,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.img, openActivity2pPendingIntent);
notification2.contentView=remoteViews;
notification2.contentIntent=pt;
NotificationManager nmManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nmManager.notify(2, notification2);


用法很简单,创建remoteView时只需提供 包名 及布局文件资源id即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: