简体   繁体   中英

Long running Android service only running on specific devices

GENERAL

I did much research on how to make a Service long running and i think that i have succeeded, but not at all, as i found out that my application does not run correctly on specific devices. My service should run even if the corresponding activity gets closed, so i have already registered an BOOT_COMPLETED receiver and start my service via Alarm Manager.

FINDINGS

My tests included:

  • HUAWEI P7 / Android V6.0
  • HUAWEI P8 / Android V6.0
  • HUAWEI P9 / Android V7.0
  • NEXUS (Emulator) / Android V6.0

On HUAWEI P9 and Android Emulator the service keeps running after the application task gets closed and the services even run after booting the device. The service even restarts itself, if i kill the service by hand.

On HUAWEI P7 and P8 the service gets destroyed every time I am destroying the task and it does not restart itself. My Boot receiver does not start my services too.

SERVICE

public class QuestionService extends Service {
    /* Attributes */

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        /* Some Code*/

        new QuestionTask().execute();

        return START_NOT_STICKY;
    }


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

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
    }

    private class QuestionTask extends AsyncTask<Void, Long, Void> {

        @Override
        protected Void doInBackground(Void... params) {
           /* Task to be executed periodically */
            return null;
        }

        @Override
        protected void onProgressUpdate(Long... values) {
            super.onProgressUpdate(values);

            /* gets periodically called and executes some code */
        }
    }
}

RECEIVER

 public class DeviceBootReceiver extends BroadcastReceiver {

    private static final long REPEAT_TIME = 1000 * 30;
    private SharedPreferences sp;

    @Override
    public void onReceive(Context context, Intent intent) {
        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        //if(!sp.getBoolean("firstStart",true)) {

            /* AppActivationService starten */
        AlarmManager service = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);

        Calendar cal = Calendar.getInstance();
        // start 15 seconds after boot completed
        cal.add(Calendar.SECOND, 15);



            /* QuestionService starten */
        Intent iq = new Intent(context, QuestionStartReceiver.class);
        PendingIntent quest = PendingIntent.getBroadcast(context, 0, iq, PendingIntent.FLAG_CANCEL_CURRENT);



        service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), REPEAT_TIME, quest);


        //}

    }
}

QuestionStartReceiver:

public class QuestionStartReceiver extends BroadcastReceiver {
    public QuestionStartReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, QuestionService.class);
        context.startService(i);
    }
}

MANIFEST

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name=".receiver.QuestionStartReceiver" />
<receiver android:name=".receiver.DeviceBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
</receiver>
<service android:name=".services.QuestionService"
            android:exported="false" />

I had also tried using WakeLocker, but it seems that this does not solve my problem, as the services simply destroy and do not start themselves while not beeing in sleep mode.

I have just found out the answer coincidentally while testing another app!

HUAWEI has a built-in feature called "protected apps". Installed applications (and therefore all corresponding services) that are not marked as protected will be killed by the system when the main process gets destroyed. Marking my application as "protected" solved my problem and now every service runs in background, even after killing my application manually.

This topic describes how to handle this struggle: "Protected Apps" setting on Huawei phones, and how to handle it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM