Android 在内存终止或销毁活动时,MI电话中的服务和广播接收器不工作

Android 在内存终止或销毁活动时,MI电话中的服务和广播接收器不工作,android,android-service,android-broadcast,Android,Android Service,Android Broadcast,我正在创建一个one RestartServiceBroadcast,以在终止应用程序后保持后台服务始终处于活动状态 public class RestartServiceBroadcast extends BroadcastReceiver { Context ctx; private PreferencesProviderWrapper prefProviderWrapper; @Over

我正在创建一个one 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()方法中的广播

 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>
在我的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

感谢您的帮助。请在服务课上使用此方法

@Override 
    public void onTaskRemoved(Intent rootIntent){
        Intent broadcastIntent = new Intent(ipManager.INTENT_SERVICE_RESTART);
        sendBroadcast(broadcastIntent);
    }

最后,我找到了一个完美的解决方案。我在奥利奥测试所有操作系统,如氧气、颜色和miui。我正在开发一个VoIP应用程序,在我的应用程序中,一个sip服务在应用程序被用户破坏或杀死后继续运行。还有一件事,用户设置应用程序总是在手机设置中运行后台和电池优化

我使用Alarm Manager来保持我的服务处于活动状态,这比作业调度器和再次呼叫服务要好。我在服务类oncreate()方法中实现了这段代码

创建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>

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>