2017-02-02 77 views
1

我有一個使用AlarmManager服務安排一些活動的應用程序。它大部分時間工作,但有時它不會發送廣播,因此廣播接收器不會收到任何東西(即使應用程序已啓動並正在運行並且是前端活動)。我從TimePicker小部件讀取鬧鐘時間。 這裏是我的代碼片段:Android:有時不會觸發鬧鐘

設置報警:

private void setAlarm(int hour, int minute) { 
    //define intent and parameters 
    Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); 
    intent.putExtra(TAG_NAME, name); 
    intent.putExtra(TAG_ADDRESS, address); 
    intent.putExtra(TAG_AVERAGE, average); 
    intent.putExtra(TAG_DATABASE_ID, dbID); 
    //define alarm pedning intent 
    PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(),(int)dbID, 
      intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    //calculate alarm time 
    Calendar currentTimeCalendar = Calendar.getInstance(); 


    currentTimeCalendar.set(Calendar.HOUR_OF_DAY, hour); 
    currentTimeCalendar.set(Calendar.MINUTE, minute); 
    currentTimeCalendar.set(Calendar.SECOND, 0); 

    int h = currentTimeCalendar.get(Calendar.HOUR_OF_DAY); 
    int m = currentTimeCalendar.get(Calendar.MINUTE); 
    int s= currentTimeCalendar.get(Calendar.SECOND); 
    Toast.makeText(getApplicationContext(), "Hour "+h+":"+m+":"+s, Toast.LENGTH_LONG).show(); 


    SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MMM-yyyy hh:mm:ss"); 
    String date = dateFormat.format(currentTimeCalendar.getTime()); 
    Log.v("VSB_TAG","Alarm Time: "+date); 
    // set alarm 
    AlarmManager alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    alarmMgr.set(AlarmManager.RTC_WAKEUP, currentTimeCalendar.getTimeInMillis(), alarmIntent); 

} 

廣播接收器:

public class AlarmReceiver extends BroadcastReceiver { 
    public AlarmReceiver() { 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle bundle = intent.getExtras(); 
     Log.v("VHB_TAG", "Alarm Received"); 
     if (bundle == null) 
      return; 
     String name = bundle.getString(StudentDetailsActivity.TAG_NAME); 
     String address = bundle.getString(StudentDetailsActivity.TAG_ADDRESS); 
     double average = bundle.getDouble(StudentDetailsActivity.TAG_AVERAGE); 
     long dbID = bundle.getLong(StudentDetailsActivity.TAG_DATABASE_ID); 
     Toast.makeText(context, "Alarm for " + name, Toast.LENGTH_LONG).show(); 

     displayStudentNotification(context, name, address, dbID,bundle); 

    } 

    private void displayStudentNotification(Context context, String name, String address, long dbID, Bundle bundle) { 
     .... 
     .... 
    } 

} 

這裏是清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="net.evegroup.studentslist"> 

    <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> 
     <activity android:name=".StudentDetailsActivity" /> 

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

</manifest> 

回答

1

由於您的問題是間歇性的,因此問題可能不在於您顯示的代碼中。

如果設置了警報,它會觸發,因此您需要檢查您是否正確設置了警報或預期的警報。

您也可以用稍後設置的另一個覆蓋警報。

+0

所以你說我發佈的代碼的整體邏輯似乎很好? – VSB

+0

我沒有檢查過它,但是你說代碼有時候工作,所以你的問題更可能是代碼的使用,而不是代碼本身。 – Kuffs