,,生死有命,富贵在天, 楼主最近失恋, 单方面的, 所以想要全新的去学习姿势,不想再沉迷女色, 今天说说通知Notification的简单链式使用,
镇楼之宝
| /** | |
| * ************************************************************************ | |
| * ** _oo0oo_ ** | |
| * ** o8888888o ** | |
| * ** 88" . "88 ** | |
| * ** (| -_- |) ** | |
| * ** 0\ = /0 ** | |
| * ** ___/'---'\___ ** | |
| * ** .' \\\| |// '. ** | |
| * ** / \\\||| : |||// \\ ** | |
| * ** / _ ||||| -:- |||||- \\ ** | |
| * ** | | \\\\ - /// | | ** | |
| * ** | \_| ''\---/'' |_/ | ** | |
| * ** \ .-\__ '-' __/-. / ** | |
| * ** ___'. .' /--.--\ '. .'___ ** | |
| * ** ."" '< '.___\_<|>_/___.' >' "". ** | |
| * ** | | : '- \'.;'\ _ /';.'/ - ' : | | ** | |
| * ** \ \ '_. \_ __\ /__ _/ .-' / / ** | |
| * ** ====='-.____'.___ \_____/___.-'____.-'===== ** | |
| * ** '=---=' ** | |
| * ************************************************************************ | |
| * ** ·ð׿±£ÓÓ ÕòÀàÖ®±¦ ** | |
| * ************************************************************************ | |
| * | |
| */ |
1,先说说一些常量
DEFAULT_ALL 使用默认字段
DEFAULT_LIGHTS 默认闪光
DEFAULT_SOUND 默认声音(uri,指向路径)
DEFAULT_VIRATE 默认震动,后来得知需要添加震动权限VIBRATE: android.permission.VIBRATE
//设置Flag位
FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
PS:效果buff可以叠加, 测试请用真机
2,说说常用字段
contentView 通知在状态栏的显示View(自定义,具体请看下文) ,常与contentIntent配合使用,点击该通知后,
即触发contentIntent
contentIntent 设置PendingIntent对象,点击该通知时发送该Intent
flags 设置flag位,例如FLAG_NO_CLEAR等
defaults 添加效果
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳
icon 设置图标
3,常用的方法
public void cancelAll() 移除所有通知 (只是针对当前Context下的Notification)
public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public void notify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为id
public void notify(int id, Notification notification) 将通知加入状态栏,,标记为id
java代码展示public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private NotificationManager nm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but_notifi = findViewById(R.id.but_notification);
but_notifi.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.but_notification:
//下拉后跳转到相应的活动
Intent intent = new Intent(this,MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("这个世界我来过") //指定通知的标题内容
.setContentText("我来过这个世界") //指定通知的正文内容
.setWhen(System.currentTimeMillis()) //指定通知被创建的时间
.setSmallIcon(R.mipmap.ic_launcher) //设置通知的小图标,注意只能使用纯alpha图层的图片进行设置
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) //设置通知的大图标
.setContentIntent(pi)
.setAutoCancel(true) //让通知消失
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luga.ogg"))) //通知到了响起音频
.setVibrate(new long[] {0,1000,1000,1000}) //震动
.setLights(Color.GREEN,1000,1000) //LED灯一闪一闪的效果
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle().bigText("你好,世界, 我迷茫了"))
.build();
manager.notify(1,notification);
break;
default:
break;
}
}xml代码展示,其实就只有一个button了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity">
<Button
android:id="@+id/but_notification"
android:text="点击发送通知"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
最后别忘记加权限了
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
本文介绍了Android中Notification的链式调用方式及其常用配置,包括设置标题、内容、图标等,并提供了完整的Java代码示例。

1553

被折叠的 条评论
为什么被折叠?



