I know that there are a lot of questions and answers out there regarding this topic but despite this I am not able to implement a never ending task which is running on an Android 7.0 device. It is working perfectly on emulator (using API level 24) but not on a real device (Xiaomi Redmi Note 4, Android 7.0 NRD90M). On emulator for instance when I close the application then the LocationUpdateService.onDestroy and the AutoStartLocationUpdate.onReceive methods are called properly as expected. But on the real device are not.
Or should I use some other approach like Alarm Manager instead?
Would someone please give me some hints? Thanks
Here is my implementation:
The Service:
package com.ivan.archangel;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class LocationUpdateService extends Service {
private Timer timer;
@Override
public void onCreate() {
super.onCreate();
timer = new Timer();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.i("Ivan", "tick");
}
}, 0, 1000);
Log.i("Ivan", "Timer started");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
Log.i("Ivan", "Timer stopped");
Intent broadcastIntent = new Intent("Hahaha");
getApplicationContext().sendBroadcast(broadcastIntent);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
The BroadcastReceiver
package com.ivan.archangel;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AutoStartLocationUpdate extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Ivan", "Restarting LocationUpdateService...");
context.startService(new Intent(context, LocationUpdateService.class));
Log.i("Ivan", "Restarted LocationUpdateService");
}
}
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ivan.archangel">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Home"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<service
android:name=".LocationUpdateService"
android:enabled="true"
android:stopWithTask="false"/>
<receiver
android:name=".AutoStartLocationUpdate"
android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped">
<intent-filter>
<action android:name="Hahaha" />
<!--<action android:name="android.intent.action.BOOT_COMPLETED" />-->
</intent-filter>
</receiver>
<receiver
android:name=".SMSReceiver"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="5822">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps"></activity>
</application>
</manifest>