Android笔记
——通知栏
【若对该知识点有更多想了解的,欢迎私信博主~~】
通知栏:Notification
制作:
private void startNotification(String word,String word2)
{
notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
builder.setContentTitle(word)
.setContentText(word2)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("这是提示文本")
.setDefaults(Notification.DEFAULT_ALL);
notificationManager.notify(1,builder.build());
}
private void endNotification(){
notificationManager.cancel(1);
}
使用:
NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main10);
startNotification("这是标题","这是内容");
}
builder的方法:
方法 | 说明 |
---|
setDefaults() | 设置通知LED灯、音乐、震动等 |
setAutoCancel() | 设置点击通知后,状态栏自动删除通知 |
setContentTitle() | 设置通知标题 |
setContentText() | 设置通知内容 |
setSmallIcon() | 为通知设置图标 |
setLargeIcon() | 为通知设置大图标 |
setTicker() | 设置通知在状态栏的提示文本 |
setContentIntent() | 设置点击通知后将要启动的程序组件对应的PendingIntent |
setSound() | 设置通知的自定义声音 |
setVibrate() | 设置通知的自定义震动 |
通知方式:
属性 | 说明 |
---|
Notification.DEFAULT_SOUND | 设置使用默认声音 |
Notification.DEFAULT_LIGHTS | 设置使用默认闪光灯 |
Notification.DEFAULT_VIBRATE | 设置使用默认震动 |
Notification.DEFAULT_ALL | 设置使用默认声音、震动、闪光灯 |
注:
1.在使用闪光灯和振动器时需要添加权限
<!--添加操作闪光灯的权限-->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!--添加操作振动器的权限-->
<uses-permission android:name="android.permission.VIBRATE"/>
2.高版本API可能收不到通知