Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

android - background service has stopped on miui rom

I am developing a background services and in OnDestroy Method, I've called an intent to start my services again. I'ts not started again on miui rom (Xiaomi mobile and huawei mobile).

How do I handle this?

public class NotificationsService extends Service {

@Override
public void onCreate() {
    ApplicationLoader.postInitApplication();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void onDestroy() {
    Intent intent = new Intent("example.app.start");
    sendBroadcast(intent);
}
}

In Manifest:

    <receiver android:name=".AppStartReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="example.app.start" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It doesn't new on Xiaomi because Xiaomi has a feature called app permission, where user has to allow the app to start automatically (Service).

Go like this and allow your app to autostart:

Settings > permissions > Autostart

Or,

Don't try to restart the same Service inside onDestroy() instead use START_STICKY inside onStartCommand(Intent intent, int flags, int startId) method.

Again you are sending broadcast not starting a service, Use onDestroy properly:

@Override
public void onDestroy() {
Intent intent = new Intent("example.app.start");
sendBroadcast(intent);
super.onDestroy();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...