I did not find a solution for this absurd problem. setRepeating() and setInexactRepeating() do not fire at all. I tried both this methods. The following is my code:
Intent intentService = new Intent(context, ServiceReceiver.class);
intentService.putExtra("checkStrikeAndNews", true);
PendingIntent pendingIntentSN = PendingIntent.getBroadcast(context, 0,intentService, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
//scheduling checkNews
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntentSN);
I also tried with setInexactRepeating() and it does not work. I also used ELAPSED_REALTIME_WAKEUP and SystemClock.elapsedRealtime(), same behaviour.
The broadcast receiver ServiceReceiver.class works perfectly it receives other intent and alarm intent (AlarmManager set() and setExact()) without problems. I tested the code on ICS, JellyBean and Marshmallow.
Thank you very much !
I think you have to use PendingIntent.getService()
instead of PendingIntent.getBroadcast()
Be sure your service is declared in your Manifest.xml
It's better to use setInexactRepeating()
for battery life
App.class where I setup the alarm
public class App extends Application {
private static final int INTERVAL = 1000 * 60;
private static final int DELAY = 5000;
@Override
public void onCreate() {
super.onCreate();
setupAlarm();
}
private void setupAlarm() {
PendingIntent myPendingIntent = PendingIntent.getService(this, 0, MyService.newIntent(this), 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, DELAY, INTERVAL, myPendingIntent);
}
}
MyService.class where I manage the intent received
public class MyService extends IntentService {
private static final String TAG = MyService.class.getSimpleName();
private static final String EXTRA_CHECK = "checkStrikeAndNews";
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public MyService() {
super("MyService");
}
public static Intent newIntent(Context context) {
return new Intent(context, MyService.class) //
.putExtra(EXTRA_CHECK, true);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent.getBooleanExtra(EXTRA_CHECK, false)) {
Log.i(TAG, "I just received an intent");
}
}
}
Manifest.xml Where I declared my service
<manifest
package="com.darzul.test"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.MyService" />
</application>
</manifest>
EDIT 01/02/2015
Here is the code with a BroadcastReceiver
App.class
public class App extends Application {
private static final int INTERVAL = 1000 * 60;
private static final int DELAY = 5000;
@Override
public void onCreate() {
super.onCreate();
setupAlarm();
}
private void setupAlarm() {
Intent myIntent = new Intent(this, MyReceiver.class);
PendingIntent myPendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, DELAY, INTERVAL, myPendingIntent);
}
}
WARNING: If you're testing on a device with a SDK equal or greater than 5.1, your interval alarm cannot be less than 1 minute.
Value will be forced up to 60000 as of Android 5.1; don't rely on this to be exact Frequent alarms are bad for battery life. As of API 22, the AlarmManager will override near-future and high-frequency alarm requests, delaying the alarm at least 5 seconds into the future and ensuring that the repeat interval is at least 60 seconds. If you really need to do work sooner than 5 seconds, post a delayed message or runnable to a Handler.
MyReceiver.class
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
}
}
Manifest.xml
<manifest
package="com.darzul.test"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:exported="false"
android:name=".receiver.MyReceiver" />
</application>
</manifest>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With