android - How to create persistent alarms even after rebooting -
presently, working on app works "to task list". have implemented notificationservice , schedularservice in application. getting alerts(notifications) @ time set tasks. here queries below:
- with code alarms deleted after reboot ? if yes, how overcome this.
- i have kept priority feature tasks. want mechanism such if user selects priority "high" should receive notifications thrice, say, before 30 minutes, before 15 minutes , on time set. how achieve ?
- i want set phone's vibrate feature when notifications raised. how achieve ?
- and want know about, can done deprecated methods , constructor in notifyservice.java. thesse deprecated in api level 11:
notification notification = new notification(icon, text, time);
,notification.setlatesteventinfo(this, title, text, contentintent);
. on developer.android.com, have suggested usenotification.builder
instead. how make app compatible api levels.
here's snippet code scheduling alarm:
... scheduleclient.setalarmfornotification(c, tmp_task_id); ...
here's class scheduleclient.java:
public class scheduleclient { private scheduleservice mboundservice; private context mcontext; private boolean misbound; public scheduleclient(context context) { mcontext = context; } public void dobindservice() { mcontext.bindservice(new intent(mcontext, scheduleservice.class), mconnection, context.bind_auto_create); misbound = true; } private serviceconnection mconnection = new serviceconnection() { public void onserviceconnected(componentname classname, ibinder service) { mboundservice = ((scheduleservice.servicebinder) service).getservice(); } public void onservicedisconnected(componentname classname) { mboundservice = null; } }; public void setalarmfornotification(calendar c, int tmp_task_id){ mboundservice.setalarm(c, tmp_task_id); } public void dounbindservice() { if (misbound) { mcontext.unbindservice(mconnection); misbound = false; } } }
here's scheduleservice.java:
public class scheduleservice extends service { int task_id; public class servicebinder extends binder { scheduleservice getservice() { return scheduleservice.this; } } @override public int onstartcommand(intent intent, int flags, int startid) { return start_sticky; } @override public ibinder onbind(intent intent) { return mbinder; } private final ibinder mbinder = new servicebinder(); public void setalarm(calendar c, int tmp_task_id) { new alarmtask(this, c, tmp_task_id).run(); } }
here's alarmtask.java:
public class alarmtask implements runnable{ private final calendar date; private final alarmmanager am; private final context context; int task_id; public alarmtask(context context, calendar date, int tmp_task_id) { this.context = context; this.am = (alarmmanager) context.getsystemservice(context.alarm_service); this.date = date; task_id = tmp_task_id; } @override public void run() { intent intent = new intent(context, notifyservice.class); intent.putextra(notifyservice.intent_notify, true); intent.putextra("task_id", task_id); pendingintent pendingintent = pendingintent.getservice(context, 0, intent, 0); am.set(alarmmanager.rtc, date.gettimeinmillis(), pendingintent); } }
here's notifyservice.java:
public class notifyservice extends service { public class servicebinder extends binder { notifyservice getservice() { return notifyservice.this; } } int task_id; private static final int notification = 123; public static final string intent_notify = "com.todotaskmanager.service.intent_notify"; private notificationmanager mnm; sqlitedatabase database; @override public void oncreate() { mnm = (notificationmanager) getsystemservice(notification_service); } @override public int onstartcommand(intent intent, int flags, int startid) { string tmp_task_brief = null; task_id = intent.getintextra("task_id", 0); loaddatabase(); cursor cursor = database.query("task_info", new string[]{"task_brief"}, "task_id=?", new string[]{task_id+""}, null, null, null); while(cursor.movetonext()) { tmp_task_brief = cursor.getstring(0); } cursor.close(); if(intent.getbooleanextra(intent_notify, false)) shownotification(tmp_task_brief); return start_not_sticky; } @override public ibinder onbind(intent intent) { return mbinder; } private final ibinder mbinder = new servicebinder(); private void shownotification(string tmp_task_brief) { charsequence title = "to task notification!!"; int icon = r.drawable.e7ca62cff1c58b6709941e51825e738f; charsequence text = tmp_task_brief; long time = system.currenttimemillis(); notification notification = new notification(icon, text, time); pendingintent contentintent = pendingintent.getactivity(this, 0, new intent(this, taskdetails.class), 0); notification.setlatesteventinfo(this, title, text, contentintent); notification.flags |= notification.flag_auto_cancel; mnm.notify(notification, notification); stopself(); } void loaddatabase() { database = openorcreatedatabase("tododatabase.db", sqlitedatabase.open_readwrite, null); } }
with code alarms deleted after reboot ? if yes, how overcome this.
yes alarm deleted, overcome this, need use android's component called broadcastreceiver follows,
first, need permission in manifest:
<uses-permission android:name="android.permission.receive_boot_completed" />
also, in manifest, define service , listen boot-completed action:
<receiver android:name=".receiver.startmyserviceatbootreceiver" android:enabled="true" android:exported="true" android:label="startmyserviceatbootreceiver"> <intent-filter> <action android:name="android.intent.action.boot_completed" /> </intent-filter> </receiver>
then need define receiver boot_completed action , start service.
public class startmyserviceatbootreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { if ("android.intent.action.boot_completed".equals(intent.getaction())) { intent serviceintent = new intent("com.myapp.notifyservice"); context.startservice(serviceintent); } } }
and service should running when phone starts up.
2 vibration
again need define permission in androidmanifest.xml file follows,
<uses-permission android:name="android.permission.vibrate"/>
here code vibration,
// instance of vibrator current context vibrator v = (vibrator) getsystemservice(context.vibrator_service); // vibrate 300 milliseconds v.vibrate(300);
Comments
Post a Comment