问题
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
and 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();
}
}
Problem is the Service started
message will be displayed for the very first time when I click the radio button, but I want the Service started
message to be displayed after the 10 seconds
. Please someone help me to solve this problem, please share your knowledge so that I can correct my mistakes.
Thanks in advance.
回答1:
Set AlarmManager like this:
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);
Change AlarmManager.RTC
to AlarmManager.RTC_WAKEUP
if u want to wake up phone when it
goes off. More about AlarmManager here
Those two parameters also means that your alarm time will be System.currentTimeMilis() which is time in UTC.
EDIT :
Your solution using AlarmManager.ELAPSED_REALTIME
which measure time since device boot including sleep. It means that if you want run this code after 10 seconds and then want to repeat it and your device is running for more than that, PendingIntent will be triggered immediately because 10 seconds after boot occurs in the past.
EDIT 2 :
If u want to run code just once after 10 seconds try this:
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);
}
回答2:
try with this
alarmManagera.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),10000, YourPendingIntent);
回答3:
replace SystemClock.elapsedRealtime()
with System.currentTimeMillis()
回答4:
This code Snippet may help you
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);
In MyService class use onStartCommand(Intent intent, int flags, int startId)
. you have used onStart(Intent intent, int startId) which will execute once.
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;
}
}
来源:https://stackoverflow.com/questions/19020853/how-to-run-some-task-at-specified-time-interval-in-android-using-alarmmanager