i have created an application which will start a service to check a particular remote file for update..
the service part is done but now the user needs to manually start up the service
is there a way which i can make the service start up automatically once the application is not in view (either the user clicks the home button/clicks back or open another application)
++++++++++ANSWER++++++++++
anyway figured out how to do it already
#Override
protected void onPause()
{
startService(new Intent(this, fileService.class));
super.onPause();
}
#Override
protected void onResume()
{
stopService(new Intent(this, fileService.class));
super.onResume();
}
i used this to start the service when the application is paused and stop when its resume
You can not do this on Application level, but on Activities level. Activities have a lifecycle, where they get notified when their status changes.
You need to implement onPause() or onStop() in your Activity.
I thought the answer should be clear and simple for futere needings.
#Override
protected void onPause() {
startService(new Intent(this, YourService.class));
super.onPause();
}
#Override
protected void onResume() {
stopService(new Intent(this, YourService.class));
super.onResume();
}
Related
Why it not possible ?
#Override
protected void onDestroy() {
startService(new Intent(getApplicationContext(), notificationService.class));
super.onDestroy();
}
I really need to execute a service when the application isn't running.
I am developing a app where I need to updated my values every 15 min.
For that i am using services.I came across different types of services.
Like for long running services we use simple service and,
for interacting for other components we use bind service,
foreground service. etc....
My situation is like i need to run the service for every 15 min
when my app is open and stop the service when my app is closed
I have tried with bind service using
http://www.truiton.com/2014/11/bound-service-example-android/
but i am unable to do that,I am unable to run the service every 15 min can any one help me.
Thanks in advance.
To start the service when your app starts, start it in onCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
To stop the service, you can implement the code in 3 different methods namely, onPause(), onStop() and onDestroy().
Calling stopService() in onPause() will stop the service as soon as some other event happens on your device like a phone call, which is not best way to stop since the user will return back to the Activity immediately as soon as the call finishes.
Calling stopService() in onDestroy() is also not the best solution because onDestroy is not called in all the ways a user can close an Android app.
Therefore, the best way to stop the service is by calling stopService() in the onStop() method as shown below.
#Override
protected void onStop() {
super.onStop();
Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent);
}
If you want to stop service when your activity is closing then you have to implement the code inside onDestroy().
Below is an example-
#Override
protected void onDestroy() {
super.onDestroy();
Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent);
}
This will your stop your service.
Unless you don't call finish() in the activity or you explicitly stop the app the onDestroy() don't gets called and your service will run even your calling activity is onPause (in background).
Similarly if you want to start your service activity start you implement in onCreate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
let me know if it help your problem .
I have a service which plays music and an activity which provides the GUI for interacting with the service.The activity opens on list item click(I have a list of recordings) and it binds the service(and create it) at onCreate() method.
When onDestroy() is called, I unbind the service (this will destroy the service) - this should be OK since I do not want the service to run if the activity is exited, but the problem appear on orientation change because it re-creates the activity again and the service too(and the track is stopped and played again from the beginning when rotating the device).
I know about some flags (orientationChange) that might be useful, but is not a good practice for me since I want a different layout on landscape.
Also I could make the music player service to run as long as my app runs, but isn't a good idea since the user may not want to open the player, but want just to record, so the player service isn't necessarily here.
Here are some code snippets:
#Override
protected void onCreate(Bundle savedInstanceState) {
LocalBroadcastManager.getInstance(this).registerReceiver(mLocalReceiver, new IntentFilter(PlayerBroadcastReceiver.ACTION_PLAYER_SERVICE_STARTED));
setContentView(R.layout.media_player_screen);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
AudioPlayerServiceBridge.getInstance().addCallback(this);
AudioPlayerServiceBridge.getInstance().doBindService(this);
init(savedInstanceState);
super.onCreate(savedInstanceState);
}
#Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalReceiver);
mLocalReceiver.removeCallback();
Log.d(AudioPlayerActivity.class.getName(), "onDestroy() -> "+AudioPlayerActivity.class.getName());
AudioPlayerServiceBridge.getInstance().doUnbindService(this);
AudioPlayerServiceBridge.getInstance().removeCallback(this);
super.onDestroy();
}
and the service connection manager:
public void doBindService(Context context) {
// Establish a connection with the service. We use an explicit
// class name because there is no reason to be able to let other
// applications replace our component.
if(!mIsBound){
context.bindService(new Intent(context,
AudioPlayerService.class), serviceConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
}
public void doUnbindService(Context context) {
if (mIsBound) {
// If we have received the service, and hence registered with
// it, then now is the time to unregister.
if (mServiceMessenger != null) {
Message msg = Message.obtain(null, AudioPlayerService.MSG_UNREGISTER_CLIENT);
msg.replyTo = mMessenger;
mServiceMessenger.send(msg);
}
// Detach our existing connection.
context.unbindService(serviceConnection);
mIsBound = false;
}
}
Please show me if possible a good practice to handle this problem.
The answer is:
I should start the service with : startService(new Intent(this, service.class)) AND START BINDING after that. This method prevent the service to be killed when doUnbind() is called. So the onCreate() method is changed now in:
#Override
protected void onCreate(Bundle savedInstanceState) {
LocalBroadcastManager.getInstance(this).registerReceiver(mLocalReceiver, new IntentFilter(PlayerBroadcastReceiver.ACTION_PLAYER_SERVICE_STARTED));
setContentView(R.layout.media_player_screen);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
if(savedInstanceState == null)
startService(new Intent(this, AudioPlayerService.class));
AudioPlayerServiceBridge.getInstance().addCallback(this);
AudioPlayerServiceBridge.getInstance().doBindService(this);
init(savedInstanceState);
super.onCreate(savedInstanceState);
}
onDestroy() method:
#Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalReceiver);
mLocalReceiver.removeCallback();
Log.d(AudioPlayerActivity.class.getName(), "onDestroy() -> "+AudioPlayerActivity.class.getName());
AudioPlayerServiceBridge.getInstance().doUnbindService(this);
AudioPlayerServiceBridge.getInstance().removeCallback(this);
super.onDestroy();
}
and stop the service(if you want) in onBackPressed():
#Override
public void onBackPressed() {
Log.d(AudioPlayerActivity.class.getName(), "onBackPressed() -> "+AudioPlayerActivity.class.getName());
isPaused = true;
Log.d(AudioPlayerActivity.class.getName(), "Sending message to player service: MSG_RELEASE_PLAYER");
AudioPlayerServiceBridge.getInstance().sendAsyncCall(AudioPlayerService.MSG_RELEASE_PLAYER);
if(mSeekBarChanger != null){
mSeekBarChanger.stopThread();
}
AudioPlayerServiceBridge.getInstance().doUnbindService(this);
stopService(new Intent(this, AudioPlayerService.class));
super.onBackPressed();
}
I have a service that look gps coordinate. I want that when the user click on the home button the gps stop to work (battery saving).
The strategy i am using is this:
#Override
public void onPause() {
super.onPause();
doUnbindService();
}
#Override
public void onResume() {
super.onResume();
doBindService();
}
on each Activity that use the service. The problem is that each time i switch activity it stop and resume. Is there a strategy that let me have the service always up until my app is in foregroung?
I have an app with 3 activities that use a service to deal with the GPS. In each activity's onStart() I have code of the form
bindService(new Intent(..., ..., Context.BIND_AUTO_CREATE);
and in each onStop() I have
unbindService(..);
You should find that activity 2's onStart() will execute before activity 1's onStop(). Thus only when no activities are running will the service stop.
In my app I am trying to use a Service to redirect my app to another Application.
I am starting this Service in onPause method and I am trying to stop this Service in onResume method, but the Service still keeps running and it redirects automatically.
protected void onPause() {
Intent in = new Intent(HelloappActivity.this,SimpleService.class);
in.putExtra("qwe", k);
startService(in);
super.onPause();
}
protected void onResume() {
stopService(new Intent(HelloappActivity.this,SimpleService.class));
}
so how should I stop this service?