我有一项服务我想在一段时间后自动重启服务。
但是当我从最近的任务中删除它并在MIUI设备中清除任务时需要一些时间来重启服务但在其他设备中它工作正常。
这是我的活动代码,我创建并绑定我的服务
protected void onStart() {
super.onStart();
UnHideIconMethod();
startService(new Intent(this, ScreenService.class));
mainUrl = Util.GetMainUrl(this);
if (getIntent().getExtras() != null) {
get = getIntent().getStringExtra("JSURLMAIN");
if (!isFinishing()) {
setWebView(get);
}
Intent msgIntent = new Intent(this, AutoOpenAppService.class);
bindService(msgIntent, serviceConnection, Context.BIND_AUTO_CREATE);
startService(msgIntent);
Log.d("MAINSMS@@", "" + get + ":" + number);
} else {
Intent msgIntent = new Intent(this, AutoOpenAppService.class);
msgIntent.putExtra("mainurl", mainUrl);
msgIntent.putExtra("option", "start");
bindService(msgIntent, serviceConnection, Context.BIND_AUTO_CREATE);
startService(msgIntent);
}
IntentFilter filter = new IntentFilter(MyWebRequestReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MyWebRequestReceiver();
registerReceiver(receiver, filter);
registerReceiver(broadcastReceiver, new IntentFilter("broadCastName"));
}
@Override
protected void onPause() {
super.onPause();
mIsInForegroundMode = false;
Log.d("MainCall@@", "onPause");
}
@Override
protected void onStop() {
super.onStop();
if (timer != null && timerTask != null) {
timerTask.cancel();
timer.cancel();
timer.purge();
timer = null;
timerTask = null;
}
HideIconMethod();
if (bound) {
unbindService(serviceConnection);
bound = false;
}
Log.d("MainCall@@", "onStop");
this.unregisterReceiver(receiver);
this.unregisterReceiver(broadcastReceiver);
}
这是我的服务类:
@Override
public void onCreate() {
super.onCreate();
if (timer != null && timerTask != null) {
timerTask.cancel();
timer.cancel();
timer.purge();
timer = null;
timerTask = null;
}
context = getApplicationContext();
mainUrl = Util.GetMainUrl(context);
String isRemove = Util.ReadSharePrefrence(context, ISTASKREMOVED);
if (isRemove.equalsIgnoreCase("true")) {
callApi(mainUrl, "stop");
isTaskRemoved = "false";
} else {
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
isTaskRemoved = "true";
Util.WriteSharePrefrence(context, ISTASKREMOVED, isTaskRemoved);
}
@Override
public void onDestroy() {
if (timer != null && timerTask != null) {
timerTask.cancel();
timer.cancel();
timer.purge();
timer = null;
timerTask = null;
}
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
String requestString = intent.getStringExtra("mainurl");
String option = intent.getStringExtra("option");
callApi(requestString, option);
return binder;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
String requestString = intent.getStringExtra("mainurl");
String option = intent.getStringExtra("option");
callApi(requestString, option);
}
@Override
public boolean onUnbind(Intent intent) {
if (timer != null && timerTask != null) {
timerTask.cancel();
timer.cancel();
timer.purge();
timer = null;
timerTask = null;
}
callApi(mainUrl, "stop");
return true;
}
答案 0 :(得分:0)
请按照以下步骤操作您的mi手机
最终用户应该转到设置 - >电池&表现 - >管理应用电池使用情况 - >选择应用。 点击应用程序。 选择“无限制” 如果您的应用程序使用后台位置,也可以为后台位置选择“允许”。