2014-09-03 80 views
0

我有報警管理器的問題。我創建了每15秒重複顯示烤麪包的鬧鐘管理器。重新啓動後報警管理器不重複

重新啓動我的設備後,烤麪包可見,但只有一次。即使在重新啓動後,我也希望每15秒再重複一遍。

我可以添加什麼來解決這個問題?這可能嗎?

這裏是我的代碼(類AlarmReceiver擴展廣播接收器):

@Override 
public void onReceive(Context context, Intent intent) { 
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG"); 
    //Acquire the lock 
    wl.acquire(); 

    Toast.makeText(context, "wow", Toast.LENGTH_LONG).show(); 

    //Release the lock 
    wl.release(); 

} 

public void SetAlarm(Context context) 
{ 
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(context, AlarmReceiver.class); 
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); 
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15000, pi); 
} 

而且我的AndroidManifest.xml

<receiver android:name=".view.activity.AlarmReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
     </intent-filter> 
    </receiver> 

編輯:這個問題的解決是onReceiver()編輯代碼:

@Override 
public void onReceive(Context context, Intent intent) { 
    if(intent.getAction()==null){ 
     Toast.makeText(context, "lol", Toast.LENGTH_LONG).show(); 

    } else 
    { 
     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent intent1 = new Intent(context, AlarmReceiver.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent1, 0); 
     am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15000, pi); 
    } 

} 
+1

由於您的報警意圖是顯式調用接收器,你不需要自定義操作。只需檢查'onReceive()'中的Intent操作是否爲空。如果是,請顯示吐司;否則請調用您的方法來設置鬧鐘。 – 2014-09-03 18:29:27

+0

這是完美的工作!謝謝! – DivinaProportio 2014-09-03 18:47:13

回答

1

看來你只需要在onReceive中調用你的SetAlarm函數方法,並監聽清單中發送的事件。

在您的清單

<intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
    //New 
    <action android:name="com.packagename.custombroadcast" /> 
</intent-filter> 

當你的意圖

Intent intent = new Intent(); 
intent.setAction("com.packagename.custombroadcast"); 
//Use Context.sendBroadcast 
sendBroadcast(intent); 
+0

好吧,我想過了,但我希望可能會有更好的解決方案來實現這一點。謝謝你的答案 ;-) – DivinaProportio 2014-09-03 18:13:36