I want to implement a background task in my android application. For Android 5.0 and lower, my code suffices with a simple BroadcastReceiver, where I can easily set an repeating alarm with
setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * seconds, pendingIntent);
On Android 6.0, however, I found out to replace my BroadcastReceiver with WakefulBroadcastReceiver and
setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
It is still not working at all and I don't know what my mistake is..
Here my code:
public class AlarmManagerBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire();
callback = new GPSActivityCallback() {...}
if(isConnectedToInternet()) {
gpsService = new GPSService(callback, context);
gpsService.refreshLocation();
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
if (extras != null && extras.getBoolean(REPEAT, Boolean.FALSE)) {
//Make sure this intent has been sent by the one-time timer button.
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
msgStr.append(formatter.format(new Date()));
}
else {
Toast.makeText(context, "Internetverbindung wurde unterbrochen", Toast.LENGTH_SHORT).show();
Log.d("INTERNET_TAG","Internetverbindung war kurrzeitig getrennt");
}
wl.release();
public void SetAlarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(REPEAT, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
if(Build.VERSION.SDK_INT < 23){
if(Build.VERSION.SDK_INT >= 19){
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
else{
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
}
else{
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pi);
}
public boolean isConnectedToInternet(){...}
}
This is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nymvno.hiob.prototyp_v30">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.nymvno.hiob.prototyp_v30.AlarmManagerBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="alarm" />
</intent-filter>
</receiver>
</application>
</manifest>