killing a service in android? - android

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?

Related

Destroying a service as soon as the app is killed

I need to create a service that will run as much as app will be running. I know that the most straightforward solution would be to bind service in my Application class but this class does not have onDestroy onStop or onPause methods so unbinding is not possible... sadly... Other approach is to bind service to my base activity (and other activities will extend that activity) and for example onResume bind and onStop unbind service. But then service will be always recreated... And that is not I want. Is it possible to have continuously running service and destroy it as soon as application closed? Here's the second approach (this code is in my base activity)
protected void onResume() {
super.onResume();
if (service == null && !isBound) {
bindService(new Intent(this, NetworkListenerService.class), mServiceConnection, BIND_AUTO_CREATE);
}
}
#Override
protected void onPause() {
super.onPause();
if (isBound) {
unbindService(mServiceConnection);
isBound = false;
}
}
Set the below attribute in your service tag in the manifest file,
<service android:name="service" android:stopWithTask="true"/>
adding this will call the ondestroy() of the service when the task is removed.

I need open a service when i close the application

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.

How to stop service the service when android app has been closed

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 .

How to unbind a Service when the Activity Stops?

I'm trying to make a music player. To do that I'm creating a service that manages a MediaPlayer object and plays the songs. But every time I reopen my music application it starts a new Service and keeps playing two songs simultaneously. How can I make my Activity bind to the already running service?
I noticed that when I don't call unbindService (...) in the OnStop() method my application runs OK. But I don't know if its right to not unbind when the activity stops.
I'm binding like that:
#Override
protected void onStart() {
super.onStart();
Intent service = new Intent(this,MyService.class);
bindService(service, mConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
if(mBound) {
unbindService(mConnection);
mBound = false;
}
}
You need to check on onStart() that is service running or not. If service is already running, then you need to just bind it. But in your current code you creating service everytime.
You should try to use Service.START_STICKY for checking that service is already running or not. You can see :
Service
, start Sticky or start not sticky
I think it will help you to update your service class.
I could fix it add startService(...) to the OnStart( ) method. I didnt understand why this fixed, but seems that I need to do that. If anyone knows why please add. Thanks for everyone help.
this is how OnStart got:
#Override
protected void onStart() {
super.onStart();
Intent service = new Intent(this,MyService.class);
bindService(service, mConnection, Context.BIND_AUTO_CREATE);
startService(service);
}

automatically start up a service when the application is closed

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();
}

Categories

Resources