android - Background service using alarm manager -
i have followed below example code implementing periodic background service. periodic task executes correctly if app on foreground on 1st time. if, close application background service not working.
1) broadcastreceiver service bootcomplete
public class bootbroadcastreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // launch specified service when message received intent startserviceintent = new intent(context, mytestservice.class); context.startservice(startserviceintent); } }
2) broadcastreceiver start service
public class myalarmreceiver extends broadcastreceiver { public static final int request_code = 12345; // triggered alarm periodically (starts service run task) @override public void onreceive(context context, intent intent) { intent = new intent(context, mytestservice.class); i.putextra("foo", "bar"); context.startservice(i); toast.maketext(context, "welcome --------1", toast.length_long).show(); } }
3) intenetservice class: execute background task
public class mytestservice extends intentservice { // must create default constructor public mytestservice() { // used name worker thread, important debugging. super("test-service"); } @override public void oncreate() { super.oncreate(); // if override oncreate(), make sure call super(). } @override protected void onhandleintent(intent intent) { } }
4) service started activity as
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); schedulealarm(); } // setup recurring alarm every half hour public void schedulealarm() { // construct intent execute alarmreceiver intent intent = new intent(getapplicationcontext(), myalarmreceiver.class); // create pendingintent triggered when alarm goes off final pendingintent pintent = pendingintent.getbroadcast(this, myalarmreceiver.request_code, intent, pendingintent.flag_update_current); // setup periodic alarm every 5 seconds long firstmillis = system.currenttimemillis(); // alarm set right away alarmmanager alarm = (alarmmanager) this.getsystemservice(context.alarm_service); // first parameter type: elapsed_realtime, elapsed_realtime_wakeup, rtc_wakeup // interval can interval_fifteen_minutes, interval_half_hour, interval_hour, interval_day alarm.setrepeating(alarmmanager.rtc_wakeup, firstmillis, 30000, pintent); } }
5) manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="test.org.help"> <permission android:name="test.org.help.permission.c2d_message" android:protectionlevel="signature" /> <uses-permission android:name="au.com.kps.companion.access_data" /> <uses-permission android:name="android.permission.receive_boot_completed" /> <uses-permission android:name="android.permission.vibrate" /> <uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.camera" /> <uses-permission android:name="android.permission.flashlight" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="test.org.help.permission.ua_data" /> <!-- gcm requires google account. --> <uses-permission android:name="android.permission.get_accounts" /> <!-- app has permission register gcm , receive message --> <uses-permission android:name="com.google.android.c2dm.permission.receive" /> <uses-permission android:name="test.org.help.permission.c2d_message" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /> <uses-feature android:name="android.hardware.camera.flash" android:required="false" /> <uses-feature android:name="android.hardware.screen.landscape" /> <application android:name="test.org.help.kpsapp" android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:logo="@drawable/ic_actionbar_logo" android:theme="@style/apptheme" tools:replace="android:icon, android:logo"> <!-- activities --> <activity android:name="test.org.help.ui.activity.splashactivity" android:label="@string/app_name" android:screenorientation="portrait" android:theme="@style/splashtheme"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".ui.activity.janrainloginactivity" android:screenorientation="portrait" /> <activity android:name=".ui.activity.linklistactivity" android:screenorientation="portrait" /> <activity android:name="test.org.help.ui.activity.kpsfragmentactivity" android:exported="true" android:screenorientation="portrait" android:windowsoftinputmode="adjustresize"></activity> <activity android:name="test.org.help.ui.activity.notificationdialogactivity" android:excludefromrecents="true" android:label="" android:launchmode="singleinstance" android:nohistory="true" android:screenorientation="portrait" android:taskaffinity="" android:theme="@style/apptheme.dialog.notification"></activity> <receiver android:name="test.org.help.receivers.notificationreceiver" tools:ignore="exportedreceiver"> <intent-filter> <action android:name="test.org.help.util.action_show_agenda" /> <action android:name="test.org.help.util.action_snooze" /> <action android:name="test.org.help.util.action_dismiss" /> <action android:name="test.org.help.util.action_show_dialog" /> <data android:scheme="kps" /> <data android:mimetype="text/plain" /> <data android:pathpattern="au\.org\.kps\.util/.*" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.boot_completed" /> </intent-filter> </receiver> <activity android:name="com.google.zxing.client.android.captureactivity" android:screenorientation="landscape" android:theme="@android:style/theme.light.notitlebar" tools:replace="android:theme,android:screenorientation"></activity> <activity android:name="test.org.help.ui.activity.apptutorialactivity" android:screenorientation="portrait" android:theme="@style/splashtheme"></activity> <activity android:name="test.org.help.ui.activity.termsandconditionsactivity" android:label="@string/title_terms_and_conditions" android:screenorientation="portrait"></activity> <activity android:name="test.org.help.ui.activity.warningactivity" android:label="@string/title_warning" android:screenorientation="portrait"></activity> <activity android:name="test.org.help.ui.activity.differentuserwarningactivity" android:label="@string/title_warning" android:screenorientation="portrait"></activity> <activity android:name="test.org.help.ui.activity.appwhatsnewactivity" android:label="@string/title_warning" android:screenorientation="portrait"></activity> <activity android:name=".ui.activity.userupgradeactivity" android:label="@string/title_upgrade" android:screenorientation="portrait"></activity> <activity android:name="eu.janmuller.android.simplecropimage.cropimage" android:label="" android:screenorientation="portrait"></activity> <!-- facebook --> <meta-data android:name="com.facebook.sdk.applicationid" android:value="@string/facebook_app_id" /> <!-- services --> <receiver android:name="com.mobileapptracker.tracker"> <intent-filter> <action android:name="com.android.vending.install_referrer" /> </intent-filter> </receiver> <service android:name="test.org.help.service.dbupdateservice" android:exported="false" android:label="@string/app_name"> <intent-filter> <action android:name="test.org.help.service.dbupdateservice"></action> </intent-filter> </service> <receiver android:name="test.org.help.receivers.timechangereceiver"> <intent-filter> <action android:name="android.intent.action.timezone_changed" /> </intent-filter> </receiver> <activity android:name="com.janrain.android.engage.ui.jrfragmenthostactivity" android:configchanges="orientation|screensize" android:screenorientation="portrait" android:windowsoftinputmode="adjustresize|statehidden" /> <activity android:name="com.janrain.android.engage.ui.jrfragmenthostactivity$fullscreen" android:configchanges="orientation|screensize" android:screenorientation="portrait" android:windowsoftinputmode="adjustresize|statehidden" /> <!--hockey app crash report--> <meta-data android:name="net.hockeyapp.android.appidentifier" android:value="@string/hockey_app_id" /> <receiver android:name="test.org.help.syncmanager.network.networkavailability"> <intent-filter> <action android:name=".android.action.broadcast" /> <action android:name="android.net.conn.connectivity_change" /> <action android:name="android.net.wifi.wifi_state_changed" /> </intent-filter> </receiver> <activity android:name=".db.androiddatabasemanager" android:theme="@style/theme.appcompat.light" /><!-- attention: auto-generated add google play services project app indexing. see https://g.co/appindexing/androidstudio more information. --> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <service android:name=".syncmanager.auto.mytestservice" android:exported="false" /> <receiver android:name=".syncmanager.auto.myalarmreceiver" android:process=":remote"></receiver> <receiver android:name=".syncmanager.auto.bootbroadcastreceiver"> </receiver> </application> </manifest>
if, implement above sample project separate then, background service executing fine always. if try integrate existing code not working...
Comments
Post a Comment