2013-10-12 184 views
0

我正在做一個示例報警應用程序。廣播未達到接收器方法

設定的時間後,廣播設置但接收方法不會被調用的時候

ReminderMainActivity:

public class ReminderMainActivity extends Activity implements OnClickListener, TimePicker.OnTimeChangedListener             { 

    DatePicker   _datePicker   = null; 
    TimePicker   _timePicker   = null; 
    EditText   _taskEditText  = null; 
    Button    _setButton   = null; 
    String    _st_time   = null; 
    String    _st_date   = null; 
    Calendar   _calendar   = null; 
    Calendar   _calendarNow  = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.reminder_view); 
     init(); 
    } 


    private void init() { 
     // TODO Auto-generated method stub 
     _datePicker = (DatePicker) findViewById(R.id.date_picker); 
     _timePicker = (TimePicker) findViewById(R.id.time_picker); 
     _taskEditText = (EditText) findViewById(R.id.task_desc); 
     _setButton = (Button) findViewById(R.id.set_button); 
     _calendarNow = Calendar.getInstance(); 
     _calendar = (Calendar) _calendarNow.clone(); 

     _timePicker.setOnTimeChangedListener(this); 
     _setButton.setOnClickListener(this); 


    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     String st_taskDescription = _taskEditText.getText().toString(); 
     int year = _datePicker.getYear(); 
     int monthOfYear = _datePicker.getMonth(); 
     int dayOfMonth = _datePicker.getDayOfMonth(); 


     _calendar.set(Calendar.YEAR, year); 
     _calendar.set(Calendar.MONTH, monthOfYear); 
     _calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
     _calendar.set(Calendar.SECOND, 0); 
     _calendar.set(Calendar.MILLISECOND, 0); 

     Log.i("", "Alarm Set: " + _calendar.getTimeInMillis()); 
     if(_calendar.compareTo(_calendarNow) <= 0){ 
       //Today Set time passed, count to tomorrow 
       _calendar.add(Calendar.DATE, 1); 
       Log.i("", "Time passed : " + _calendar.getTimeInMillis()); 
       Toast.makeText(this, "Time passed" + _calendar.getTimeInMillis(), Toast.LENGTH_SHORT).show(); 

       } 



     Intent intent = new Intent(this,AlarmReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(ReminderMainActivity.this, 0, intent, 0); 
     AlarmManager alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE);   
     alarm_manager.set(AlarmManager.RTC_WAKEUP, _calendar.getTimeInMillis(), pendingIntent); 
     Toast.makeText(this, "Alarm Set: " + _calendar.getTimeInMillis(), Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 
     // TODO Auto-generated method stub 
     _calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); 
     _calendar.set(Calendar.MINUTE, minute); 
     _st_time = hourOfDay + " : " + minute; 
    } 

} 

Receiver類

public class AlarmReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     // TODO Auto-generated method stub 
     Log.i("AlarmActivity", "Receiver"); 
     Toast.makeText(arg0, "Intent Received", Toast.LENGTH_SHORT).show(); 
    } 

} 

如果我更換_calendar.getTimeInMillis() System.CurrentTimeInMillis()然後它的工作..但這不是我想要的

接收器類也在manifest中提及。 我無法找到錯誤。誰能幫忙?有什麼我錯過了嗎?

+0

您的'_calendar'可能在過去,具體取決於您使用'set()'的值。除此之外,使用**'adb shell dumpsys alarm' **來檢查你的預定警報。 – CommonsWare

+0

我不明白如何使用第二個?你能否清楚 – Sjk

+0

打開命令提示符,然後輸入**'adb shell dumpsys alarm' **。如果**'adb' **在您的路徑中,則會從您的設備或模擬器轉儲與警報相關的系統信息。 http://developer.android.com/tools/help/adb.html#othershell命令 – CommonsWare

回答

0

得到了解決辦法。得到日曆的實例後,清除值。

_calendarNow = Calendar.getInstance(); 
_calendar = (Calendar) _calendarNow.clone(); 
_calendar.clear();