在我的一个应用程序中,我正在使用前台服务获取用户当前位置... 我的问题是,当我从最近的应用程序中删除应用程序时,它会强制停止我的应用程序并停止我的服务 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) {}
并且在 list 中我已经将我的上述服务添加为
<service
android:name=".service.LocationUpdateForegroundService"
android:enabled="true"
android:stopWithTask="false"
android:exported="true" />
但是当我从最近的应用程序中删除应用程序时,它会强制关闭我的应用程序,如果我检查应用程序设置它显示为强制关闭应用程序。(以上问题仅与某些设备有关。不会在每个设备上都出现此类问题设备 ) 我应该怎么办?请帮我 根据建议,我喜欢参加我的服务类(class),但还没有得到我的解决方案!!!
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但是,当用户单击最近 Activity 的关闭按钮时,它会停止我的服务,并且会从内存中清除所有应用程序
最佳答案
我得到了一个完美的解决方案。我在 oreo 中测试所有操作系统,如氧气、颜色和 miui。我正在开发一个 VoIP 应用程序,在我的应用程序中,一个 sip 服务在应用程序被用户破坏或杀死后继续运行。用户设置应用程序的另一件事是始终在手机设置中运行后台和电池优化。
我正在使用警报管理器来保持我的服务处于 Activity 状态,这比作业调度程序和再次调用服务更好。我在服务类 oncreate() 方法中实现了这段代码。
PendingIntent mKeepAlivePendingIntent;
public class CallService extends Service {
@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent(this, RestartServiceBroadcast.class);
mKeepAlivePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60000, 60000, mKeepAlivePendingIntent);
}
}
创建 BroadcastReceiver 以在用户从内存任务中杀死应用程序时再次调用服务
public class RestartServiceBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("service.RestartService")) {
// Here Call a Service
}
}
}
像这样显示
<receiver
android:name=".service.receiver.RestartServiceBroadcast"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="service.RestartService" />
</intent-filter>
</receiver>
<service
android:name=".service.CallService"
android:enabled="true"
android:exported="false"
android:stopWithTask="false">
</service>
谢谢,编码愉快....
关于android - 从最近的应用程序关闭应用程序时服务停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231243/