Android通知(Notification)兼容到AndroidQ,以及使用中的bug

Android通知(Notification)兼容到AndroidQ,以及使⽤中的
bug
/**
* 安卓8.0以后创建通知需要做兼容
* channelId 要唯⼀,⾃定义即可,如 "进程销毁通知","TestNotification","FirstServiceNotification",⾃定义即可
* 创建包含内容的通知,点击通知跳转LoginActivity
* 第⼀个参数是Context 的时候不能置为前台进程,只能发送通知等操作
*/
public static void createHasDataNotification(Context context, String channelId, int notificationId) {
//创建通知管理器
NotificationManager manager = (NotificationManager) SystemService(context.NOTIFICATION_SERVICE);
//设置点击通知时的响应事件
Intent intent = new Intent(context, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = Activity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//NotificationManager.IMPORTANCE_MIN: 静默;
//NotificationManager.IMPORTANCE_HIGH:随系统使⽤声⾳或振动
NotificationChannel channel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_HIGH);
channel.setLightColor(Color.RED);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
Notification notification = new Notification.Builder(context)
.setTicker("紧急通知")
.setContentText("聚合环境切换APP已退出,请点击重新启动")//设置通知内容
.setAutoCancel(true)//设置点击通知后⾃动删除通知
.setChannelId(channelId) //设置Id,安卓8.0后必须加
.setContentIntent(pendingIntent) //设置点击通知时的响应事件
.setSmallIcon(R.mipmap.changyoulogo)//通知左侧的⼩图标
.
build();
//设置当前进程为前台进程,第⼀个参数是Context不能置为前台进程
//            context.startForeground(notificationId, notification);
} else {
Notification notification = new Notification.Builder(context)
.setTicker("紧急通知")阻燃双面胶
.setContentText("聚合环境切换APP已退出,请点击重新启动")//设置通知内容
.setAutoCancel(true)//设置点击通知后⾃动删除通知
.setContentIntent(pendingIntent) //设置点击通知时的响应事件
.
setSmallIcon(R.mipmap.changyoulogo)//通知左侧的⼩图标
.setDefaults(Notification.DEFAULT_SOUND)//使⽤系统默认的声⾳或震动
.build();
//设置当前进程为前台进程,第⼀个参数是Context不能置为前台进程
//            context.startForeground(notificationId, notification);
if (null != notification) {
notification.flags |= Notification.FLAG_AUTO_CANCEL;
}
//创建通知管理器
}
}
/**
* 安卓8.0以后创建通知要做兼容处理
* channelId 要唯⼀,⾃定义即可,如 "进程销毁通知","TestNotification","FirstServiceNotification",⾃定义即可
* 创建包含内容的通知,点击通知跳转LoginActivity
* 第⼀个参数是Service 就可以置为前台进程,当前通知从Service中发出
*/虹膜识别芯片
public static void createHasDataNotification(Service context, String channelId, int
notificationId) {
//创建通知管理器
NotificationManager manager = (NotificationManager) SystemService(context.NOTIFICATION_SERVICE);
//设置点击通知时的响应事件
模具水嘴
Intent intent = new Intent(context, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = Activity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//NotificationManager.IMPORTANCE_MIN: 静默;
//NotificationManager.IMPORTANCE_HIGH:随系统使⽤声⾳或振动
NotificationChannel channel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_HIGH);
channel.setLightColor(Color.RED);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
Notification notification = new Notification.Builder(context)
.setTicker("紧急通知")
.setContentText("PP已退出,请点击重新启动")//设置通知内容
.setAutoCancel(true)//设置点击通知后⾃动删除通知
.setChannelId(channelId) //设置Id,安卓8.0后必须加
.setContentIntent(pendingIntent) //设置点击通知时的响应事件
.setSmallIcon(R.mipmap.changyoulogo)//通知左侧的⼩图标
.
build();
//设置当前进程为前台进程,增加进程保活率
context.startForeground(notificationId, notification);
} else {
Notification notification = new Notification.Builder(context)
.setTicker("紧急通知")
.setContentText("APP已退出,请点击重新启动")//设置通知内容
.setAutoCancel(true)//设置点击通知后⾃动删除通知
.setContentIntent(pendingIntent) //设置点击通知时的响应事件
.
setSmallIcon(R.mipmap.changyoulogo)//通知左侧的⼩图标
.setDefaults(Notification.DEFAULT_SOUND)//使⽤系统默认的声⾳或震动
.build();
//设置当前进程为前台进程,增加进程保活率
context.startForeground(notificationId, notification);
if (null != notification) {
notification.flags |= Notification.FLAG_AUTO_CANCEL;
}
//创建通知管理器
}
}
问题描述:
1. 我现在有⼀个简单的Demo,⼀个三个Activity,BaseActivity,LoginActivity,MainActivity.
2. 分别让LoginActivity,MainActivity继承BaseActivity,在MainActivity中使⽤startService()启动FirstService,在FirstService的
onDestroy()⽅法中发送通知,通知当前APP的使⽤者,进程被销毁,APP已退出,可是不知道Service什么时候被GM回收,为了测试通知我就在MainActivity的onCreate()⽅法中直接发送通知.测试通知的兼容性
3. 在BaseActivity中重写onBackPressed(),监听返回按钮,弹出Dialog,让⽤户选择是否退出,如果退出则还原⼀些设置,并执⾏退出逻辑,
代码如下:
4. 正式步⼊主题:LoginActivity登录成功后跳转MainActivity,MainActivity的onCreate⽅法中发送通知:
4.1 现在登录成功,通知也发送出去了,页⾯停留在MainActivity,此时按下home键,回到桌⾯,点击
通知会跳转到LoginActivity页⾯, 执⾏登录,成功后跳转到MainActivity页⾯,此时按下返回键,退出App,成功退出APP以后,APP会再次启动,如果登录成功后停留在MainActivity页⾯,不点击home键,现在APP在前台显⽰,点击通知,执⾏上⾯的退出逻辑,则APP不会重新启动⼀次.
4.2问题分析:第⼀个APP启动后,点击home到后台后,进程并没有被销毁,这个LoginActivity和MainActivity还存在栈中,点击通知跳转到LoginActivity登录成功跳转到MainActivity,⼜重新开启⼀个线程,⼜会创建⼀个LoginActivity和MainActivity,存放在栈中,此时退出的是第⼆个栈的LoginActivity和MainActivity,第⼀次创建的LoginActivity和MainActivity并没有被杀死,还存放在栈中,所以会重新启动⼀次.第一语音
5.解决办法:
直接给BaseActivity,MainActivity,LoginActivity设置singleInstance的启动模式,让每个Activity单独使⽤⼀个栈,当点击通知再次启动的时候,栈中有当前Activity对象,就不会重新创建⼀次,故此在栈中就⼀个MainActivity,LoginActivity的对象,退出的时候就可以直接退出,不会出现重启的现象
下⾯是⾃定义Dialog的代码,也就是上⾯的退出dialog的源码,有需要的朋友可以直接复制使⽤1. ⾸先⾃定义⼀个Dialog
/**
* ⽤⾃定义dialog
*/
public class CustomDialog extends Dialog {
private Button commitButton;//确定按钮
private Button cancelButton;//取消按钮
private TextView titleTextView;//标题
private TextView messageTextView;//提⽰⽂字
private String titleText;//设置给标题的⽂字
private String messageText;//提醒内容
private String commitText;//设置给确定按钮的⽂字
private String cancelText;//设置给取消按钮的⽂字
private ColorStateList titleTextColor;//设置给标题⽂字的颜⾊
private ColorStateList messageTextColor;//设置给提醒内容的⽂字颜⾊
private onCommitClickListener commitClickListener;//确定按钮的监听接⼝
private onCancelClickListener cancelClickListener;//取消按钮的监听接⼝
public CustomDialog(@NonNull Context context) {
super(context, R.style.CustomDialogStyle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.custom_dialog_layout);
this.setCanceledOnTouchOutside(false);
//初始化控件
initView();
//初始化dialog的内容
initDate();
//按钮点击监听
clickListener();
荸荠削皮机}
private void clickListener() {
commitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (commitClickListener != null)
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cancelClickListener != null)
}
});
}
private void initView() {
titleTextView = (TextView) findViewById(R.id.custom_dialog_title_textview);
messageTextView = (TextView) findViewById(R.id.custom_dialog_message_textview);
commitButton = (Button) findViewById(R.id.custom_dialog_commit_button);
cancelButton = (Button) findViewById(R.id.custom_dialog_cancel_button);
}
//设置⽂字和颜⾊
private void initDate() {
if (!TextUtils.isEmpty(titleText)) {//标题⽂字
titleTextView.setText(titleText);
}
if (!TextUtils.isEmpty(messageText)) {//提⽰内容
messageTextView.setText(messageText);
}
if (!TextUtils.isEmpty(commitText)) {//确定按钮⽂字
commitButton.setText(commitText);
}
if (!TextUtils.isEmpty(cancelText)) {//取消按钮⽂字
cancelButton.setText(cancelText);
}
if (titleTextColor != null) {//标题⽂字颜⾊
titleTextView.setTextColor(titleTextColor);
}
if (messageTextColor != null) {//提醒⽂字颜⾊
messageTextView.setTextColor(messageTextColor);
}
}
public void setTitle(String title) {
this.titleText = title;
}
public void setTitleColor(int color) {
titleTextColor = ColorStateList.valueOf(color);
}
public void setMessage(String message) {
}
public void setMessageColor(int color) {
70secmessageTextColor = ColorStateList.valueOf(color);
}
public void setOnCommitClickListener(String btnText, onCommitClickListener yesClickListener) {        commitText = btnText;
thismitClickListener = yesClickListener;

本文发布于:2024-09-23 10:22:37,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/277419.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:通知   进程   点击   设置   退出
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议