废话不多说,先上代码:
/**
* Author : 马占柱
* E-mail : mazhanzhu_3351@163.com
* Time : 2021/8/19 16:33
* Desc : 通知栏管理类
*/
public class NotifyManager {
public static final String CHANNEL_ID = "mayidaijiasj"; //通道渠道id
public static final String CHANEL_NAME = "蚂蚁代驾司机端"; //通道渠道名称
//使用volatile关键字保其可见性
volatile private static NotifyManager instance = null;
private NotificationManager mNotificationManager;
public static final int mp3ID = 4716;
private NotifyManager() {
}
public static NotifyManager getInstance() {
try {
if (instance != null) {//懒汉式
} else {
//创建实例之前可能会有一些准备性的耗时工作
Thread.sleep(300);
synchronized (NotifyManager.class) {
if (instance == null) {//二次检查
instance = new NotifyManager();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return instance;
}
public void init(Context context) {
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
public void mp3(Context context, int type) {
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) App.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
}
RemoteViews remoteViews = new RemoteViews(AppUtils_Ma.getPackageName(context), R.layout.notification_control);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, Activity_Main_2.class), 0);
remoteViews.setOnClickPendingIntent(R.id.mzz, contentIntent);
if (type == 1) {
remoteViews.setTextViewText(R.id.content, "司机已上线");
} else {
remoteViews.setTextViewText(R.id.content, "司机已下线");
}
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
CHANEL_NAME,
NotificationManager.IMPORTANCE_LOW);
channel.setSound(null, null);
channel.setLockscreenVisibility(IMPORTANCE_MAX);//设置锁屏是否显示通知
channel.setBypassDnd(true);//设置是否可以绕过请勿打扰模式
mNotificationManager.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(context, CHANNEL_ID);
builder.setSmallIcon(R.mipmap.logo)
.setContentTitle("蚂蚁代驾")
.setContentText("软件运行中") //以上的设置是在为了兼容3.0之前的版本
.setCustomBigContentView(remoteViews)
.setAutoCancel(false)//false 通知栏点击后不消失
.setOnlyAlertOnce(true)
.setOngoing(true);//true 不能滑动删除
notification = builder.build();
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
builder.setSmallIcon(R.mipmap.logo)
.setContentTitle("蚂蚁代驾")
.setContentText("软件运行中") //以上的设置是在为了兼容3.0之前的版本
.setAutoCancel(false)//false 通知栏点击后不消失
.setOngoing(true)//true 不能滑动删除
.setPriority(Notification.FLAG_ONLY_ALERT_ONCE)//优先级越高的,通知排的越靠前,优先级低的,不会在手机最顶部的状态栏显示图标
.setContent(remoteViews);
notification = builder.build();
}
mNotificationManager.notify(mp3ID, notification);
}
/**
* 清空所有通知
*/
public void clear() {
if (mNotificationManager != null) {
mNotificationManager.cancelAll();
}
}
/**
* 根据id清除通知
*/
public void cancelNotifyById(int id) {
if (mNotificationManager != null) {
mNotificationManager.cancel(id);
}
}
/**
* 根据id清除通知
*/
public void cancelNotifyByMp3() {
if (mNotificationManager != null) {
mNotificationManager.cancel(mp3ID);
}
}
}
使用的时候,首先在Application里面初始化
NotifyManager.getInstance().init(this);
如果不初始化的话,也不会报错,因为我做判断了,没啥事的;
这是我的使用方式:
NotifyManager.getInstance().mp3(context, 1);
这是代码里面的布局文件notification_control
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/mzz"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/no_img"
android:layout_width="@dimen/dp_35"
android:layout_height="@dimen/dp_35"
android:layout_marginLeft="@dimen/dp_10"
android:background="@mipmap/logo"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp_10"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="【蚂蚁代驾】竭诚为您服务"
android:textColor="@color/Txt_Title"
android:textSize="@dimen/sp_10" />
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center"
android:singleLine="true"
android:text="@string/hint_content"
android:textColor="@color/red"
android:textSize="@dimen/sp_14"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
这里要注意,布局文件里面,不是所有的控件都可以使用的【判断方法,随便点击一个控件名字,进源码查看】只有带下面的这个标识,才是可以在这里使用的
实际使用效果