I try to use AlarmManager with BroadcastReceiver.
My problem is that I dont get the intent fire from the AlarmManager.
Im not getting any call from Log.i("test", "test")
Here is my code:
public void activityCheck()
{
Intent intent = new Intent(this.getApplicationContext(), com.example.workoutlog.UsageCheckService.class);
intent.putExtra("nofitication", "nofitication");
PendingIntent service = PendingIntent.getService(this, 0, intent, 0);
AlarmManager m = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
Log.i("Yo", "Yo");
m.setRepeating(AlarmManager.RTC, time.getTimeInMillis(), 1000*60, service);
}
My BroadcastReceiver class
public class UsageCheckService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("test", "test");
}
}
In my manifest
<receiver android:name="com.example.workoutlog.UsageCheckService" >
</receiver>
If you device is in "sleep mode" (battery save), the Broadcast will not wake up it.
You must use WakefulBroadcastReceiver
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html
If the phone is in sleep state, you will have to wake it up.
Try this:
A small example:
This is working code. It wakes CPU every 10 minutes until the phone turns off.
Add to Manifest.xml:
Code:
Set Alarm from Service:
If you want set alarm repeating at phone boot time:
Add permission to Manifest.xml:
And create new class: