如何使用AlarmManager运行在指定的时间间隔有些任务在android系统?间隔、如何使用、任务、时间

2023-09-07 03:01:20 作者:中国逗比代表

您好计算器我试图开发可运行在特定的时间间隔某些任务的Andr​​oid应用程序,我使用 AlarmManager 做任务时,code片断如下,

Hello stackoverflow I'm trying to develop an android application that can run some task at specific time interval, I'm using AlarmManager to do the task, the code snippet is as follows,

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

问题是服务启动信息将被显示在第一次当我点击单选按钮,但我想在服务启动消息