IM应用在推到后台时,有时候会有仍然能开麦说话的需求,这时候往往在app退到后台时开启一个前台服务,8.0以后需要设置通知栏;在设置通知栏的时候会指定一个setContentIntent方法来对应点击通知栏跳转的逻辑。
在实际开发中,使用
//获取启动Activity val intent = this.packageManager .getLaunchIntentForPackage(packageName)
return PendingIntent.getActivity(this ,1,intent,PendingIntent.FLAG_UPDATE_CURRENT)
在实际操作中,会发现点击通知栏会打开一次闪屏页,经过网上查证,原因是多了一个flag:FLAG_ACTIVITY_BROUGHT_TO_FRONT,故记录一下几种解决方案:
1.val intent = this.packageManager .getLaunchIntentForPackage(packageName) intent.setPackage(null) //添加此行代码
return PendingIntent.getActivity(this ,1,intent,PendingIntent.FLAG_UPDATE_CURRENT)
2.在SplashActivity的OnCretae方法中添加
if(!isTaskRoot) { finish() return }
3.在SplashActivity的OnCretae方法中添加
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0{ finish(); return; }
最近在bugly上看到很多
RemoteServiceException
Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{89e3b91 u0 com.funlink.building6/com.lqkj.base.common.KeepAppLifeService}
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2126)
初步推测有可能是在频繁前后台切换的时候,stopForeground在startForeground(1, getNotification())之前执行的原因,目前只是通过加了一个标志位来作为开启和关闭服务的依据,也有人说在onCreate中
@Override
public void onCreate() {
super.onCreate();
int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(NOTIFICATION_ID, new Notification.Builder(this).build());
}
// Do whatever you want to do here
}
就是每次保证 NOTIFICATION_ID不重复,此种方法未测试过是否有用,也没想到更好的解决办法!