0

I have been searching in the entire web understanding why my background service is not working. When I close the application the service is killed without being called back from the Brodcast Receiver. This guide : https://www.quora.com/How-do-I-keep-an-app-running-in-the-background-in-MIUI shows three different methods and none of them is working for me. I'm sure that the application is perfectly working because it works fine with the emulator, same API 24, and it worked with a huawei aswell, again same API.

XML

<service
    android:name="com.arvi.neverendingbackgroundservice.SensorService"
    android:enabled="true"
    android:exported="true"
    android:stopWithTask="false">
</service>

<receiver
    android:name="com.arvi.neverendingbackgroundservice.SensorRestartBroadcastReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="RestartServiceWhenStopped">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
    </intent-filter>
</receiver>

Service

public class SensorService extends Service {
    private Context ctx;
    TimerCounter tc;
    private int counter = 0;
    private static final String TAG = SensorService.class.getSimpleName();

    public SensorService() {

    }

    public SensorService(Context applicationContext) {
        super();
        ctx = applicationContext;
        Log.i(TAG, "SensorService class");
    }

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

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate()");

        tc = new TimerCounter();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.i(TAG, "onStartCommand()");


        tc.startTimer(counter);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "serviceOnDestroy()");

        super.onDestroy();

        Intent broadcastIntent = new Intent(getApplicationContext(),SensorRestartBroadcastReceiver.class);
        sendBroadcast(broadcastIntent);
        tc.stopTimerTask();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.i(TAG, "serviceonTaskRemoved()");



        // workaround for kitkat: set an alarm service to trigger service again
        Intent intent = new Intent(getApplicationContext(), SensorService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 5000, pendingIntent);

        super.onTaskRemoved(rootIntent);

    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.i(TAG, "onLowMemory()");
    }
}
Zxhammelzx
  • 23
  • 4
  • What does "not working" mean? It doesn't start? It doesn't receive callbacks? It gets killed after a while? – Pawel Oct 13 '18 at 19:39
  • I'm going to update my post, sorry for not explaining it. After the application is closed the service is being killed, so the brodcast receiver doesn't call it back. – Zxhammelzx Oct 13 '18 at 19:44
  • @Pawel Any solution after my explanation? – Zxhammelzx Oct 13 '18 at 20:09
  • Can't help with what you're trying to accomplish since at this point it's just old API "hack" that won't work on any device with android Oreo or higher. If you really need something to run indefinitely it's best practice to use a foreground service, since they are less likely to get killed in the first place. – Pawel Oct 13 '18 at 20:53
  • This is well-known issue on MIUI (China Custom OS), so I suggest you find the solution in Chinese forum If you know some Chinese. If you want to know what App works in MIUI, there is one called "Alarmy ( 睡你妹鬧鐘)". Also, It's developed by China Team. – Tokenyet Nov 11 '18 at 17:01

0 Answers0