Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a code for creating an alarmManager main activity

Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 14);
c.set(Calendar.MINUTE, 41);
c.set(Calendar.SECOND, 0);
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC,c.getTimeInMillis(), pendingIntent.getBroadcast(this, 0, intentAlarm, pendingIntent.FLAG_ONE_SHOT));

In the AlarmReceive class I have:

public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
    intent = new Intent(context, firstservice.class);
    context.startService(intent);
}

In service firstservice I did some operation. The problem is that the alarm is calling the broadcast receiver on time but after that when I open the app it also call the broadcast receiver and this mean that the service is worked again.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
139 views
Welcome To Ask or Share your Answers For Others

1 Answer

I also had this problem. After copple of hours, I could point out the problem with this solution. In brief I have used intent extras to notify the alarm time.

in the manifest.xml, register the receiver.

 <receiver android:name=".receivers.DayAlarmReceiver"/>

This is how I set the alarm manager

 public void setDayAlarm(Context context) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 4);

    AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent alarmIntent = new Intent(context, DayAlarmReceiver.class);
    alarmIntent.putExtra("HOUR_OF_DAY", 4);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 5555, alarmIntent, 0);
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
    Log.d("Set the day alarm");

}

This is the OnReceive() method of my DayAlarmReceiver class

@Override
public void onReceive(Context context, Intent intent) {
    int setHour = intent.getIntExtra("HOUR_OF_DAY", -1);
    Calendar rightNow = Calendar.getInstance();
    int currentHour = rightNow.get(Calendar.HOUR_OF_DAY);

    if(currentHour == setHour){
        Log.d("This is perfect, Trigger the alarm");
        showNotification(context, msg);
    }else{
       Log.d("This is perfect, Trigger the alarm");

    }

Happy coding :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...