Android的重复报警器不能正确重复 [英] Android repeating alarm not repeating correctly

查看:331
本文介绍了Android的重复报警器不能正确重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想重复各地每5分钟发出警报。出于测试目的,我把它设置为重复连用5 代替,因为这样的:

  AlarmManager alarmManager =(AlarmManager)getActivity()getSystemService(getActivity()ALARM_SERVICE)。
        意向意图=新意图(getActivity(),CoordinateAlarmReceiver.class);
        的PendingIntent的PendingIntent = PendingIntent.getService(getActivity(),1,意向,0);
        INT repeatSeconds = 5;
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()
                repeatSeconds * 1000,的PendingIntent);

和接收IntentService打印日志语句当它接收到报警。然而,围绕触发一次分半钟,而不是每5秒一次,它是设置不正确在哪里?我一直在使用setRepeating()而不是setInexactRepeating(也试过),但我得到相同的结果。

下面是我的警报接收器:

 公共类CoordinateAlarmReceiver扩展IntentService {    公共CoordinateAlarmReceiver(){
        超级(CoordinateAlarmReceiver);
    }    / *
    接到报警,得到新的联系人,然后显示通知
     * /
    @覆盖
    保护无效onHandleIntent(意向意图){
        MyLog.i(坐标接到报警);        //新GetNewContactsTask(本).execute();
    }
}


解决方案

我在哪,我需要一个服务触发每15秒一个类似的问题......我做了以下。


  1. 我有一个扩展应用程序调用的所有MyApplication的类。此类包含报警管理器的实例。

     公共类MyApplication的扩展应用{
        私人MyAlarmManager alarmMgr;    @覆盖
        公共无效的onCreate(){
            Log.d(TAG,所有MyApplication的onCreate);
            super.onCreate();
            Log.d(TAG,INITING alarmMgr ......);
            alarmMgr =新MyAlarmManager(本);
        }    公共MyAlarmManager getAlarmManager(){
            返回alarmMgr;
        }
    }

    <醇开始=2>

  2. 这是AlarmManager称为MyAlarmManager创建,启动和放大器;停止报警现在设置此一个服务的下一个报警。

     公共类MyAlarmManager {
        私人所有MyApplication MAPP;
        私人意图意图;
        私人的PendingIntent的PendingIntent;
        私人AlarmManager alarmMgr;
        私有静态最后长的频率= 15000;    公共MyAlarmManager(上下文的背景下){
            MAPP =(所有MyApplication)范围内;
            // Android的报警服务
            alarmMgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);        //服务将每15秒发射
            意图=新意图(背景下,MyService.class);
            的PendingIntent = PendingIntent.getService(背景下,1,意向,PendingIntent.FLAG_UPDATE_CURRENT);
            setNextAlarm();
        }    公共无效setNextAlarm(){
            Log.d(TAG,设置一个报警......);
            alarmMgr.set(AlarmManager.RTC_WAKEUP,(System.currentTimeMillis的()+频率)的PendingIntent);
        }    私人无效stopAlarms(){
            Log.d(TAG,停止报警);
            alarmMgr.cancel(的PendingIntent);
        }}


  3. 当服务被激发我得到MyApplication的实例,让AlarmManager和安排下一次警钟。

     公共类的MyService扩展服务{
        所有MyApplication MAPP;    @覆盖
        公众诠释onStartCommand(意向意图,诠释标志诠释startId){
            Log.d(TAG,在onStartCommand);
            //如果需要初始化应用参考
            如果(MAPP == NULL){
                Log.d(TAG,应用为null现在创建......);
                MAPP =(所有MyApplication)getApplicationContext();
            }        //安排下一个区检查
            mApp.getAlarmMgr()setNextAlarm()。       //自定义功能...
       }



I have an alarm that I am wanting to repeat around every 5 minutes. For testing purposes, I have it set to repeat once every 5 seconds instead, as such:

AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
        Intent intent = new Intent(getActivity(), CoordinateAlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getService(getActivity(), 1, intent, 0);
        int repeatSeconds = 5;
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                repeatSeconds * 1000, pendingIntent);

And the receiving IntentService prints a log statement when it receives the alarm. However, it fires around once every minute and a half instead of once every 5 seconds, where is it set incorrectly? I have also tried using setRepeating() instead of setInexactRepeating() but I get the same results.

Here is my alarm receiver:

public class CoordinateAlarmReceiver extends IntentService {

    public CoordinateAlarmReceiver(){
        super("CoordinateAlarmReceiver");
    }

    /*
    Alarm received, get new contacts which then shows notification
     */
    @Override
    protected void onHandleIntent(Intent intent) {
        MyLog.i("coordinate alarm received");

        //new GetNewContactsTask(this).execute();
    }
}

解决方案

I had a similar problem in which I needed a Service fired every 15 seconds... I did the following.

  1. I have a class that extends Application called MyApplication. This class holds an instance of an alarm manager.

    public class MyApplication extends Application {
        private MyAlarmManager alarmMgr;
    
        @Override
        public void onCreate() {
            Log.d(TAG, "MyApplication onCreate");
            super.onCreate();
            Log.d(TAG, "initing alarmMgr ...");
            alarmMgr = new MyAlarmManager(this);
        }
    
        public MyAlarmManager getAlarmManager(){
            return alarmMgr;
        }
    }
    

    1. An AlarmManager called MyAlarmManager creates, starts & stops the alarms AND NOW sets the next alarm for this one service.

      public class MyAlarmManager {
          private MyApplication mApp;
          private Intent intent;
          private PendingIntent pendingIntent;
          private AlarmManager alarmMgr;
          private static final long FREQUENCY = 15000;
      
          public MyAlarmManager(Context context) {
              mApp = (MyApplication) context;
              // Android alarm service
              alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      
              // service to be fired every 15 seconds
              intent = new Intent(context, MyService.class);
              pendingIntent = PendingIntent.getService(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
              setNextAlarm();
          }
      
          public void setNextAlarm(){
              Log.d(TAG, "setting next alarm...");
              alarmMgr.set(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + FREQUENCY), pendingIntent);
          } 
      
          private void stopAlarms(){
              Log.d(TAG, "stopping Alarms");
              alarmMgr.cancel(pendingIntent);
          }
      
      }
      

    2. When the Service is fired I get an instance of MyApplication, get the AlarmManager and schedule the next alarm.

      public class MyService extends Service {
          MyApplication mApp;
      
          @Override
          public int onStartCommand(Intent intent, int flags, int startId) {
              Log.d(TAG, "in onStartCommand");
              // init the app reference if needed
              if (mApp == null) {
                  Log.d(TAG, "app was null. Creating now...");
                  mApp = (MyApplication) getApplicationContext();
              }
      
              // schedule the next zone check
              mApp.getAlarmMgr().setNextAlarm();
      
             // custom functionality ... 
         }
      

这篇关于Android的重复报警器不能正确重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