0

I'm having a really strange issue, the broadcast receiver is not working on my own device but is on the android emulator. What does my application is restarting the service by calling the broadcast receiver everytime onDestroy method is called. I even tried to look at developer option on my device to see if the was some restriction activated like "not keep activities", but it was unchecked.

Xml file

<service
    android:name="oak.shef.ac.uk.testrunningservicesbackgroundrelaunched.SensorService"
    android:enabled="true">
</service>

<receiver
    android:name="oak.shef.ac.uk.testrunningservicesbackgroundrelaunched.SensorRestarterBroadcastReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="RestartServiceWhenStopped"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="SensorRestarterBroadcastReceiver" />
    </intent-filter>
</receiver>

Service

public class SensorService extends Service {

    private Timer timer;
    MyThread myThread;
    private TimerTask timerTask;

    public int counter=0;
    public SensorService(Context applicationContext) {
        super();
        Log.i("HERE", "here I am!");
    }

    public SensorService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        myThread = new MyThread();
        myThread.start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("EXIT", "ondestroy!");
        Intent restartService = new Intent("SensorRestarterBroadcastReceiver");
        sendBroadcast(restartService);
        timer.cancel();
        myThread.interrupt();
    }

    public class MyThread extends Thread{

        @Override
        public void run() {
            // TODO Auto-generated method stub

            try {
                int delay = 10000; // delay for 10 sec.
                int period = 10000; // repeat every 10 sec.
                timer = new Timer();
                timer.scheduleAtFixedRate(new TimerTask() {
                    public void run() {
                        Log.i("in timer", "in timer ++++  "+ (counter++));
                    }
                }, delay, period);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

    }

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

Brodcast Receiver

public class SensorRestarterBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(SensorRestarterBroadcastReceiver.class.getSimpleName(),
"Restarting Service");

        context.startService(new Intent(context, SensorService.class));;
    }

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Zxhammelzx
  • 23
  • 4
  • what are the android versions of emulator and devices – Vivek Mishra Oct 12 '18 at 11:35
  • 24 both of them. I downloaded it just for testing if the problem was the version, and it works on emulator. – Zxhammelzx Oct 12 '18 at 11:49
  • I have tried with another device with the same API and it works. I have a xiaomi redmi 4x, I'm sure something is being stopped by my device, I'd like to know what thought. – Zxhammelzx Oct 12 '18 at 14:43
  • Which phone are you testing on? It may have something to do with the OEM preventing some background services to start as part of their battery optimization routines as mentioned here - https://stackoverflow.com/a/41627296/6835535 – Bhavin Jun 28 '19 at 10:00

0 Answers0