0

您好stackoverflow我正在尝试开发一个可以在特定时间间隔运行某些任务的android应用程序,我正在使用AlarmManager它来执行任务,代码片段如下,

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

4 回答 4

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.RTCAlarmManager.RTC_WAKEUP如果您想在手机关机时唤醒手机。更多关于 AlarmManager 的信息在这里

这两个参数也意味着您的闹钟时间将是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);
}
于 2013-09-26T07:03:10.037 回答
0

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

于 2013-09-26T08:21:03.063 回答
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;
    }
}
于 2013-09-26T07:04:53.423 回答
0

试试这个

alarmManagera.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),10000, YourPendingIntent);
于 2013-09-26T06:32:48.640 回答