问题描述
我安排的PendingIntent,以在5秒内被发送,但23秒开始。 API = 19(AlarmManager.setExact(...)):
16 10-23:43:44.638 11903-11903 /? D / MainActivity:周五10月23日十六时43分44秒GMT + 05:00 2015年:计划在5000毫秒
10-23 16:44:07.728 11903-11903 /? D / MainActivity:周五10月23日16时44分07秒GMT + 05:00 2015:解雇私人的PendingIntent的PendingIntent;
私人诠释SCHEDULE_REQUEST_ code = 1;
私有静态最后弦乐SCHEDULER_ACTION =android.intent.action.FOTA_SCHEDULE_CHECK; 私人无效日志(字符串消息){
StringBuilder的SB =新的StringBuilder();
(。新的日期()的toString())sb.append;
sb.append(:);
sb.append(消息);
sb.append(\\ n); Log.d(的getClass()getSimpleName(),sb.toString());
tv.append(sb.toString()); tv.append(\\ n);
} 私人广播接收器schedulerReceiver =新的广播接收器(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
日志(炒鱿鱼);
}
}; 私人无效时间表(){
registerReceiver(schedulerReceiver,新的IntentFilter(SCHEDULER_ACTION)); 意图scheduleIntent =新意图(SCHEDULER_ACTION);
的PendingIntent = PendingIntent.getBroadcast(这一点,SCHEDULE_REQUEST_ code,scheduleIntent,0); AlarmManager alarmManager =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE); //第一次调用(一次,确切)
INT延迟= 5 * 1000; // 5S
日志(预定+延迟+毫秒);
alarmManager.setExact(AlarmManager.RTC,System.currentTimeMillis的()+延迟的PendingIntent);
} @覆盖
保护无效的onDestroy(){
super.onDestroy(); 取消调度();
} 私人无效取消调度(){
AlarmManager alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(的PendingIntent); 的PendingIntent = NULL; unregisterReceiver(schedulerReceiver);
}
我心中已经得到了同样的问题,但的PendingIntent 5秒后开始,而不是2,因为我尝试设置。
alarmManager.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()+ 2 * 1000,pIntent);
如果我设置在5秒多, alarmManager
开始完全吻合。我不知道是什么inexatly时间我会在生产环境中得到的,所以我选1纪录如短的时间间隔。
所以,用我的应用程序 postDelayed()
而不是 AlarmManager
在很短的时间间隔,而 AlarmManager
长。
code样品:
如果(延迟== 0){
意向意图=新意图(背景下,SoundService.class);
intent.putExtra(SoundService.SOUND,声音);
context.startService(意向);
}否则如果(延迟< = 60){
(新的处理程序())。postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
意向意图=新意图(背景下,SoundService.class);
intent.putExtra(SoundService.SOUND,声音);
context.startService(意向);
}
},延迟* 1000);
}否则如果(延迟→60){
最终诠释SDK_INT = Build.VERSION.SDK_INT;
意向意图=新意图(背景下,AlarmBroadcastReceiver.class);
intent.setAction(action_play:+声音);
intent.putExtra(SoundService.SOUND,声音);
的PendingIntent pIntent = PendingIntent.getBroadcast(上下文,0,意图,0);
AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
长运行时间= System.currentTimeMillis的()+延迟* 1000;
如果(SDK_INT< Build.VERSION_ codeS.KITKAT){
alarmManager.set(AlarmManager.RTC_WAKEUP,运行时pIntent);
}否则如果((SDK_INT> = Build.VERSION_ codeS.KITKAT)及及(SDK_INT< Build.VERSION_ codeS.M)){
alarmManager.setExact(AlarmManager.RTC_WAKEUP,运行时pIntent);
}否则如果(SDK_INT> = Build.VERSION_ codeS.M){
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,运行时pIntent);
}
}
其中, SoundService
- 服务与完善的功能发挥,延迟
- 播放前的延迟,以秒为单位, AlarmBroadcastReceiver
-
公共类AlarmBroadcastReceiver扩展广播接收器{
最后弦乐LOG_TAG = this.getClass()的getName()。@覆盖
公共无效的onReceive(上下文CTX,意图意图){
如果(intent.getAction()startsWith(action_play:)){
意图playIntent =新意图(CTX,SoundService.class);
playIntent.putExtra(SoundService.SOUND,intent.getStringExtra(SoundService.SOUND));
ctx.startService(playIntent);
}
}
}
I schedule pendingIntent to be sent in 5 seconds, but it starts in 23 seconds. API=19 (AlarmManager.setExact(...)):
10-23 16:43:44.638 11903-11903/? D/MainActivity﹕ Fri Oct 23 16:43:44 GMT+05:00 2015: scheduled in 5000 ms
10-23 16:44:07.728 11903-11903/? D/MainActivity﹕ Fri Oct 23 16:44:07 GMT+05:00 2015: fired
private PendingIntent pendingIntent;
private int SCHEDULE_REQUEST_CODE = 1;
private static final String SCHEDULER_ACTION = "android.intent.action.FOTA_SCHEDULE_CHECK";
private void log(String message) {
StringBuilder sb = new StringBuilder();
sb.append(new Date().toString());
sb.append(": ");
sb.append(message);
sb.append("\n");
Log.d(getClass().getSimpleName(), sb.toString());
tv.append(sb.toString());
tv.append("\n");
}
private BroadcastReceiver schedulerReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
log("fired");
}
};
private void schedule() {
registerReceiver(schedulerReceiver, new IntentFilter(SCHEDULER_ACTION));
Intent scheduleIntent = new Intent(SCHEDULER_ACTION);
pendingIntent = PendingIntent.getBroadcast(this, SCHEDULE_REQUEST_CODE, scheduleIntent, 0);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// first invocation (once, exact)
int delay = 5 * 1000; // 5s
log("scheduled in " + delay + " ms");
alarmManager.setExact(AlarmManager.RTC, System.currentTimeMillis() + delay, pendingIntent);
}
@Override
protected void onDestroy() {
super.onDestroy();
unschedule();
}
private void unschedule() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
pendingIntent = null;
unregisterReceiver(schedulerReceiver);
}
I'v got the same problem, but pendingIntent start after 5 seconds, not 2 as i try to set.
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 2 * 1000, pIntent);
If i set 5 seconds and more, alarmManager
starts exactly. I don't know what inexatly interval i'll get in production environment, so i choose 1 minute as short time interval.
So, my app used postDelayed()
instead of AlarmManager
for the short time intervals, and AlarmManager
for long.
Code sample:
if (delay == 0) {
Intent intent = new Intent(context, SoundService.class);
intent.putExtra(SoundService.SOUND, sound);
context.startService(intent);
} else if (delay <= 60) {
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(context, SoundService.class);
intent.putExtra(SoundService.SOUND, sound);
context.startService(intent);
}
}, delay * 1000);
} else if (delay > 60) {
final int SDK_INT = Build.VERSION.SDK_INT;
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
intent.setAction("action_play: " + sound);
intent.putExtra(SoundService.SOUND, sound);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long runTime = System.currentTimeMillis() + delay * 1000;
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, runTime, pIntent);
} else if ((SDK_INT >= Build.VERSION_CODES.KITKAT) && (SDK_INT < Build.VERSION_CODES.M)) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, runTime, pIntent);
} else if (SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, runTime, pIntent);
}
}
Where SoundService
- Service with sound playing functionality,delay
- delay before playing, in seconds,AlarmBroadcastReceiver
-
public class AlarmBroadcastReceiver extends BroadcastReceiver {
final String LOG_TAG = this.getClass().getName();
@Override
public void onReceive(Context ctx, Intent intent) {
if (intent.getAction().startsWith("action_play:")){
Intent playIntent = new Intent(ctx, SoundService.class);
playIntent.putExtra(SoundService.SOUND, intent.getStringExtra(SoundService.SOUND));
ctx.startService(playIntent);
}
}
}
这篇关于AlarmManager.setExact()在错误的时间开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!