如何使用AlarmManager在android中以指定的时间间隔运行某些任务?

时间:2013-09-26 06:25:52

标签: android alarmmanager

Hello stackoverflow我正在尝试开发一个可以在特定时间间隔运行某个任务的Android应用程序,我正在使用AlarmManager来完成任务,代码片段如下,< / p>

if (radioBtnChecked)
{   
     MyActivity.this.alarmMgr = (AlarmManager) MyActivity.this.getSystemService(Context.ALARM_SERVICE);
     Intent serviceIntent = new Intent(MyActivity.this, MyService.class);
     MyActivity.this.pi = PendingIntent.getService(MyActivity.this, 0, serviceIntent, 0);
     MyActivity.this.alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, pi);

 }//End of if condition

MyService.java

public class MyService extends Service 
{
    @Override
    public IBinder onBind(Intent intent) 
    {
        return null;
    }//End of onBind method

    public void onStart(Intent intent, int startId) 
    {
        super.onStart(intent, startId);
        Toast.makeText(getApplicationContext(),"Service started", Toast.LENGTH_SHORT).show();
    }
}

问题是,当我单击单选按钮时,第一次显示Service started消息,但我希望在Service started之后显示10 seconds消息。请有人帮我解决这个问题,请分享你的知识,以便纠正我的错误。

提前致谢。

4 个答案:

答案 0 :(得分:5)

像这样设置AlarmManager:

private static final int REPEAT_TIME_IN_SECONDS = 60; //repeat every 60 seconds

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
            REPEAT_TIME_IN_SECONDS * 1000, pendingIntent);

如果您想要唤醒手机,请将AlarmManager.RTC更改为AlarmManager.RTC_WAKEUP  熄灭。有关AlarmManager here

的更多信息

这两个参数也意味着您的闹钟时间将是System.currentTimeMilis(),这是UTC的时间。

编辑:

您的解决方案使用AlarmManager.ELAPSED_REALTIME来衡量自设备启动以来的时间,包括睡眠。这意味着如果您希望在10秒后运行此代码,然后想要重复此代码并且您的设备运行的时间超过此值,则 PendingIntent将立即触发,因为过去启动后10秒。

编辑2:

如果您想在10秒后运行一次代码,请尝试以下操作:

private static final int START_AFTER_SECONDS = 10;
...
if (radioBtnChecked)
{ 
    Runnable mRunnable;
    Handler mHandler = new Handler();
    mRunnable = new Runnable() {
        @Override
        public void run() {
            Intent serviceIntent = new Intent(MyActivity.this, MyService.class);
            MyActivity.this.startService(serviceIntent);
        }
    };
    mHandler.postDelayed(mRunnable, START_AFTER_SECONDS * 1000);
}

答案 1 :(得分:0)

试试这个

alarmManagera.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),10000, YourPendingIntent);

答案 2 :(得分:0)

此代码段可能会帮助您

 private static final long REPEAT_TIME = 30*1000;  
  AlarmManager service = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
            Intent i = new Intent(context, MyStartServiceReceiver.class);
            PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
                PendingIntent.FLAG_CANCEL_CURRENT);
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.SECOND, 30);
            service.setInexactRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), REPEAT_TIME, pending);

在MyService类中使用onStartCommand(Intent intent, int flags, int startId)。你已经使用了onStart(Intent intent,int startId),它将执行一次。

public class MyService extends Service 
{
    @Override
    public IBinder onBind(Intent intent) 
    {
        return null;
    }//End of onBind method

    public void onStart(Intent intent, int startId) 
    {
        super.onStart(intent, startId);
        Toast.makeText(getApplicationContext(),"Service started", Toast.LENGTH_SHORT).show();
    }

@Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
         Toast.makeText(getApplicationContext(),"onStartCommand=Service started", Toast.LENGTH_SHORT).show();        
        return Service.START_STICKY;
    }
}

答案 3 :(得分:0)

SystemClock.elapsedRealtime()替换为System.currentTimeMillis()