2013-04-26 57 views
0

我加入報警我的應用程序設置。當用戶使用timepicker設置預約時間時,我想警告用戶。如果我設置報警,只需要最後報警我設置,我多麼想使這兩個方面的工作?..報警使用timepicker不工作,機器人

沒有錯誤,我在Manefest已經註冊。其實我想獲取日期和時間設置sqlite的,報警是基於日期和time.i'm卡住,首先我要用戶添加約會,讓說25/4/2013下午4:30上..數據保存在sqlite中。那麼當天,我想要應用程序,以警告用戶..我怎麼能這樣做?我知道使用報警管理器,但我不知道從哪裏代碼

開始,爲alarmReceiver我希望類能夠提醒用戶也許使用警報或吐司或通知。但我已嘗試警告和吐司不能使用。有沒有其他方法? 請幫我 下面是我的代碼:

public class AppointmentAdd extends Activity 
{ 

private SQLiteDatabase database; 
private DBHelper helper; 
private ScheduleClient scheduleClient; 
Button btnSave, btnDate, btnTime; 
EditText addPurpose; 

DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance(); 
Calendar myCalendar = Calendar.getInstance(); 

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() { 
public void onDateSet(DatePicker view, int year, int monthOfYear, 
     int dayOfMonth) { 
    myCalendar.set(Calendar.YEAR, year); 
    myCalendar.set(Calendar.MONTH, monthOfYear); 
    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
    updateLabelDate(year, monthOfYear, dayOfMonth); 
} 
}; 

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() { 
public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
    myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay); 
    myCalendar.set(Calendar.MINUTE, minute); 
    updateLabelTime(hourOfDay, minute); 
} 
}; 

private void updateLabelDate(int year, int monthOfYear, 
     int dayOfMonth) { 

    year = myCalendar.get(Calendar.YEAR); 
    monthOfYear = myCalendar.get(Calendar.MONTH); 
    dayOfMonth = myCalendar.get(Calendar.DATE); 

    btnDate.setText(new StringBuilder().append(dayOfMonth).append(".") 
      .append(monthOfYear + 1).append(".").append(year).append(" ")); 
} 

private void updateLabelTime(int hourOfDay, int minute) { 

    hourOfDay = myCalendar.get(Calendar.HOUR_OF_DAY); 
    minute = myCalendar.get(Calendar.MINUTE); 
    btnTime.setText(new StringBuilder().append(hourOfDay).append(":") 
      .append(minute)); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.appointment_add); 

    btnSave = (Button)findViewById(R.id.button3); 
    btnDate = (Button)findViewById(R.id.button1); 
    btnTime = (Button)findViewById(R.id.button2); 
    addPurpose = (EditText)findViewById(R.id.editText1); 

    scheduleClient = new ScheduleClient(this); 
    scheduleClient.doBindService(); 

    btnDate.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      new DatePickerDialog(AppointmentAdd.this, d, myCalendar 
        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), 
        myCalendar.get(Calendar.DAY_OF_MONTH)).show(); 
     } 
    }); 

    btnTime.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      new TimePickerDialog(AppointmentAdd.this, t, myCalendar 
        .get(Calendar.HOUR_OF_DAY), myCalendar 
        .get(Calendar.MINUTE), true).show(); 
     } 

    }); 


    helper = new DBHelper(this); 
    database = helper.getWritableDatabase(); 

    btnSave.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      String purpose = addPurpose.getText().toString(); 
      String date = btnDate.getText().toString(); 
      String time = btnTime.getText().toString(); 

      helper.insertDataAppointment(database, date, time, purpose); 
      Toast.makeText(AppointmentAdd.this, "Successfully Add", Toast.LENGTH_SHORT).show(); 

      setAlarm(myCalendar.get(Calendar.HOUR_OF_DAY), myCalendar.get(Calendar.MINUTE)); 

      Intent i = new Intent(AppointmentAdd.this, AppointmentView.class); 
      startActivity(i); 
     } 
    }); 

    updateLabelDate(0, 0, 0); 
    updateLabelTime(0, 0); 
} 

