Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

service - AlarmManager in android runs every time i open the app

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

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

1 Reply

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...