java - Service code stops when application is stopped -



java - Service code stops when application is stopped -

i have app gets users messages database , if there new message pushes notification utilize service that.. service works fine when app opened or in foreground.. when close it doesn't work.. not destroyed or stopped it's doesn't work :s don't know why.. service code :

public class bgservice extends service { arraylist<message> messages = new arraylist<message>(); arraylist<string> requests = new arraylist<string>(); timer timer = new timer(); timer timer2 = new timer(); @override public ibinder onbind(intent intent) { // todo auto-generated method stub homecoming null; } @override public void ondestroy() { log.d("chat", "bgservice destroyed"); timer.cancel(); timer.purge(); timer2.cancel(); timer2.purge(); } @suppresswarnings("unchecked") @override public void onstart(intent intent, int startid) { log.d("chat", "bgservice started"); messages = (arraylist<message>) intent.getextras().get("messages"); requests = (arraylist<string>) intent.getextras().get("requests"); log.d("button clicked", "messages: " + messages); new timer().scheduleatfixedrate(new timertask() { public void run() { log.d("service", "running"); } }, 2000, 2000); } }

your telling service stop in code. because using onbind() appears not starting service , instead binging it. if bind service service automatically ends when activity ends.

if want maintain service running.

start service maintain service running bind service have service while app running set notification service in foreground change manifest service runs in separate process

start service can homecoming startsticky in onstartcommand() tell os want stick on

@override public int onstartcommand(intent intent, int flags, int startid) { homecoming service.start_sticky; }

start service. bind service if have connection communicate service , forth with. returned onbind()

startservice(new intent(context, servicelocationrecorder.class)); // bind service if (!misbound) { // bind service bindservice(new intent(context, servicelocationrecorder.class), mconnection, context.bind_auto_create); misbound = true; }

binding service used setup binder handler can communicate , service with. returning null in onbind() defeats purpose of onbind() event skip code.

/** * when binding service, homecoming interface our messenger * sending messages service. */ @override public ibinder onbind(intent intent) { homecoming mmessenger.getbinder(); }

set service foreground , os less end service memory app.

//this done within service startforeground(r.id.action_record, getmycustomnotification());

run service in own process gc able collect activity , maintain service running.

<service android:name="com.example.service" android:process=":myseparateprocess" >s --> </service>

java android service

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -