2013-04-08 72 views
6

我正在搭建一個出租車預訂應用程序,我需要每20秒鐘在駕駛室的當前位置。沒有調用BroadcastReceiver的onReceiver,AlarmManager

我定義了一個AlarmManager,需要它每20秒重複一次。但它不會定期重複。相反,它在233秒後重復了自己,只是一次。我在這裏做錯了什麼?

我的主屏幕有一個內部類OnAlarmReceiver,我在主屏幕的OnCreate我打電話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); 
在主屏幕

內部類

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

我AndroidManifest文件有

<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> 

adb shell dumpsys alarm

的輸出
com.taxeeta 
    51471ms running, 5248 wakeups 
    5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver 

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 

回答

10

要修復它,我刪除了內部類OnAlarmReceiver並修復了androidmanifest.xml文件。

<receiver 
     android:name="com.taxeeta.support.OnAlarmReceiver" 
     android:exported="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.NOTIFY" /> 
     </intent-filter> 
    </receiver> 
0

輸出這段代碼爲我工作,並確保你已經在Android清單文件中添加reciever。

AlarmManager service = (AlarmManager) context 
      .getSystemService(Context.ALARM_SERVICE); 
Intent i = new Intent(context, OnAlarmReceiver.class); 
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, 
         PendingIntent.FLAG_CANCEL_CURRENT); 
Calendar cal = Calendar.getInstance(); 
// Start 20 seconds after boot completed 
cal.add(Calendar.SECOND, 20); 
// 
// Fetch every 20 seconds 
// InexactRepeating allows Android to optimize the energy consumption 
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
         cal.getTimeInMillis(), 1000*20, pending); 
+1

不,不起作用。 OnAlarmReceiver,onReceive也沒有被調用。 – Siddharth 2013-04-08 10:04:42

+0

同樣在這裏alarmmanager什麼都不適合我。我已經爲此嘗試了所有建議。不知道該怎麼做相當卡住 – JPM 2016-11-08 21:39:34

0

如果回答上述方法不爲你工作那麼還有另一種方式來收到任何回調時AlarmManager火災過期報警。你只需要在PendingIntent的實例化上發送錯誤的Intent來檢查這一個:。例如,您希望在您的某個接收器上接到呼叫onReceive,但您通過getActivitygetService實例化了PendingIntent,但實際上您的意思是getReceiver

當創建的PendingIntent例如,有許多方法來創建它(getServicegetActivitygetReceivergetForegroundService

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 

,如果:如果你要Activity意圖的接收器,那麼你

你想BroadcastReceiver接收者的意圖:

PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*); 

如果你想有一個前景Service意圖的接收器:

PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*); 

如果你想有一個Service意圖的接收器:

PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*); 

此外,請確保您的意圖均指向正確的類。 (例如創建活動,服務等的意圖)。

Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver 

// PendingIntent was created in such a way 
// you wanted this to be received by an activity. 
// you will not receive any call if you set it up like this. 
PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 

我還張貼類似的回答here:如果你通過不正當這樣你會不會收到任何呼叫。

HTH

相關問題