I want to call the start Activity in BroadcastReceiver in android - android

I want to call the start Activity in BroadcastReceiver in android. I know it is a service and we cant call an activity from a service.

I want to call the start Activity in BroadcastReceiver in android.
Call startActivity() on the Context passed in as a parameter.
I know it is a service
A BroadcastReceiver is not a Service.
and we cant call an activity from a service.
Yes, you can call startActivity() in a Service.
However, calling startActivity() from either a BroadcastReceiver or a Service may not be what the user wants. You have no idea what the user is doing, and you might be taking over the foreground in the middle of something else. Please only use this in places where the user will value the interruption, or offer alternatives (e.g., a SharedPreference to indicate that you should use a Notification instead of starting an activity).

Related

Activity and Service communication

My application consists of one activity which creates a service. I want the service to be keep running as long as application is running. I know:
It is not guaranteed as Android system can kill activity in low memory conditions and if activity is in background.
The service can be stopped (and killed) by system.
If I bind service to the activity, the activity would get notification in case service is being stopped or started. However, the service may stop running if activity goes in background (onStop()). Please correct me if I am wrong here.
If I bind to service in onResume() of activity and unbind() in onStop(), it might happen that service stops running when my application goes in background. If I bind in onCreate() and unbind() in onDestroy() of activity, would it mean that my activity will keep getting notification from service even when in background.
What is the best way to keep service running and get notification from service to Activity as long as application is running. Please note that there is just one activity in the application so sending activity in background means application goes in background.
Thanks
true
true, but its more rare if us use startForeground()
The service usually won't stop until all activities have unbound. But when the last has, it will. So u can prevent the service from dieing when going to background, if you only unbind in onPause if isFinishing() == true.
see 3.
I personally like to set up a Handler in the Activity and send Messages to it from the service.
If you are binding a Service to your Activity. It simply means that you need service to run as long as your activity is running. If you do not need to bind Service with activity or you do not need to update your UI while your Service is running. you must not bind your Service to your Activity. In this case, for different actions done by Service you can notify user using Android Notifications. Like notifying user that xx download has been completed.
It totally depends upon your purpose that you want to achieve from Service.
if you can use IntentService for your application, you can pass data to the service through an Intent. results can be passed back to the Activity through a ResultReceiver
If you bind your Service to your unique Activity, you'll have it alive as long as the Activity is not terminated or the service isn't unbound. Just bind it on the onCreate() and let it get unbound when stopping your activity (no need to do anything).
You can create a Listener interface within your service, that you'll implement in the Activity, so you can send those notifications from the Service to the Activity. You'll find suitable example and information about this if Googling.

How to implement the communication between a Service and an Activity in different processes?

