android - Use AlarmManager without a service -
is possible use alarmmanager run alarm without app using service. alarmmanager have bring device out of sleep mode , execute code in onreceive. virtually every code sample have seen posted here shows service being used. yet android docs not mention need of having service.
here working example,
activity_alarm_manager.xml file's code
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <button android:id="@+id/btstart" android:layout_height="wrap_content" android:layout_width="match_parent" android:onclick="startrepeatingtimer" android:padding="@dimen/padding_medium" android:text="@string/btstart" tools:context=".widgetalarmmanageractivity"/> <button android:id="@+id/btcancel" android:layout_height="wrap_content" android:layout_width="match_parent" android:onclick="cancelrepeatingtimer" android:padding="@dimen/padding_medium" android:text="@string/btcancel" tools:context=".widgetalarmmanageractivity"/> <button android:id="@+id/btonetime" android:layout_height="wrap_content" android:layout_width="match_parent" android:onclick="onetimetimer" android:padding="@dimen/padding_medium" android:text="@string/btonetime" tools:context=".widgetalarmmanageractivity"/> </linearlayout>
broadcastreceiver's code,
public class alarmmanagerbroadcastreceiver extends broadcastreceiver { final public static string one_time = "onetime"; @override public void onreceive(context context, intent intent) { powermanager pm = (powermanager) context.getsystemservice(context.power_service); powermanager.wakelock wl = pm.newwakelock(powermanager.partial_wake_lock, "your tag"); //acquire lock wl.acquire(); //you can processing here. bundle extras = intent.getextras(); stringbuilder msgstr = new stringbuilder(); if(extras != null && extras.getboolean(one_time, boolean.false)){ //make sure intent has been sent one-time timer button. msgstr.append("one time timer : "); } format formatter = new simpledateformat("hh:mm:ss a"); msgstr.append(formatter.format(new date())); toast.maketext(context, msgstr, toast.length_long).show(); //release lock wl.release(); } public void setalarm(context context) { alarmmanager am=(alarmmanager)context.getsystemservice(context.alarm_service); intent intent = new intent(context, alarmmanagerbroadcastreceiver.class); intent.putextra(one_time, boolean.false); pendingintent pi = pendingintent.getbroadcast(context, 0, intent, 0); //after after 5 seconds am.setrepeating(alarmmanager.rtc_wakeup, system.currenttimemillis(), 1000 * 5 , pi); } public void cancelalarm(context context) { intent intent = new intent(context, alarmmanagerbroadcastreceiver.class); pendingintent sender = pendingintent.getbroadcast(context, 0, intent, 0); alarmmanager alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service); alarmmanager.cancel(sender); } public void setonetimetimer(context context){ alarmmanager am=(alarmmanager)context.getsystemservice(context.alarm_service); intent intent = new intent(context, alarmmanagerbroadcastreceiver.class); intent.putextra(one_time, boolean.true); pendingintent pi = pendingintent.getbroadcast(context, 0, intent, 0); am.set(alarmmanager.rtc_wakeup, system.currenttimemillis(), pi); } }
androidmanifest.xml's code
<manifest android:versioncode="1" android:versionname="1.0" package="com.rakesh.alarmmanagerexample" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-sdk android:minsdkversion="10" android:targetsdkversion="15"/> <uses-permission android:name="android.permission.wake_lock"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme"> <activity android:label="@string/title_activity_alarm_manager" android:name="com.rakesh.alarmmanagerexample.alarmmanageractivity"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name="com.rakesh.alarmmanagerexample.alarmmanagerbroadcastreceiver"> </receiver> </application> </manifest>
alarmmanageractivity.java file's code
public class alarmmanageractivity extends activity { private alarmmanagerbroadcastreceiver alarm; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_alarm_manager); alarm = new alarmmanagerbroadcastreceiver(); } @override protected void onstart() { super.onstart(); } public void startrepeatingtimer(view view) { context context = this.getapplicationcontext(); if(alarm != null){ alarm.setalarm(context); }else{ toast.maketext(context, "alarm null", toast.length_short).show(); } } public void cancelrepeatingtimer(view view){ context context = this.getapplicationcontext(); if(alarm != null){ alarm.cancelalarm(context); }else{ toast.maketext(context, "alarm null", toast.length_short).show(); } } public void onetimetimer(view view){ context context = this.getapplicationcontext(); if(alarm != null){ alarm.setonetimetimer(context); }else{ toast.maketext(context, "alarm null", toast.length_short).show(); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_widget_alarm_manager, menu); return true; } }
a reference link github
Comments
Post a Comment