【发布时间】:2019-12-05 11:02:27
【问题描述】:
在我的一个应用程序中,我正在使用前台服务获取用户当前位置... 我的问题是当我从最近的应用程序中删除应用程序时,它会强制停止我的应用程序并停止我的服务 for this device i am getting such issue 我尝试了如下所示的服务
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
//TRY 1 but not worked!!!!
// Intent broadcastIntent = new Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
// sendBroadcast(broadcastIntent);
// stoptimertask();
// Intent broadcastIntent = new Intent(this, SensorRestarterBroadcastReceiver.class);
// sendBroadcast(broadcastIntent);
//
// writeDataInLogFile("Service onTaskRemoved ", " TRIED TO RESTART SERVICE FROM ONDESTROY ");
Log.i(TAG, "Task Removed!!!!!!!!!!!!!!!!!!!!!!");
try {
//TRY 2 ALSO NOT WORKING
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
} catch (Exception e) {}
在清单中,我已将上述服务添加为
<service
android:name=".service.LocationUpdateForegroundService"
android:enabled="true"
android:stopWithTask="false"
android:exported="true" />
但是,当我从最近的应用程序中删除应用程序时,它会强制关闭我的应用程序,如果我检查应用程序是否将其设置为强制关闭应用程序。(上述问题仅与某些设备有关.. 并非每个设备都出现此类问题设备 ) 我该怎么办?请帮我 根据建议,我喜欢参加我的服务班,但还没有得到我的解决方案!!!
try {
System.out.println("this is start!!!!!!!!!!!!!!!!!!!!");
Intent intent = new Intent(this, PowerOnOffBroadcastReceiver.class);
intent.setAction("service.RestartService");
PendingIntent mKeepAlivePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10000, 10000, mKeepAlivePendingIntent);
}
catch (Exception e)
{
System.out.println("new implementation !!!!!!!!!!!!!!!!!");
}
我发现一些解决方案是:This 但是当用户单击最近活动的关闭按钮并从内存中清除所有应用程序时,它会停止我的服务
【问题讨论】:
-
手机安卓版本是多少?
-
@Mr AF 请检查我在链接中提供的图片
标签: android