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()设置点击通知后将要启动的程序组件对应的PendingIntentsetSound()设置通知的自定义声音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可能收不到通知