android - Keep service running in background even user clear memory -
i have service follows:
public class service extends service { private static final string tag = "myservice"; @override public void oncreate() { super.oncreate(); log.d(tag, "oncreate"); } @override public ibinder onbind(intent intent) { return null; } public void ondestroy() { toast.maketext(this, "my service stopped", toast.length_long).show(); log.d(tag, "ondestroy"); } @override public int onstartcommand(intent intent, int flags, int startid) { intent intents = new intent(getbasecontext(),mainactivity.class); intents.setflags(intent.flag_activity_new_task); startactivity(intents); toast.maketext(this, "my service started", toast.length_long).show(); log.d(tag, "onstart"); return start_not_sticky; } }
it worked well, , can check service running when go application setting. however, if clear applications long press home button , clear them. did not see running service. how can maintain service running in background, user clear apps or memory? thank all
this manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hellobootservice" android:permission="android.permission.receive_boot_completed"> <uses-permission android:name="android.permission.receive_boot_completed"></uses-permission> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".autostart"> <intent-filter> <action android:name="android.intent.action.boot_completed" /> </intent-filter> </receiver> <service android:enabled="true" android:process=":hello" android:name=".service" /> </application> </manifest>
replace
return start_not_sticky;
with
return start_sticky;
inside onstartcommand().
Comments
Post a Comment