I need open a service when i close the application - android

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.

Related

How to start intent service when app is killed or destroyed?

I am try to solve this issue by Creating This Code Which is Shown Below. But I am not getting Result. can Anyone help to answer this Question?
-In Geofence Triggering Transition notification not getting while killed the app.
-Entry Exit Notification Works Properly When app is in Background but After kill the application notification not working Properly.
public void onDestroy()
{
super.onDestroy();
if(GpsService.mTimer != null)
{
startService(new Intent(context, GpsService.class));
}
Log.e(TAG, "onDestroy");
}
From your service's onStartCommand(), return START_STICKY. In that case (ideally) the service will restart even when the app is "killed".
(If START_STICKY works, no need to start service from onDestroy. Android will take care of it).
However, in some manufacturer's phone, if you clear the app from Recent Task list, it "Force Kills" the app. In that case the service will not restart and you can't do much.
public void onDestroy()
{
super.onDestroy();
if(GpsService.mTimer != null)
{
startService(new Intent(context, GpsService.class));
}
Log.e(TAG, "onDestroy");
}
This will first destroy your activity and then your condition will be checked . Which is useless thing .You can try this
public void onDestroy()
{
if(GpsService.mTimer != null)
{
startService(new Intent(context, GpsService.class));
}
Log.e(TAG, "onDestroy");
super.onDestroy();
}
`

Activity rotation cause service to be killed

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

Stopping a foreground service on Android when the application crashes

I have an Android application where I implement a Service which interacts with some hardware over a Bluetooth serial connection. The setup of this connection is slow, so I decided to keep the service in the foreground, so if/when you want to view another application, the connection is ready to go (pseudocode follows):
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
start();
return (START_STICKY);
}
#Override
public void onDestroy() {
stop();
}
start() and stop() are private methods which start communication with the hardware, and in start's case, creates a Notification for use in startForeground() My Activity will call
#Override
public void onStart() {
super.onStart();
// Start the service
Intent intent = new Intent(getApplicationContext(), MyService.class);
ComponentName theService = startService(intent);
//this is to register the functions I need to handle functions my Activity calls
// to the service
bindService(intent, svcConn, BIND_AUTO_CREATE);
}
#Override
public void onStop() {
super.onStop();
if (theService != null) {
unbindService(svcConn);
theService = null;
if (isFinishing()) {
stopService(new Intent(getApplicationContext(), MyService.class));
}
}
}
I've had to add a "Quit" menu item to make sure that the Service shuts down. Worse, if my app crashes, I have to go in and manually kill the Service. Is there a way to elegantly kill the Service if things go horribly wrong, or am I abusing the purpose of a Service, and should find an alternative method of doing what I'd like to do?
Perhaps you can add a hook for your application's main thread(UI thread) for crash, see below:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
//Kill the service.
}
});
throw new RuntimeException("Uncaught Exception");

killing a service in 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?

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