2016-07-14 35 views
1

我正在製作一個應用程序,您可以在其中設置鬧鐘。 例如,我在七點鐘的星期一上課,所以我需要在每個星期一開始鬧鐘,但星期二我還要上課,並且必須這樣做。重新啓動後的android alarmmanager警報2016

我已經可以做到這一點,並與每個curso的警報,但當我重新啓動手機,然後他們不工作。

這裏是我的代碼投放額外在我的應用程序,所以重要的是:

Calendar cal = Calendar.getInstance(); 
    cal.setTimeInMillis(System.currentTimeMillis()); 
    cal.set(Calendar.HOUR_OF_DAY, horai); 
    cal.set(Calendar.MINUTE, minutoi); 
    cal.set(Calendar.DAY_OF_WEEK, dias.getSelectedItemPosition() + 1); 
    cal.add(Calendar.SECOND, 2); 



    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); 
    intent.putExtra("name", curso); 
    //intent.putExtra("curos bn",1); 
    PendingIntent pendingIntent = 
      PendingIntent.getBroadcast(getBaseContext(), 
      cont, intent, PendingIntent.FLAG_UPDATE_CURRENT);//cont star with 1 a then ++ 

接收機

public class AlarmReceiver extends BroadcastReceiver { 

private static final String TAG = "BANANEALARM"; 
Intent intent; 
PendingIntent pendingIntent; 
NotificationManager notificationManager; 
private static final int PERIOD=5000; 
@Override 
public void onReceive(Context context, Intent intent) { 



    Log.i(TAG, "BroadcastReceiver has received alarm intent."); 
    Intent service1 = new Intent(context, AlarmService.class); 
    String id = intent.getStringExtra("name"); // this is so important 
    service1.putExtra("name",id); 
    context.startService(service1); 

}

Manifets

<receiver android:name=".AlarmReceiver" android:enabled="true" > 

    </receiver> 
    <service android:name=".AlarmService" /> 

因此,在我的應用程序的其他部分,我使用從json獲取的數據設置了警報。 所有作品像我想要唯一的問題是當我重新啓動手機?

回答

3

根據developer.google.com

註冊,而該設備處於睡眠狀態報警被保留(和可以選擇喚醒設備,如果他們在這段時間內熄滅),但將被清除,如果它被關閉並重新啓動。

因此,你需要創建另一個接收器,將重建所有的報警。此接收器不是您的AlarmReceiver,它不處理激活的警報。它僅在重新啓動後用於重置它們。

要做到這一點,你需要這些代碼行:

AndroidManifest.xml中

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

<application 

    <!-- rest of the code --> 

    <receiver android:name=".AlarmBroadcastReceiver"/> 

    <service android:name=".BootService"/> 

    <receiver android:name=".RestartAlarmsReceiver" android:enabled="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 

RestartAlarmsReceiver.java

public class RestartAlarmsReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 

      // It is better to reset alarms using Background IntentService 
      Intent i = new Intent(context, BootService.class); 
      ComponentName service = context.startService(i); 

      if (null == service) { 
       // something really wrong here 
       Log.e(TAG, "Could not start service "); 
      } 
      else { 
       Log.e(TAG, "Successfully started service "); 
      } 

     } else { 
     Log.e(TAG, "Received unexpected intent " + intent.toString()); 
     } 
    } 
} 

BootService.java

public class BootService extends IntentService { 

    public BootService() { 
     super("BootService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     // Your code to reset alarms. 
     // All of these will be done in Background and will not affect 
     // on phone's performance 

    } 
} 
+0

好的謝謝,但在每個警報意圖我設置了一個額外的「intent.putExtra(」name「,curso);」它只是一個字符串,但是每個課程的名稱,我需要它 – fer

+0

它沒有問題,你可以把任何數量的額外的你的意圖如果你覺得這個答案有幫助,請接受和Upvote它,我會很感激:) – Marat

0

你可以聽事件重啓,然後做你想做的時候,手機重啓

在您的清單

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

... 

<receiver android:name=".YourBootReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    </intent-filter> 
</receiver> 

Java代碼

public class YourBootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //do what you want 
    } 
} 
+0

這個我必須創建一個新的接收器???或者我應該修改我的 fer

+0

public void onReceive(Context context,Intent intent){ //做你想要的 }在那部分我需要從額外的數據我怎麼能得到它???????? – fer

+0

YourBootReceiver是新的接收器,接收事件手機重新啓動和onReceive發生時,手機重新啓動完成,在這個函數中,你可以設置警報再次。我不知道你需要什麼數據到達那裏? – Fuyuba

0

內核觸發報警,當你警報超時,但重啓後無法保存警報狀態。所以如果用戶重啓設備,你應該重置你的鬧鐘。

Android開發者給出的答案here