Shutdown Foreground Service from tap notification action - android

I have an app that uses a ForegroundService and a BroadcastReceiver.
When the app is started, the ForegroundService is started too and the mandatory notification is sent to user to notify that such a service is running. Based on the App's way to work, Service needs to run indefinetely but i would like that the user could eventually shutdown the Service using a TapAction on this notification.
Actually i achieved this result sending an explicit Intent to BroadcastReceiver and then on the onReceive() method send another explicit Intent to the ForegroundService to shut it down with stopService() method.
I was wondering if there is a way to send directly an Intent from TapAction to ForegroundService considering that there is no onReceive() method on Service

Related

Is there is a way to for activity to communicate a service which start the activity?

I have a service which have a callback function for detecting changes in the clipboard where the callback has a pendingIntent which is used by a notification and later notification used to start an activity once the notification is clicked.So here is my question, so is there is a possibility for the started activity to notify back to service once the activity is started (one way communication is just fine)?
You could send a broadcast Intent from the Activity to notify the Service. The Service should register a BroadcastReceiver that will be triggered by the broadcast Intent.

Foreground service stops on killing app

I have added below code in onStartCommand of service-
Notification notification = new NotificationCompat.Builder(AbcService.this)
.setContentTitle("xyz")
.setTicker("xyz")
.setContentText("xyz")
.setSmallIcon(R.drawable.mevo_chat_mevopic)
.setOngoing(true).build();
startForeground(AppConstants.ABC,
notification);
and stopForeground(true); inside onDestroy() of service.I expect service to run even when app is destroyed as it is foreground service but i see that sometimes it runs and sometimes it gets stopped.I don't have any other apps open and my device doesn't have memory issue.What could be possible reasons for my foreground service being stopped and how can i resolve this?
If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped. So what you can do is, send broadcast from onTaskRemoved method of your service as follows:
Intent intent = new Intent("com.android.ServiceStopped");
sendBroadcast(intent);
and have an broadcast receiver which will again start a service. I have tried it. service restarts from all type of kills.

Android BroadcastReceiver and killing process

let's assume we start a service from BrodcastReceiver on some broadcast. Can the process be killed after onReceive() returns and before service is started? And if so, how can this behavior be prevented?
If the BroadcastReceiver has called Context.startService() and it receives back a non-null ComponentName object, then no. That means the Service was found in the system and has been "started". Note that the start is asynchronous, so there's no guarantee that the Service has received its onStartCommand() callback before the receiver exits its onReceive() callback.
Also note that even though the BroadcastReceiver has started the Service, by default this has no effect on power management. So it is possible for your Service to not get called back until the device is fully up and running - it could go back to sleep after your BroadcastReceiver finishes but before your Service gets to run.

Stopping an IntentService from an activity

I have an IntentSerivce that runs in the background sometimes and it can be quite a long running process in certain instances. I give an option for the user to quit the application which basically just stops and polling and ignores any GCM push notifications. But if a push notification came in and the IntentService is taking a while to finish doing what it has to (gets information from a server and sends a notification to the user if needed, like a new message arrived or something).
Here is the problem, if the user elects to "Quit" the app while the intentservice is still running they will still get the notification which I do not want. I know that there is the stopSelf() method for the service but I need to stop it in an activity when I know the user "Quit" the application via a menu button. Sending another intent to the service does not work since the intents get queued up and calling context.stopService(intent); in my activity does not work either so how else can I stop it?
Are you passing a new Intent into stopService(Intent) or the original one used in startService(Intent). Passing the original Intent should stop the service.
Failing that you could use a Handler to pass the service a Message. Have the IntentService implement the Handler.Callback interface and create a new Handler(Handler.Callback) in your Activity, passing your IntentService as callback. Then implement the onHandleMessage() in your IntentService to call stopSelf() and have your Activity pass a message to it when you want it to stop.
Below code is perfectly working fine for me to stop IntentService from Activity:
stopService(new Intent(getApplicationContext(), MyIntentService.class));

Android - Can I send an intent on Wifi state change to a service?

I want to send an intent to my service everytime the state of Wifi connectivity changes.
So when I currently use a broadcast receiver to listen for the state changes in Wifi, so when this recieves an intent I want to be able to send this info on to my service.
Is this possible and if so the correct way to do it?
If the service is going to be running at the time, you could just register a BroadcastReceiver in the Service directly via registerReceiver().
Otherwise, call startService() from the BroadcastReceiver to let the Service know of the event, starting up the Service if it is not running. Be sure to shut down that Service at some point (e.g., use IntentService, which will automatically shut itself down when there is no more work to do).

Categories

Resources