标签:
安卓系统通知用户三种方式:
1.Toast Notification
2.Dialog Notification
3.Status Bar Notification Status Bar Notification,状态栏通知
发送一个状态栏通知必须用到两个类:NotificationManager,Notification
1.NotificationManager是一个系统Service,必须通过getSystemService()获取
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2.Notification是具体的状态栏通知对象
调用NotificationManager的notify()方法创建Notification
两部分:
①:状态栏通知
notification.icon=R.drawable.ic_launcher;
notification.tickerText="My First Notification";
notification.when=System.currentTimeMillis();
②:下拉通知列表和点击跳转:
两种方式:
一:setLatestEventInfo()方法
Context context = getApplicationContext();
CharSequence contentTitle="Notification";
CharSequence contentText="Notification Context";
Intent intent=new Intent(Main.this,Turn.class);
PendingIntent pendingIntent=PendingIntent.getActivity(Main.this, 0, intent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
二:自定义通知栏
notification.flags=Notification.FLAG_AUTO_CANCEL;用户点击后通知自动取消
设置两个变量contentView和contentIntent
RemoteViews contenView=new RemoteViews(getPackageName(), R.layout.notification_layout);
contenView.setImageViewResource(R.id.icon, R.drawable.ic_launcher);
contenView.setTextViewText(R.id.contentText, "自定义通知");
notification.contentView=contenView;
Intent intent1=new Intent(Main.this,Turn.class);
PendingIntent pendingIntent1=PendingIntent.getActivity(Main.this, 0, intent1, 0);
notification.contentIntent=pendingIntent1;
Tips:
可能遇到的错误:Couldn‘t expand RemoteViews for:
检查是否是RemoteViews对应的layout里使用了它不支持的组件
检查RemoteViews对应的layout布局文件是否有基本错误,例如忘记声明宽高等
安卓状态栏通知Status Bar Notification
标签:
原文地址:http://www.cnblogs.com/dazzlingstar/p/5069866.html