我正在创建一个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.in我也触发一个广播重新启动相同的服务。我也在服务中使用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用于获取Broadcast的值
public final class ipManager {
public static final String INTENT_SIP_SERVICE_RESTART = "demo.magic.mobile.service.RestartService";
}
所有代码在我的moto G5和MI Note5 Pro设备上运行良好。但是在MI note 4和MI 4设备中,服务和广播在从后台删除应用程序后被杀死。
Moto G5和其他设备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
将不胜感激,谢谢。
在Service Class中使用此方法
@Override
public void onTaskRemoved(Intent rootIntent){
Intent broadcastIntent = new Intent(ipManager.INTENT_SERVICE_RESTART);
sendBroadcast(broadcastIntent);
}
最后,我得到了一个完美的解决方案我正在使用所有操作系统测试oreo,如氧气,颜色和miui。我正在开发一个VoIP应用程序,在我的应用程序中,一个应用程序被用户销毁或杀死后,一个SIP服务正在继续运行。用户设置应用程序总是在手机设置中运行后台和电池优化。
我正在使用警报管理器为了让我的服务保持活动,它比作业调度程序和再次呼叫服务更好。我在服务类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>