I read some similar questions (for example at this link), but the problem I'm asking is a bit different. In fact, in my case the service is started manually by the startService method, then as a consequence it can not be started using the bindService method.
Suppose we have a package that contains the MainService service and MainServiceActivity activity. In the file "AndroidManifest.xml" this activity is declared with action MAIN and category LAUNCHER. This activity is used to configure the service via the SharedPreferences and start the service by invoking startService method. In other words, typically the user launches the MainServiceActivity and configures/starts the MainService.
Now consider another activity (Let's call it SecondActivity) that is part of another package. Depending on the configuration, the service starts this activity using the startActivity method, so this other activity is running on a separate process than the MainService. As soon as the activity is running, it should inform the service.
At this point, a communication request/reply begins between the MainService and the SecondActivity: the service sends a request and the activity sends a reply.
The communication via messaging might fit, but the MainService is started through startService method, so the bindService method can not be invoked by activities that want to bind to the service.
Then I had an idea that makes use of an additional service (Let's call it UtilityService), which is part of the same package of MainService: the UtilityService could be started using the bindService method. As a consequence:
as soon as the MainService is running, it might perform the bind to the UtilityService;
when the MainService launches an external activity (for example the above SecondActivity), this activity bind to the UtilityService.
In this way, both the MainService and the SecondActivity are connected to the UtilityService, where the latter acts as an intermediary for communication.
Are there alternatives to this idea?
In fact, in my case the service is started manually by the startService method, then as a consequence it can not be started using the bindService method.
You can both bind and start a service, if you wish. It's a bit unusual, but it can be done.
Are there alternatives to this idea?
Binding has nothing in particular to do with services being able to communicate with activities. Using some sort of callback or listener object via binding is a possibility, but it is far from the only one.
You can:
Have the service send a broadcast Intent, to be picked up by the activity
Have the activity send a PendingIntent (e.g., via createPendingResult()) to the service in an Intent extra on the command sent via startService(), to be used by the service to send information back to the activity (or wherever the activity wants it to go, such as a broadcast)
Have the activity pass a Messenger tied to its Handler to the service in an Intent extra on the command sent via startService(), to be used by the service to send information back to the activity
All of those work perfectly well between processes, as well as within a process.
You can use Android Interface Definition Language (AIDL).
You can find an easy to use guide here

How to make a service in this case?

So i have a class that listens to incoming calls, i want to make a service to all my app to receive calls even when i'm not in the application's UI.
How to trigger this class(broadcast receiver). I used "sendBroadcast" and have a FC.
sendBroadcast(new Intent(context, IncomingCallReceiver.class));
Thank you for your help.
In your case I would use the following approach:
Create Service and start it from your Activity (you mentioned that you have several applications, so starting first of them may also start the Service).
Make sure that Service does not work forever, so stop the service when you do not need it any more (last of your applications is finished). Service may terminate self even without Activity by calling stopSelf(). Please note that also System may terminate your Service and prevent it from working forever.
Make private class within Service that extends BroadcastReceiver and register it for the Intents you want to monitor using Service function registerReceiver().
Once you receive wanted Intent, you can call some Service function from within BroadcastReceiver onReceive(). For example, you may sendBroadcast() using some custom Intent that is recognized by your applications.
When Service is stopped make sure that you un-register the BroadcastReceiver extension using Service function unregisterReceiver().
UPDATE:
Service building guidelines
SDK example that illustrates BroadcastReceiver extension usage within Activity. All important related to register/unregister is the same if you do that within Service: android-sdk-windows\samples\android-8\Home\src\com\example\android\home\Home.java
Start Activity from Service
Handle the case of Activity already running in the background

Activity synchronization

My application has one service and a set of activities.
Each activity covers a little task (such as ask the user to insert a number, or a text, or to express a preference).
The service starts the activities or a sub set of the activities.
The order in which the service starts the activities changes with day hours.
But every time, the service has to wait the end of an activity (to obtain the activity result) before starts the next activity.
My idea was to use a wait() call in the service between two activities execution.
Each activity uses a sendBroadcast to return the result to a BroadcastReceiver.
The BroadcastReceiver executes the notify() to free the service and forwards the activity result to the service.
Obviously, this idea does not work. The problems are:
1) Starting many activities from a service gives me problem related to the Task each activities belongs to.
2) I don't know how to pass information from the BroadcastReceiver to the Service (in a first time I declared the BroadcastReceiver inside the Service class, but when the service entered the wait() the process remained blocked and BroadcastReceiver never receives).
Please, I accept all kind of suggestions. Maybe a change in the application architecture?
Thanks
The flow is typically one activity triggering the next. You should re-architect so that when you are ready to finish() activity1, you already know what Activity2 is and you start it from Activity1. Perhaps your service should expose a method that your activities can call to get a determination which should be the next activity. It is OK to do so as Services and Activities run in the same process and can call each other.

Notify activity from service

I'm trying to start a Service from my Activity to look out for changes on a web page, it's a private app so I don't bother the battery life...
But I'd like to pass data from my Service to my Activity... I can't seem to find a way to call the Activity from my Service. How can I achieve this?
As Alex indicated, you can bind to the service and pass some sort of listener or callback to the service to use on events.
Or, you can use a broadcast Intent, perhaps using methods like setPackage() on the Intent to limit the scope of the broadcast.
Or, you can use createPendingResult() to create a PendingIntent that you pass as an Intent extra to the service -- the service can then use that PendingIntent to trigger onActivityResult() in your activity.
Or, you can use a ResultReceiver.
Or, you can use a Messenger.
(admittedly, I have not tried those latter two approaches, but I think they will work here)
One more alternative: if your service updates content provider, activity can be notified via ContentObserver. This would be enough if your service downloads some data from server and you simply want to display fresh contents in the activity.
Some ugly ways:
1.) If the activity has not started yet, then use intent and startActivity, but remember intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2.) Otherwise if the activity has started already, you can write your own callback method in the activity and register the method in the service, then direct call the method in the service.
Hope to find some smart way.
I think broadcast also work well, you can write a static inner class for receive broadcast and start activity. But it is also ugly in my opinion.
The ResultReceiver mechanism has been explained in another post :-
Restful API service
However it would not work in all cases. Please refer to my comment on that post. The limited scope broadcast or PendingIntent mechanism seem more suitable.

Categories

Resources