我正在创建一个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


感谢您的帮助,在此先感谢。

最佳答案

在服务类别中使用此方法

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

关于android - 内存终止或销毁 Activity 时,Service和BroadcastReceiver在MI电话中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55847610/

10-10 09:47