2016-12-17 22 views
0

我在按鈕單擊時創建了警報管理器。但在電話重啓後無法使用。我的AlarmbroadcastReceiver不會在電話重啓時撥打電話。它工作時,電話鎖定,應用程序死亡,但不工作後,手機重新啓動我創建了一個進度條,開始點擊按鈕和警報廣播後開始停止,但不會停止時,手機重新啓動。我已經將我的按鈕單擊事件和廣播接收器類Alarmmanager在電話重啓之後不工作

按鈕單擊事件

b1.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
pb1.setVisibility(View.VISIBLE); 
progress_edit.putBoolean("progress_one", true); 
progress_edit.apply(); 
        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
        Intent intnt = new Intent(getApplicationContext(), AlarmbroadcastReceiver.class); 
        intnt.setAction("com.ex.Alarm"); 
        PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intnt, 0); 
        manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pending); 
           Log.d("Broadcast ","Fired"); 
       } 
      }); 

廣播接收器類

@Override 
     public void onReceive(Context context, Intent intent) { 
      Log.d("inside","broadcast receive"); 
      if(intent.getAction().equalsIgnoreCase("com.ex.Alarm")) 
      { 
enterSys_progress_edit.putBoolean("progress_one", false); 
       enterSys_progress_edit.apply(); 
       Toast.makeText(context,"Receive",Toast.LENGTH_LONG).show(); 
      } 

     } 

我的清單文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.example.krutarth.alarm"> 
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
     <application 
      android:allowBackup="true" 
      android:icon="@mipmap/ic_launcher" 
      android:label="@string/app_name" 
      android:supportsRtl="true" 
      android:theme="@style/AppTheme"> 
      <activity android:name=".MainActivity"> 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 
      </activity> 
      <receiver android:name=".AlarmbroadcastReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
       <intent-filter > 
        <action android:name="android.intent.action.BOOT_COMPLETED"/> 
       </intent-filter> 
      </receiver> 
     </application> 
    </manifest> 
+0

可能是重複的http://stackoverflow.com/questions/26296270/alarmmanager-not-trigger-after-the-phone-reset – sasikumar

回答

2

所有報警復位當手機重新啓動,所以在重新啓動時創建一個回調像這樣

public class BootCompletedIntentReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
     ////// reset your alrarms here 
    } 

} 

}

ALSO添加到您的清單中註冊廣播

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

創建方法說,

setMyalarm(){ 
    // here set the alarm as u need 
} 

現在調用這個方法setMyAlarm,隨時隨地u需要來設置特定的報警,無論是其上的按鈕點擊,或者是否在重新啓動reciever

+0

如何,我可以重置報警,我想阻止它基於sharedpreferences進度價值,所以我怎麼能改變他們我把布爾值內首選項。我有近32個不同的進度條。所以請幫助我 –

+0

只需調用通過這個接收器設置鬧鐘的方法,簡單! 。一旦你的報警被重置,你的代碼可以像以前一樣運行。並且爲你的sharedpreferences設置和重置請通過教程和文檔關於它 – Ak9637

+0

你可以提供一些演示,因爲我在按鈕點擊添加報警設置,所以請幫我 –

0

知府ANS從Ak9637 但沒忘了補充的

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

<使用權限android:name =「android.permission.RECEIVE_BOOT_COMPLETED」/> –

+0

請把這個在你的清單文件的權限部分 –