i have simple intentservice run when remove android:process=":serviceprocess" line in manifest if add not running.
but need intentservice continue running when app finished.
when android:process removed, debug works (onhandleintent) when android:process added , debug not enter code, in example have counter increment every second, work when android:process deleted , not work when added.
code en manifest
android:name=".myintentservice" android:exported="false" android:process=":serviceprocess" ></service>
simple intentservice
public class myintentservice extends intentservice { // todo: rename actions, choose action names describe tasks // intentservice can perform, e.g. action_fetch_new_items private static final string action_foo = "layout.action.foo"; private static final string action_baz = "layout.action.baz"; public static volatile boolean shouldcontinue = true; public static float contador=0; // todo: rename parameters private static final string extra_param1 = "layout.extra.param1"; private static final string extra_param2 = "layout.extra.param2"; public myintentservice() { super("myintentservice"); } /** * starts service perform action foo given parameters. if * service performing task action queued. * * @see intentservice */ // todo: customize helper method public static void startactionfoo(context context, string param1, string param2) { intent intent = new intent(context, myintentservice.class); intent.setaction(action_foo); intent.putextra(extra_param1, param1); intent.putextra(extra_param2, param2); context.startservice(intent); } /** * starts service perform action baz given parameters. if * service performing task action queued. * * @see intentservice */ // todo: customize helper method public static void startactionbaz(context context, string param1, string param2) { intent intent = new intent(context, myintentservice.class); intent.setaction(action_baz); intent.putextra(extra_param1, param1); intent.putextra(extra_param2, param2); context.startservice(intent); } @override protected void onhandleintent(intent intent) { while(true) { if(shouldcontinue) { try { thread.sleep(1000); contador++; } catch (interruptedexception e) { e.printstacktrace(); } }else return; } } /** * handle action foo in provided background thread provided * parameters. */ private void handleactionfoo(string param1, string param2) { // todo: handle action foo throw new unsupportedoperationexception("not yet implemented"); } /** * handle action baz in provided background thread provided * parameters. */ private void handleactionbaz(string param1, string param2) { // todo: handle action baz throw new unsupportedoperationexception("not yet implemented"); } }
here called
button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { myintentservice.shouldcontinue=true; intent mserviceintent = new intent(getactivity(), myintentservice.class); getactivity().startservice(mserviceintent); } });
in other fragment value change when intentservice running (whenandroid:process line deleted manifest)
txtcontador.settext( string.valueof(myintentservice.contador));
Comments
Post a Comment