2018-06-05-Notification
2018-06-05 本文已影响0人
天才蒙蒙亮
Notification 通知栏
Notification是在应用常规界面之外展示的消息。当app让系统发送一个消息的时候,消息首先以图标的形式显示在通知栏中。要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看。通知栏和通知抽屉都是系统曾main控制的,你可以随时查看,不限制于App。
Notication的创建
public class MainActivity extends Activity {
private static final int NID_1 = 0x1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendnotification1(View v){
//API 11以后
// Notification.Builder builder = new Notification.Builder(this);
//v4支持包
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
//设置相关属性
builder1.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder1.setContentTitle("你有一条新的消息");
builder1.setContentText("你好啊");
//创建一个通知对象
Notification n = builder1.build();
//获取系统的通知管理器,并发送
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NID_1,n);
}
}
notification的其他属性
//其他不必要的属性
builder1.setAutoCancel(true);
builder1.setDefaults(Notification.DEFAULT_ALL); //声音、震动、呼吸灯
builder1.setNumber(10);
builder1.setTicker("新消息"); //没下拉的时候显示的字
builder1.setOngoing(true); //设置为常驻通知
利用Intent,通过notification打开另一个Activity
public class MainActivity extends Activity {
public static final int NID_1 = 0x1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendnotification1(View v){
//API 11以后
// Notification.Builder builder = new Notification.Builder(this);
//v4支持包
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
//设置相关属性
builder1.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder1.setContentTitle("你有一条新的消息");
builder1.setContentText("你好啊");
//其他不必要的属性
builder1.setAutoCancel(true);
builder1.setDefaults(Notification.DEFAULT_ALL); //声音、震动、呼吸灯
builder1.setNumber(10);
builder1.setTicker("新消息"); //没下拉的时候显示的字
builder1.setOngoing(true); //设置为常驻通知
//定义一个意图,当店家通知时要打开一个界面
Intent intent = new Intent(this,Main2Activity.class);
//参数:上下文,请求编码,意图,创建PendingIntent的方式
// PendingIntent.FLAG_CANCEL_CURRENT :取消当前的PI,创建新的
// PendingIntent.FLAG_NO_CREATE :如果有就使用,没有就不创建
// PendingIntent.FLAG_ONE_SHOT :只是用一次
// PendingIntent.FLAG_UPDATE_CURRENT :如果有,更新Intent,没有就创建
PendingIntent pi = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
//通知的事件
builder1.setContentIntent(pi);
//创建一个通知对象
Notification n = builder1.build();
//获取系统的通知管理器,并发送
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NID_1,n);
}
}
另一个被打开的Activity
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//打开界面后取消指定ID的通知
//Notification是系统层的服务,从哪里都可以获取到
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(MainActivity.NID_1);
}
}
第二个Activity中取消notification和第一个Activity中
builder1.setAutoCancel(true);
这里的效果一样,原因是notification是系统层的服务,从哪里都可以获取
大视图的notification
大视图的东西样式设置一般在Style中,普通设置(比如说setContentTitle、setContentText)在builder
//API 11以后
// Notification.Builder builder = new Notification.Builder(this);
//v4支持包
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
//设置相关属性
builder1.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder1.setContentTitle("消息");
builder1.setContentText("消息");
//新建样式
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle("吟诗作对");
style.addLine("长亭外");
style.addLine("古道边");
style.addLine("一行白鹭上青天");
builder1.setStyle(style);
Notification n = builder1.build();
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NID_2,n);
显示进度条通知
线程模拟发送进度
public void sendnotification3(View v){
//API 11以后
// Notification.Builder builder = new Notification.Builder(this);
//v4支持包
final NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
//设置相关属性
builder1.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder1.setContentTitle("更新中..");
builder1.setContentText("正在升级");
builder1.setProgress(100,5,false); //总进度,现在进度,确定的
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NID_3,builder1.build());
//模拟更新的线程
new Thread(new Runnable() {
@Override
public void run() {
for(int progress = 0; progress <= 100; progress+=5){
NotificationCompat.Builder builder = builder1.setProgress(100, progress, false);
nm.notify(NID_3, builder1.build());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
builder1.setProgress(0,0,false);
builder1.setContentText("更新完成");
nm.notify(NID_3,builder1.build());
}
}).start();
}
}
自定义通知视图
- 通知的框架允许自己去定义同志的布局。通过REmoteViews对象来定义通知的外观,自定义的通知布局与常规通知类似,但是它是基于定义在xml文件的RemoteViews对象来操作的。
- 自定义通知的可用高度取决于通知视图的。正常的视图布局高度限制在256dp.
- 为了去定义自己的通知布局,从扩充xml文件获取一个RemoteViews对象的实例展开开始。然后类似于调用setContentTitle方法一样,我们呢需要调用setcontent()。为了能设置更多的细节,我们使用RemoteViews对象的方法来设置更多的内容。
public void sendnotification4(View v){
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//创建一个远程的视图
RemoteViews views = new RemoteViews(getPackageName(),R.layout.custom_layout);
builder.setContent(views);
builder.setTicker("音乐");
builder.setOngoing(true);
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
//view显示的其他内容
//button也是继承
views.setTextViewText(R.id.button,"暂停");
// views.setOnClickPendingIntent(); 按钮的单击事件,通过Intent启动另一个Actiity
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NID4,builder.build());
}
自定义的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:maxWidth="64dp"
android:maxHeight="64dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:text="歌曲名称"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>