Reputation: 1667
I am having a weird situation where a Service that is created is stopping - sometimes. I have a entry Activity A that starts a service using bindService
// if we now have an IP address then bind ourselves to the MessageService
bindService(new Intent(this, MessagingService.class),
onMessageService,
BIND_AUTO_CREATE);
The MessageService handles kicking of Read and Send threads to handle message traffic with the app. It basically handles polling for new messages at 1 second intervals using a StatusTask and it's timer using timer.scheduleAtFixedRate.
Activity A then kicks off another Activity B that displays info to the user. For some reason that I yet to figure out, most of the time when I press the home button, the polling stops and the Service seems to have stopped. Reslecting my app from the Home recent apps list or via a notification I post when not visible brings the Activity to the foreground, but the Service seems to be gone. Making this harder to debug, about 10-20% of the time everything works great and the Message Polling service keeps plugging away.
Should I be using startService instead? The only direct relationship that the second Activity B has with the Service is that registers itself as an observer of the Read thread in order to be notified about timeouts on Reads. I am not calling stopService anywhere in my code.
Upvotes: 0
Views: 1672
Reputation: 188
public class Testservice extends Service {
private static final String TAG = Testservice.class.getSimpleName();
public Timer timer; TimerTask scanTask; final Handler handler = new Handler(); Timer t = new Timer();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Service creating");
_startService();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroying");
t.cancel();
t = null;
}
public void yourfunction() {
}
//this will invoke the function on everysecond basis so try it if it helpsa public void _startService(){
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
yourfunction();
}
});
}};
t.schedule(scanTask, 1000L, 1000L);
}
Upvotes: 1
Reputation: 48559
Starting a service with bbindService
makes the service lifecycle tied to the bound activities. Once your activity unbinds from the service, the service dies.
Upvotes: 1
Reputation: 188
a simple service demo class
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;
public class ServicesDemo extends Activity implements OnClickListener {
private static final String TAG = "AlertService";
Button buttonStart, buttonStop;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.servicedemo);
buttonStart = (Button) findViewById(R.id.btnStart);
buttonStop = (Button) findViewById(R.id.btnStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src) { switch (src.getId()) { case R.id.btnStart: Log.d(TAG, "onClick: starting srvice"); startService(new Intent(this, Testservice.class)); break; case R.id.btnStop: Log.d(TAG, "onClick: stopping srvice"); stopService(new Intent(this, Testservice.class)); break; } } }
Upvotes: 0
Reputation: 188
if developing using eclipse ---> try this go to DDMS that will be in the Perspective Option in Menu bar ---> select Logcat and while you are running your application just try to repeat the sequence you just mentioned above and on pressing home button just look at what is the error if at all coming during that instance and post the error so that the specific reason could be understood
Regards, Mistry Hardik
Upvotes: 1