问题
Hai am trying to set alarm for every 10 minutes.But its running first time only any Body kindly help me
Intent intent = new Intent(this, ConnectionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();//every 10 minutes i want to print the toast
回答1:
use alarmManager.setRepeating method
follow this link on developer site
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmService.html
http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)
回答2:
Calendar calCurrent = Calendar.getInstance();
long tenmin = 10 * 60 * 1000;
int mynotifyidis = Integer.parseInt(mydelid);
Intent mIntent = new Intent(this, NotificationService1.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mIntent.putExtra("id", mynotifyidis);
AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent snoozependingintent = PendingIntent.getService(this,
-mynotifyidis, mIntent, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calCurrent.getTimeInMillis() + fivemin, fivemin,
snoozependingintent);
来源:https://stackoverflow.com/questions/8986964/how-to-set-alarm-for-every-10-minutes-in-android-application