制作定制的Android报警器提醒 [英] Make Custom Android Alarm for reminder
问题描述
我在做药的提醒应用程序,在该应用程序我已经包括增加了对医生的约会提醒的设施。在我的应用程序允许用户设定他/她自己的日期和时间,并在该日期和时间报警应触发..请帮我..我有需要引用从这个线程.. 如何在Android中设置报警?
i am making medicine reminder app,In that app i have included facility of adding a reminder about doctor's appointment. In my application user is allowed to set his/her own date and time and on that date and time alarm should triggered.. please help me.. and i have take reference from this thread.. How to set Alarm in Android?
推荐答案
使用以下步骤:
为了提醒日历:
1,创建一个事件是:
1.Create a event as:
// 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.Then创建提醒,并设置期运用code为:
2.Then create reminder and set it useing code as :
// 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.设置权限为:
3.Set permissions as :
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
对于仅报警:
1,创建的广播接收机,其
1.Create a broadcast receiver as:
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.设置权限:
2.set permission:
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
3.安装事件:
3.set up event:
// 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.Activity:
4.Activity from broadcast receiver:
@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();
}
}
现在编辑或修改code是根据你。希望这会帮助你。如果是接受并投票的答案。
Now edit or modify code as according to you. Hope this will help you. If yes accept and vote answer.
这篇关于制作定制的Android报警器提醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!