android 随手记 广播通知栏 二
关于通知栏的使用:
Notification及NotificationManager的使用详解
相关类:
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Notification;
1、使用标准的布局来显示通知信息
- Intent intent = new Intent(MainActivity.this, Other.class);
- PendingIntent pi = PendingIntent.getActivity(MainActivity.this,
- 0, intent, 0);
- Notification notify = new Notification();
- notify.icon = R.drawable.ic_launcher; // 通知狼的两个图标
- notify.tickerText = "启动其他activity的通知"; // 通知栏浮动的时候显示的信息
- notify.when = System.currentTimeMillis(); //设置时间
- notify.defaults = Notification.DEFAULT_SOUND; //默认的声音
- notify.defaults = Notification.DEFAULT_ALL;
- notify.setLatestEventInfo(MainActivity.this, "普通通知 ", "点击查看",pi);//使用标准的布局来显示通知信息
- NotificationManager no = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- no.notify(NOTIFICATION_ID, notify);
2、使用RemoteViews自定义布局
- Intent intent = new Intent(MainActivity.this,Other.class);
- //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
- Notification notification = new Notification();
- notification.tickerText="fadf";
- notification.icon=R.drawable.ic_action_search;
- RemoteViews contentview = new RemoteViews(getPackageName(),R.layout.status_bar_balloon);
- //这种是你自定义一个布局进行显示的。
- contentview.setTextViewText(R.id.text, "你好吧");
- contentview.setImageViewResource(R.id.icon, R.drawable.ic_action_search);
- //在视图中添加图片和文本进行显示。
- notification.contentView = contentview ;
- NotificationManager ni = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- ni.notify(NOTIFICATION_ID, notification);
3、带参数的Notification构造方法实现通知
- Intent intent = new Intent(MainActivity.this,Other.class);
- //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
- Notification notification = new Notification(R.drawable.ic_action_search,"fadf",SystemClock.currentThreadTimeMillis());
- //这一种里面的构造方法,可以理解为使用默认的布局进行现实的,显示图标,滚动图标的显示内容,以及显示时间。
- notification.setLatestEventInfo(MainActivity.this,"你好", "还不错", pi); //在通知栏中现实的内容
- NotificationManager ni = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- ni.notify(NOTIFICATION_ID, notification);