赞
踩
一、通知的概念
通知是Android系统中一个特色功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现
二、通知的基本用法
2.1 通知的基本使用
1、通知可以在活动里创建,也可以在广播接收器里创建,还可以在服务里创建
2、创建步骤:
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this).build();
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("这是内容标题")
.setContentText("这是内容")
.setWhen(System.currentTimeMillis())
.build();
Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC")
.....
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("YonC", "YonC",NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
1、这里首先进行了一个判断,判断SDK的版本是否在26以上
2、创建NotificationChannel 的对象,传入三个参数:第一个为id,在创建Notification对象时会用到;第二个为通知渠道的的描述信息;第三个参数为该通知渠道的重要性
3、调用NotificationManager的createNotificationChannel方法创建渠道
notificationManager.notify(1,notification);
注意:第一个传入到参数id是唯一的,即要保证为每个通知所指定的id都是不同的
5、整体实现:
Button notification = findViewById(R.id.notification); notification.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC") .setContentTitle("这是内容标题") .setContentText("这是内容") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_qq) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_qq)) .build(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("YonC", "YonC",NotificationManager.IMPORTANCE_HIGH); notificationManager.createNotificationChannel(channel); } notificationManager.notify(1,notification); } });
2.2 PendingIntent的使用
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);//创建通知的“意图”
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
....
.setContentIntent(pendingIntent)
.build();
....
}
});
2.3 取消通知
解决方法有两种:
1、在NotificationCompat.Builder构造器后面再连缀一个setAutoCancel()方法,点击通知后通知就会自动取消
2、显式的调用NotificationManager的cancel()方法将它取消,代码如下:
public class NotificationActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(1);//这里传入参数id,即当时设置这条通知时的id
}
}
三、 通知的进阶技巧
1、播放音频文件:
Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC")
....
.setSound(Uri.parse(new File(音频文件路径)))
.build();
2、在通知来时让手机震动
Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC")
....
.setVibrate(new long[]{0,1000,1000,1000})
.build();
该数组的元素表示手机静止和震动的时长,以毫秒为单位:
下标为0的值表示手机静止时长;下标为1的值表示手机震动时长;下标为2同理静止;下标为3同理为震动
注意:手机振动需要声明权限
<uses-permission android:name="android.permission.VIBRATE"/>
3、在通知来时闪烁呼吸灯
Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC")
....
.setLights(Color.GREEN,1000,1000)
.build();
第一个参数为颜色;第二个参数为亮起时长;第三个参数为暗去时长
4、所有的设置都跟随系统
Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC")
....
.setDefaults(NotificationCompat.DEFAULT_ALL)
.build();
四、 通知的高级功能
NotificationCompat.Builder构造器中提供了很多API,如:
1、setStyle()方法:该方法允许我们构建出富文本的通知内容,使得通知中不光有文字还有图片等很多东西。该方法接收一个NotificationCompat.Style参数,该参数就是用于构建具体的富文本信息
(1)显示长文字:
Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC")
....
.setStyle(new NotificationCompat.BigTextStyle().bigText("长文本"))
.build();
(2)显示图片:
Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC")
....
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(
BitmapFactory.decodeResource(getResources(),R.drawable.ic_qq)))
.build();
通过BitmapFactory的decodeResource()方法将图片解析成Bitmap对象即可
2、setPriority()方法:用于设置通知的重要程度,该方法接收一个整型参数用于设置这条通知的重要程度。系统会根据通知的重要程度改变通知的显示顺序
Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC")
....
.setPriority(NotificationCompat.PRIORIY_MAX)
.build();
五、折叠式Notification
折叠式Notification是一种自定义视图的Notification,可以用于显示长文本和一些自定义布局场景。它存在两种状态:一种是普通状态下的视图;一种是展开状态下的视图,具体实现如下:
1、首先要自定义一个展开情况下的视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="100dp"> <ImageView android:id="@+id/image" android:src="@drawable/th1" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="20dp"/> <TextView android:id="@+id/text" android:layout_marginTop="30dp" android:layout_marginLeft="50dp" android:text="展开后的自定义视图" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
2、接下来就是代码实现
notification1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(MainActivity.this,"YonC") .setContentTitle("这是内容标题") .setContentText("折叠通知") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_qq) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_qq)) .build(); RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.view_fold);// notification.bigContentView = remoteViews;// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("YonC", "YonC",NotificationManager.IMPORTANCE_HIGH); notificationManager.createNotificationChannel(channel); } notificationManager.notify(2,notification); } });
1)与普通视图的定义不同的是,该视图用到RemoteViews来创建我们自定义的视图,传入两个参数:包名和自定义视图
2)通过Notification 的 bigContentView或contentView指定何时展示我们自定义的视图
六、悬挂式Notification
与普通Notification区别:用普通的通知,只能在状态栏提示,提示不够明显;用悬挂式的,提示够明显,自动消失或手动划除后,在状态就消失,具体做法:
只需要在获取Notification对象时Builder连缀下列代码即可:
.setFullScreenIntent(pendingIntent,true)
七、Notification的显示等级
分类:
1、VISIBILITY_PUBLIC:任何情况都会显示通知
2、VISIBILITY_PRIVATE:只有在没有锁屏时会显示通知
3、VISIBILITY_SECRET:在pin、password等安全锁和没有锁屏情况下才能够显示通知
设置方法:
只需要在获取Notification对象时Builder连缀下列代码即可:
.setVisibility(Notification.VISIBILITY_PUBLIC)
八、 小结
1、通知的基本用法,显示一个普通通知,并且点击后有反馈效果
2、通知的进阶技巧:播放音频;手机震动;闪烁呼吸灯;根据手机设置改变
3、通知的高级功能:显示长文字;显示图片;设置通知的重要程度
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。