Android:调度应用程序以重复警报无效

时间:2012-10-07 09:34:38

标签: android alarmmanager

我让我的广播接收器设置定期闹钟,以启动服务。不幸的是,这不会导致重复调用服务(基于logcat)。我也尝试了时间间隔的不同值。有人可以帮忙吗? (我在Android 3.2 Motorola xoom上通过Eclipse进行测试)

以下是广播接收器的代码。

    alarm = (AlarmManager) arg0.getSystemService(Context.ALARM_SERVICE);
    Intent intentUploadService = new Intent (arg0, com.vikramdhunta.UploaderService.class);

    Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 3);

    PendingIntent pi = PendingIntent.getBroadcast(arg0, 0, intentUploadService , 0);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5, pi);

以下是服务类的代码

    public UploaderService()
    {
        super("UploaderService");
        mycounterid = globalcounter++;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        synchronized(this)
        {
            try
            {
                for (int i = 1;i < 5;i++)
                {
// doesn't do much right now.. but this should appear in logcat
                    Log.i(TAG,"OK " + globalcounter++ + " uploading..." + System.currentTimeMillis());

                }
            }
            catch(Exception e)
            {

            }
        }   
    }
     @Override
        public void onCreate() {
            super.onCreate();
            Log.d("TAG", "Service created.");
        }


        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        Log.i(TAG, "Starting upload service..." + mycounterid);
        return super.onStartCommand(intent,flags,startId);
    }

1 个答案:

答案 0 :(得分:2)

好的,好像我得到了这个。需要做出两项改变 -
1 - 我在使用PendingIntent.getBroadcast时犯了一个错误,当我需要做getService时(地狱,谁知道!)
2 - 在getService中,我应该在最后提供PendingIntent.FLAG_UPDATE_CURRENT而不是0. FLAG_ONE_SHOT不起作用。我猜因为间隔只有5秒,这是正确的方法。

现在我每隔5秒钟就会调用一次正确的服务/功能。乌拉!

相关问题