2016-12-17 52 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

请把这个在你的清单文件的权限部分 –