The snippets below works when the activity is at the foreground. However, MainActivity will fail to receive the broadcast when MainActivity is at the background.
How do I make sure that MainActivity will always receive the broadcast? Is there any other way to send data from IntentService to a paused activity?
In my IntentService, I'm sending data back to MainActivity like this
Intent intent = new Intent();
intent.setAction(MY_SERVICE_RESULT);
intent.putExtra(MY_SERVICE_VALUE, "hello from service");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
In my activity, I'm using BroadcastReceiver like this
public class MainActivity extends AppCompatActivity{
BroadcastReceiver receiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String value = intent.getStringExtra(MY_SERVICE_VALUE);
}
};
#Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MY_SERVICE_RESULT);
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, intentFilter);
}
#Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}
}
}
How do I make sure that MainActivity will always receive the broadcast?
You can't.
However you could work around it by storing the data that the IntentService wants to send to the MainActivity somewhere temporarily (e.g. SharedPreferences), and have MainActivity check that storage in onResume.
Possible scenario
IntentService does its thing. Checks if MainActivity is active*. If it is -> send broadcast. If it's not -> store somewhere and let MainActivity check that storage in onResume.
*you can use Application's lifecyclecallbacks to monitor activity's states to see if an activity is running and if so which one.
Related
I am working on music player app and I have included the basic functionality like Play, Pause, next and Previous tracks using service class.
Now I want to update myrecyclerview UI according to the action that user clicks(eg: on the play, there should be an image of the selected element in recyclerview).
I thought to use Broadcast Receiver that service class will broadcast on different actions and then recyclerview can update according to the broadcast action. But how do I add such functionality?
You'd need an in-activity broadcast receiver. Such receivers are registered when your activity is shown and unregistered once it's hidden.
public class MainActivity extends Activity {
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Here update your RecyclerView
}
};
#Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(YourService.YOUR_CUSTOM_ACTION);
registerReceiver(mReceiver, filter);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
And in your service, simply send broadcasts:
Intent intent = new Intent(YOUR_CUSTOM_ACTION);
// put extras in the intent as you wish
sendBroadcast(intent);
I am using broadcast receiver in my app to detect incomming call and it works fine. But problem is I can not send action to activity. I mean.. I want do something in activity not in receiver. I read many tutorial but they all are performing action in receiver. Any idea ?
You can declare a BroadcastReceiver as inner class of the Activity. In this case you can directly call activity's methods:
public class MyActivity extends Activity {
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
activityMethod();
}
};
private final IntentFilter filter = new IntentFilter("android.intent.action.PHONE_STATE");
#Override
protected void onStart() {
super.onResume();
registerReceiver(receiver, filter);
}
#Override
protected void onStop() {
super.onPause();
unregisterReceiver(receiver);
}
private void activityMethod() {
}
}
You can start the Activity using an Intent and put a command code in the Intent extra fields. In your Activity you can then decide the behaviour based on the command code or resort to a default behaviour if none is present.
You can start an activity from your receiver via the normal means:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, YourActivity.class);
startActivity(i);
}
Note though that the user is going to expect that the phone application starts up since they are receiving a phone call. It is very likely a bad idea to hijack the phone call by dumping your own activity on top of the stock dialer app.
I have a main activity that launches:
1.- A network prone thread that writes into a socket.
2.- A network prone service that is supposed to read from a socket.
So far I'm done with 1. but I want the information read from the socket to be shown in the main activity. I know I can pass information between the activity and the service using extras but how can I tell the activity to update and get the new data?
I guess that you could use broadcasting intents combined with a BroadcastReceiver in your main activity in order to achieve background communication.
Here's a snippet that can achieve this.
(CODE PUT IN THE ACTIVITY):
class MyActivity extends Activity{
CustomEventReceiver mReceiver=new CustomEventReceiver();
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/*YOUR ONCREATE CODE HERE*/
/*Set up filters for broadcast receiver so that your reciver
can only receive what you want it to receive*/
IntentFilter filter = new IntentFilter();
filter.addAction(CustomEventReceiver.ACTION_MSG_CUSTOM1);
filter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(mReceiver, filter);
}
#Override
public void onDestroy(){
super.onDestroy();
/*YOUR DESTROY CODE HERE*/
unregisterReceiver(mReceiver);
}
/*YOUR CURRENT ACTIVITY OTHER CODE HERE, WHATEVER IT IS*/
public class CustomEventReceiver extends BroadcastReceiver{
public static final String ACTION_MSG_CUSTOM1 = "yourproject.action.MSG_CUSTOM1";
#Override
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals(ACTION_MSG_CUSTOM1)){
/*Fetch your extras here from the intent
and update your activity here.
Everything will be done in the UI thread*/
}
}
}
}
Then, in your service, you simply broadcast an intent (with whatever extras you need)... Say with something like this:
Intent tmpIntent = new Intent();
tmpIntent.setAction(CustomEventReceiver.ACTION_MSG_CUSTOM1);
tmpIntent.setCategory(Intent.CATEGORY_DEFAULT);
/*put your extras here, with tmpIntent.putExtra(..., ...)*/
sendBroadcast(tmpIntent);
One option could be to write the output of the socket reader to a stream - a file stored in the app's internal storage for example, and then periodically poll that file in the activity thread.
I have a class that extends BroadcastReceiver that reads new sms
public class SmsReceiver extends BroadcastReceiver
{
// reading sms
// I want to send the sms text to my main activity
}
And have another class in the same app that is my main Activity.
So when I receive new sms, I want to send its content to my main Activity that is already running and display it.
How can I do that?
I would be thankful for some code samples :)
i can suggest you two possibilities
send new broadcasts from this receiver to a new receiver which is registered inside your activity
register this receiver inside your activity and reduce the hassle
i guess option two is more suitable
this is how you may register a broadcast receiver inside your activity class:
IntentFilter filter = new IntentFilter();
public void onResume(){
filter.addAction("action_string_1");
filter.addAction("action_string_2");
registerReceiver(receiver, filter);
}
public void onPause(){
unregisterReceiver(receiver);
}
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("action_string_1")){
//do something here
}
else if(action.equals("action_string_2")){
//do somethign here
}
}
};
When i have a broadcastReceiver say android.intent.action.MEDIA_BUTTON and i want to update the current activity's UI without creating a new activity, is there any good practice on this one?
What i know (might not be correct)
1) I can put the BroadcastReceiver in the same class as the activity and call the updateUI function after certain activity
2) Create a ContentObserver?
3) Communicate to a service created by the activity, use aidl. (I dont know how to get the current service if its registered from an activity)
4) Create a custom filter on the broadcastReceiver located on the same class as the activity, and use context.sendBroadcast(msg of custom filter) and in the custom filter call updateUI (same as one but more generic?)
The final flow is it would come from a BroadcastReceiver and ends up updating the UI without renewing the activity (unless the activity is dead?)
Kindly provide links/source code on your how you tackle this kind of problem. Thanks a lot in advance :)
The easiest way to provide this functionality is to put the broadcast receiver in you Activity and bind / unbind it using registerReceiver and unregisterreceiver:
public class MyActivity extends Activity {
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
MyActivity.this.receivedBroadcast(intent);
}
};
#Override
public void onResume() {
super.onResume();
IntentFilter iff = new IntentFilter();
iff.addAction("android.intent.action.MEDIA_BUTTON");
// Put whatever message you want to receive as the action
this.registerReceiver(this.mBroadcastReceiver,iff);
}
#Override
public void onPause() {
super.onPause();
this.unregisterReceiver(this.mBroadcastReceiver);
}
private void receivedBroadcast(Intent i) {
// Put your receive handling code here
}
}
Depending on the intent you wish to receive, you may need to add the appropriate permissions to your AndroidManifest.xml file.
What I recently had to do to change a Button's text after receiving data from a LocalBroadcastManager is to store the value in a private field and then do the UI stuff in my onResume() method.
public class myClass extends Activity {
private String myString;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// register to receive data
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, new IntentFilter("myAction"));
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// get the extra data included in the intent
myString = intent.getStringExtra("myString");
}
};
#Override
public void onResume() {
super.onResume();
System.out.println("onResume");
// do something to the UI
myButton.setText(myString != null ? myString : "Default");
}
}