问题描述
我建立一个出租车预订应用程序,我需要每20秒的驾驶室当前位置。
我已经定义了一个AlarmManager并且需要它重演每20秒。但它不是经常重演。相反,它重演233秒后,并只有一次。我在做什么错在这里?
我的主屏幕上有一个内部类OnAlarmReceiver,在我的主屏幕我打电话AlarmManager的OnCreate
AlarmManager经理=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
意图I =新的意图(这一点,OnAlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent PI = PendingIntent.getBroadcast(此,0,I,PendingIntent.FLAG_CANCEL_CURRENT);
日历CAL = Calendar.getInstance();
cal.add(Calendar.SECOND,20);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
cal.getTimeInMillis(),God.UPDATE_PENDING_INTERVAL,PI);
内部类中的主屏幕
OnAlarmReceiver延伸的BroadcastReceiver {public类
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
// PullPendingRequests.acquireStaticLock(上下文);
Toast.makeText(背景下,不要panik但你的时间到了!!!!,Toast.LENGTH_LONG)
。显示();
Log.d(Taxeeta:PullPendingRequets,CallService位置);
context.startService(新意图(背景下,PullPendingRequests.class));
}
}
我AndroidManifest文件有
<服务
机器人:名称=com.taxeeta.support.PullPendingRequests
机器人:启用=真
机器人:标签=@字符串/ APP_NAME
机器人:screenOrientation =画像
机器人:主题=@安卓风格/ Theme.Light.NoTitleBar/>
<接收机器人:名称=。com.taxeeta.HomeScreen.OnAlarmReceiver/>
< /用途>
的亚行外壳dumpsys报警输出
com.taxeeta
51471ms运行,5248唤醒
5248报警:FLG =为0x4 CMP = com.taxeeta / .HomeScreen $ OnAlarmReceiver
对 输出| grep的taxeeta code> 亚行外壳dumpsys报警
ELAPSED_WAKEUP#7:报警{409303b0 2型com.taxeeta}
操作= PendingIntent {408ba2d8:PendingIntentRecord {40887be8 com.taxeeta broadcastIntent}}
com.taxeeta
5248报警:FLG =为0x4 CMP = com.taxeeta / .HomeScreen $ OnAlarmReceiver
要解决这个问题,我删除了内部类OnAlarmReceiver和固定AndroidManifest.xml文件。
<接收器
机器人:名称=com.taxeeta.support.OnAlarmReceiver
机器人:出口=真正的>
<意向滤光器>
<作用机器人:名称=android.intent.action.NOTIFY/>
&所述; /意图滤光器>
< /接收器>
I am building a cab booking app, I need current location of the cab every 20 seconds.
I have defined a AlarmManager and need it to repeat itself every 20 seconds. But its not repeating itself regularly. Instead it repeated itself after 233 seconds, and just once. What am I doing wrong here ?
My HomeScreen has a inner class OnAlarmReceiver, in the onCreate of my HomeScreen I am calling AlarmManager
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, OnAlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
cal.getTimeInMillis(), God.UPDATE_PENDING_INTERVAL, pi);
Inner class in HomeScreen
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// PullPendingRequests.acquireStaticLock(context);
Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG)
.show();
Log.d("Taxeeta:PullPendingRequets", "CallService Location");
context.startService(new Intent(context, PullPendingRequests.class));
}
}
My AndroidManifest file has
<service
android:name="com.taxeeta.support.PullPendingRequests"
android:enabled="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Light.NoTitleBar" />
<receiver android:name=".com.taxeeta.HomeScreen.OnAlarmReceiver" />
</application>
Output of adb shell dumpsys alarm
com.taxeeta
51471ms running, 5248 wakeups
5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver
Output of adb shell dumpsys alarm | grep taxeeta
ELAPSED_WAKEUP #7: Alarm{409303b0 type 2 com.taxeeta}
operation=PendingIntent{408ba2d8: PendingIntentRecord{40887be8 com.taxeeta broadcastIntent}}
com.taxeeta
5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver
To fix it, I removed the inner class OnAlarmReceiver and fixed the androidmanifest.xml file.
<receiver
android:name="com.taxeeta.support.OnAlarmReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
这篇关于的BroadcastReceiver的onReceiver不叫,AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!