好久没写文章,今天遇到一个新需求,app正在通话过程切换到后台,点击通知栏的时候 回到通话界面。
直接上代码吧
首先是实现通知的代码
NotificationCompat.Builder notificationBuild =null;intNOTIFY_ID =100;
NotificationManager mNotificationManager;booleanisHasNotification =false;
android.app.Notification notification;/**
* 显示通知栏
*/public void showNotification(String title,String content){if(mNotificationManager ==null&&!isHasNotification){
Intent deleteIntent =newIntent(this, NotificationTipsReceiver.class);
notificationBuild =newNotificationCompat.Builder(this);
notificationBuild.setAutoCancel(false);//设置能手动取消notificationBuild.setOngoing(true);//设置不能取消notificationBuild.setContentTitle(title);
notificationBuild.setSmallIcon(R.mipmap.icon);//设置图标notificationBuild.setTicker("小尾巴 正在接单中");
notificationBuild.setDefaults(android.app.Notification.DEFAULT_ALL);
notificationBuild.setPriority(android.app.Notification.PRIORITY_MAX);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0, deleteIntent,0);
notificationBuild.setContentIntent(pendingIntent);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
notification = notificationBuild.build();
mNotificationManager.notify(NOTIFY_ID, notification);
isHasNotification =true;
}
下面是通知点击处理类
publicclassNotificationTipsReceiverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Context context, Intent intent) {
//判断app进程是否存活
booleanisApplive = SystemUtil.isAppAlive(context,"com.iol8.te.tr");
Intent dialogIntent =newIntent(context, DialogueActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|Intent.FLAG_ACTIVITY_NEW_TASK);//注意这里是关键 经过测试Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED也可以用Intent.FLAG_ACTIVITY_SINGLE_TOP 也可以做到 具体区别没有去深入测试if(isApplive) {if(WYIMManager.isDialogue) {if(!SystemUtil.isAppRunningForeground(context)){//app在后台运行context.startActivity(dialogIntent);
}return;
}
}
}
}
另外附上一个Intent的flag 启动介绍(感谢这位大神的分享)