直播录屏中应用容易被杀掉解决方案

直播录制手机屏幕的时候应用容易被杀死,录制没有几分钟就挂了,再次点开应用就是重启的状态

在网上查询说是在AndroidMinfest.xml 的application标签里加上 android:persistent="true" 这个玩意,但是这个只有放在System/app目录下的应用加上才有用,其他的应用加上也没用,也有可能崩溃

另外一种就是将应用里加上前台服务,让系统认为应用一直在被使用,从而不会被杀死

直播录屏会加上通知栏告知用户正在录屏

在当前的Activity里开启前端服务,代码如下:

public class LiveService extends Service{
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent it = new Intent(this, GameLiveStreamingActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, it, 0);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(me.lifre.live.R.drawable.icon_headview)
.setContentTitle(getString(string.recording_screen))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setTicker(getString(R.string.recording_screen))
.setOngoing(true)
.setContentIntent(contentIntent);
Notification notification1 = notification.build();
startForeground(1, notification1);//设置为前台服务
  return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
ISLog.d("直播测试","销毁服务");
super.onDestroy();
}
}
如此,系统就不会轻易杀死你的应用了,音乐播放器类似这些app都是这样的
posted @ 2018-09-06 14:22  zx巴拉  阅读(428)  评论(0编辑  收藏  举报