Adam Adam - 1 month ago 22
Android Question

AlarmManeger setExact is not working

setExact method is not working on my project

Log.d("Alarm","Fire");


that is in AlarmManagerBroadcastReceiver is not showing up after 1 minute!

public class MainActivity extends AppCompatActivity {
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
.
.

Calendar alarm = Calendar.getInstance();
alarm.setTimeInMillis(System.currentTimeMillis());
alarm.add(Calendar.MINUTE,1);

Intent myIntent = new Intent(MainActivity.this, AlarmManagerBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(), pendingIntent);
.
.
}
}


AlarmManagerBroadcastReceiver:

public class AlarmManagerBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

Log.d("Alarm","Fire");

}
}


I have tried to extend BroadcastReceiver instead of WakefulBroadcastReceiver. I also tried to set diffrenet hard coded times. However, nothing works.

Please help

Answer

This link (Scheduling Repeating Alarms) helps you. You must add

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> <receiver android:process=":remote" android:name=".AlarmManagerBroadcastReceiver"></receiver>