每当我打开应用程序时,Android中的AlarmManager都会运行

编程入门 行业动态 更新时间:2024-10-27 06:26:05
本文介绍了每当我打开应用程序时,Android中的AlarmManager都会运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有用于创建 alarmManager 主要活动的代码

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

在 AlarmReceive 类中,我有:

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.

推荐答案

我也遇到了这个问题。在几个小时之后,我可以指出此解决方案的问题。简而言之,我已经使用了意图附加功能来通知警报时间。

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.

在manifest.xml中,注册接收者。

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"); }

这是我的DayAlarmReceiver类的OnReceive()方法

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"); }

快乐编码:)

更多推荐

每当我打开应用程序时,Android中的AlarmManager都会运行

本文发布于:2023-11-27 03:22:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636362.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都会   当我   应用程序   Android   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!