private void setAlarm(int Hour, int Minute){ 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.HOUR_OF_DAY, Hour); 
    cal.set(Calendar.MINUTE, Minute); 
    cal.set(Calendar.SECOND, 0); 

    //in case of choosing a previous hour, then set alarm to next day 
    if (cal.getTimeInMillis() < System.currentTimeMillis()) 
     cal.set(Calendar.HOUR_OF_DAY, Hour + 24); 

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
Intent Notifyintent = new Intent(this, Notify.class); 
PendingIntent Notifysender = PendingIntent.getBroadcast(this, 0, Notifyintent, PendingIntent.FLAG_UPDATE_CURRENT); 
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 3600000, Notifysender); 

    Toast.makeText(AppointmentAdd.this, "Alarm set successfully" , Toast.LENGTH_LONG).show(); 
} 

public void onClickCancel(View v){ 
    startActivity (new Intent(getApplicationContext(), AppointmentView.class)); 
} 

} // end class 

,在這裏我AlarmReceiver類:

public class AlarmReceiver extends BroadcastReceiver { 

@SuppressWarnings("deprecation") 
@Override 
public void onReceive(Context context, Intent intent) { 
     NotificationManager myNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(R.drawable.ic_launcher, "Update Device", 0); 
     Intent notificationIntent = new Intent(context, AppointmentView.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, "Appointment", "Please check your appointment list", contentIntent); 
     notification.flags |= Notification.FLAG_HIGH_PRIORITY; 
     myNotificationManager.notify(0, notification); 
} 
} 
+0

任何錯誤,而達到特定的報警時間,還是你在註冊清單中的接收器? – Kanth 2013-04-26 04:20:46

+0

沒有錯誤,我已經在Manefest註冊了。實際上我想獲取在sqlite中設置的日期和時間,並且鬧鐘基於日期和時間。我覺得我的代碼是功能setAlarm錯的..我越來越堅持,首先我要用戶添加約會,讓說25/4/2013下午4:30上。該數據是保存在sqlite的。那麼當天,我想要應用程序,以警告用戶..我怎麼能這樣做?我知道使用報警管理器,但我不知道從哪裏代碼 – new 2013-04-26 10:24:46

+0

ok了,它已經可以到主菜單啓動,爲alarmreceiver類,我可以做麪包或alertdialog而不是更改活動? – new 2013-04-26 11:16:36

回答

0

按您的評論:

這是因爲pendingintent需要不同請求ID每次。現在

你現在的PendingIntent是以下方式:

PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

在這裏,你是給reqid爲0每一次,所以不是被創造不同未決的意圖,它覆蓋本懸而未決的意圖,這就是爲什麼你會得到最新的設定警報。因此,每次像下面給出不同號碼的唯一請求ID。

PendingIntent pi = PendingIntent.getBroadcast(this, reqid, intent,PendingIntent.FLAG_UPDATE_CURRENT); 

即使我做了同樣的事情,按照您的要求,我不得不把整個數據存儲在有關消息和報警SQLite數據庫。因此,爲了每次設置不同的request id,我已經使用數據庫中的主鍵ID來爲待定意圖設置主鍵ID。希望這可以幫助。

+0

regilia需要iniliaze它是可變的嗎? – new 2013-04-27 10:13:40

+0

這是一個整數變量。您需要生成不同的編號並每次分配給該變量。 – Kanth 2013-04-27 10:47:35

+0

是的,但現在的概率是,我不知道如何獲取我存儲爲日期時間在sqlite中的日期,並根據代碼,小時,月,日,年使用它..我陷入了困境 – new 2013-04-27 10:55:47

0

你可以試試這個:

final int alarmId = (int) System.currentTimeMillis(); 
PendingIntent pi = 
    PendingIntent.getBroadcast(Date_Time.this, alarmId, i,PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager manager = (AlarmManager) getSystemService(Date_Time.this.ALARM_SERVICE); 
manager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis() , pi);