我正在创建一个RestartServiceBroadcast,以在杀死应用程序后使我的后台服务始终保持活动状态。
public class RestartServiceBroadcast extends BroadcastReceiver {
Context ctx;
private PreferencesProviderWrapper prefProviderWrapper;
@Override
public void onReceive(Context context, Intent intent) {
this.ctx= context;
System.out.println("RestartServiceBroadcast:");
if(intent.getAction().equals(ipManager.INTENT_SERVICE_RESTART)){
startOneService();
}
}
private void startOneService() {
}
}
在这里,我还创建了一项服务来连接服务器IP。在该服务中,我还触发了广播以重新启动相同的服务。我也在服务中使用START_STICKEY
public class IpService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("ipStack : onDestroy");
Intent broadcastIntent = new Intent(ipManager.INTENT_SERVICE_RESTART);
sendBroadcast(broadcastIntent);
}
}
我正在mainActivity onDestroy()方法中调用Broadcast。
override fun onDestroy() {
val broadcastIntent = Intent(ipManager.INTENT_SERVICE_RESTART)
sendBroadcast(broadcastIntent)
super.onDestroy()
}
这是我的清单
<service
android:name=".service.ipService"
android:enabled="true"
android:exported="false"
android:permission="demo.magic.mobile.android.permission.CONFIGURE_IP"
android:process=":ipStack"
android:stopWithTask="false">
<intent-filter>
<action android:name="demo.magic.mobile.service.ipService" />
<action android:name="demo.magic.mobile.service.ipConfiguration" />
</intent-filter>
</service>
<receiver
android:enabled="true"
android:exported="true"
android:name=".service.receiver.RestartServiceBroadcast"
android:process=":ipStack">
<intent-filter>
<action android:name="demo.magic.mobile.service.RestartService" />
</intent-filter>
</receiver>
用于获取广播值的ipManager类
public final class ipManager {
public static final String INTENT_SIP_SERVICE_RESTART = "demo.magic.mobile.service.RestartService";
}
所有代码在我的motoG5和MI Note5 Pro设备中均能正常工作。但在MI note4和MI4设备中,从后台删除该应用程序后,服务和广播已被终止。
MotoG5和其他设备Logcat在杀死应用程序时像这样。
2019-04-25 15:27:00.220 2345-3735/? W/ActivityManager: Scheduling restart of crashed service demo.magic.mobile/.service.ipService in 20942ms
2019-04-25 15:27:21.192 2345-2410/? I/ActivityManager: Start proc 23954:demo.magic.mobile:ipStack/u0a247 for service demo.magic.mobile/.service.ipService
2019-04-25 15:27:21.490 23954-23954/demo.magic.mobile:ipStack I/System.out: ipStack : onstart
MI Note4的Logcat杀死应用程序进程
2019-04-25 15:26:04.584 1604-2918/? I/ActivityManager: Start proc 10971:demo.magic.mobile:ipStack/u0a643 for service demo.magic.mobile/.service.ipService caller=demo.magic.mobile
2019-04-25 15:36:04.216 1604-2785/? I/ActivityManager: Start proc 19393:demo.magic.mobile:ipStack/u0a643 for service dailer.demo.magic.mobile/.service.ipService caller=demo.magic.mobile
我们将不胜感激,谢谢您。
答案 0 :(得分:1)
在服务类别中使用此方法
@Override
public void onTaskRemoved(Intent rootIntent){
Intent broadcastIntent = new Intent(ipManager.INTENT_SERVICE_RESTART);
sendBroadcast(broadcastIntent);
}
答案 1 :(得分:1)
最后,我得到了一个完美的解决方案。我正在使用氧气,色彩和miui等所有操作系统在oreo中进行测试。我正在开发VoIP应用程序,并且在应用程序被用户破坏或杀死后,一个sip服务正在继续运行。用户设置应用程序还有另外一件事,那就是始终在手机设置中运行后台和电池优化。
我正在使用Alarm Manager来使我的服务保持活动状态,它比作业计划程序再呼叫服务更好。我在服务类的oncreate()方法中实现了此代码。
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"
android:process=":sipStack">
<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>