2013-03-26 157 views
4

我正在提醒藥物的應用程序,在那個應用程序中,我已包括添加關於醫生預約提醒的功能。在我的應用程序中,用戶可以設置他/她自己的日期和時間,並在該日期和時間觸發警報..請幫助我..並且我已參考此線程。 How to set Alarm in Android?使自定義Android提醒提醒

+0

你試過了什麼? – Rahil2952 2013-03-26 11:42:12

+0

我已經嘗試了報警管理器,等待意圖和一切,但還沒有得到預期的結果.. – 2013-03-26 11:44:53

+1

http://android-er.blogspot.in/2010/10/simple-example-of-alarm-service-using.html – 2013-03-26 11:45:16

回答

6

使用以下腳步。

對於提醒日曆:

1.創建一個事件爲:

// get calendar 
Calendar cal = Calendar.getInstance();  
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events"); 
ContentResolver cr = getContentResolver(); 

// event insert 
ContentValues values = new ContentValues(); 
values.put("calendar_id", 1); 
values.put("title", "Reminder Title"); 
values.put("allDay", 0); 
values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11  minutes from now 
values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now 
values.put("description", "Reminder description"); 
values.put("visibility", 0); 
values.put("hasAlarm", 1); 
Uri event = cr.insert(EVENTS_URI, values); 

2.然後創建提醒並將其設置期運用代碼:

// reminder insert 
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders"); 
values = new ContentValues(); 
values.put("event_id", Long.parseLong(event.getLastPathSegment())); 
values.put("method", 1); 
values.put("minutes", 10); 
cr.insert(REMINDERS_URI, values); 

3。設置權限爲:

<uses-permission android:name="android.permission.READ_CALENDAR" /> 
<uses-permission android:name="android.permission.WRITE_CALENDAR" /> 

僅用於報警:

1.創建一個廣播接收器:

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     try { 
      Bundle bundle = intent.getExtras(); 
      String message = bundle.getString("alarm_message"); 
      Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
     } catch (Exception e) { 
      Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); 
      e.printStackTrace(); 
     } 
    } 
} 

2.設置權限:

<receiver android:process=":remote" android:name="AlarmReceiver"></receiver> 

3.設定了事件:

// get a Calendar object with current time 
Calendar cal = Calendar.getInstance(); 
// add 5 minutes to the calendar object 
cal.add(Calendar.MINUTE, 5); 
Intent intent = new Intent(ctx, AlarmReceiver.class); 
intent.putExtra("alarm_message", "O'Doyle Rules!"); 
// In reality, you would want to have a static variable for the request code instead of 192837 
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

// Get the AlarmManager service 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

4.來自廣播的活動接收器:

@Override 
public void onReceive(Context context, Intent intent) { 
    try { 
     Bundle bundle = intent.getExtras(); 
     String message = bundle.getString("alarm_message"); 

     Intent newIntent = new Intent(context, AlarmActivity.class); 
     newIntent.putExtra("alarm_message", message); 
     newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(newIntent); 
    } catch (Exception e) { 
     Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); 
     e.printStackTrace(); 
    } 
} 

根據需要修改代碼。

+0

我不需要把這些東西放在日曆中。我只是想當用戶保存他/她的醫生的詳細信息,當時警報應該設置並在所需的時間應該被觸發.. – 2013-03-26 11:53:14

+0

所以你只需要警報或提醒? – 2013-03-26 11:54:59

+0

我需要提醒在這樣的日期你有任命Dr.X.i希望你有我的問題。你做? – 2013-03-26 12:00:23