Get BroadcastRecevier Object from ResloveInfo - android

I am stuck up just now; issue is my one of activity has register one private broadcastReceiver and I need to unregister it and as usual I cannot change that file.
My approach is get all broadcast receiver information () for that intent by Package Manager.queryBraodcastReceiver method.
Now I want to get broadcast receiver object from it.
Is there any other approach to work out this problem?

Is there any other approach to work out this problem?
Fix the activity to unregister its receiver. You cannot do this from anywhere else in your app.

Related

How to avoid broadcast from being wasted when receiver is registered inside activity?

I have a service which will broadcast a message to broadcast receiver which is registered in some activity. The activity might be running or not. If it's open then broadcast will be received. But if not, the broadcast will be wasted. Is there a way to avoid this broadcast from being wasted without adding the broadcast inside manifest?
Consider to start/stop that service based on Activity's lifecycle, when Activity is created (onCreate()) start that service. When Activity is destroyed (onDestroy()) stop the service so you avoid "waste" those messages and even have a service running.
1- Create an inner class in your activity that extends BroadcastReceiver.
2- Create an object of your receiver and an intent filter.
3- Override onResume and registerReceiver(receiver, intentFilter);
4- Override onPause and unregisterReceiver(receiver);
Take a look at this answer. you dont have to add it in the manifest.
answer here

How get Activity form BroadcastReceiver in onReceive

I define BroadcastReceiver in an application manifest and the receiver receives events in onReceive as expected. However I do not create the receiver class so I can't pass any information about my activity which the receiver is supposed to control. Context parameter gives me only application context and no any activity reference. So what is right way to make communication between application activities and broadcast receiver?
The answer is a little twisted but I found it here as well
Inform Activity from a BroadcastReceiver ONLY if it is in the foreground
It seems working. Please close the question then.
You can communicate broadcast to activity from this:-
Intent intent=new Intent(context,YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Start Activity for result in a Broadcast Receiver?

I have managed to get an Activity to start from my onReceive() methdod, but I really need to do a startActivityForResult();.
Is there any way I could do this?
On a side note, how would I make my app become a 'camera' app, as in it would appear when an app started the intent to take a picture?
The important thing to know about broadcast receivers is that you should not add long running processes in it, because after something like 5 seconds your app will crash.
The best thing to do in your case is to intent to other Activity from your broadcast receiver, and from that activity use startActivityForResult(), get the picture and continue from there...
startActivityForResult can only be called from an Activity since it is defined in the Activity class and require instance of activity.
You can only call startAcivity() from broadcast receiver since in onRecieve() you only have access to generic context object and it does not have startActivityForResult method defined in the class..

Persisting BroadcastReceiver reference

What I want to achieve is to give user a button saying 'Start broadcast receiving' and another one saying 'Stop broadcast receiving'.
I'm registering BroadcastReceiver for "android.provider.Telephony.SMS_RECEIVED" intent ('Start broadcast receiving' functionality):
incomingSmsReceiver = new IncomingSmsReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
getApplicationContext().registerReceiver(incomingSmsReceiver, filter);
Then I'm using unregisterReceiver() for 'Stop broadcast receiving':
getApplicationContext().unregisterReceiver(incomingSmsReceiver);
As you can see it's using the same reference (private static BroadcastReceiver incomingSmsReceiver;).
The problem is:
This works fine as long as my app's process is not terminated. When user click 'Start receiving broadcast' and after that my app is been killed by Android I'm loosing incomingSmsReceiver reference (when I run my app next time it's set to null by default). There's no way for user to stop receiving broadcast as the reference is lost.
How to persist this reference? And how to make it possible to call getApplicationContext().unregisterReceiver(incomingSmsReceiver); after recreating app's process by Android?
I've found better solution for such problem: Enable and disable a Broadcast Receiver (CommonsWare's answer).
The solution is to register BroadcastReceiver in AndroidManifest file. Then to use PackageManager.setComponentEnabledSetting(...) to enable / disable this component.
AFAIK, you don't need to hold on to the exact same BroadcastReciever reference. Create a new reference in the exact same way in which you would create one normally and pass it to unregisterService.

broadcast receiver issue need help?

I created a broadcast receiver. After that in onReceive, I received an information and called a method in the class.
my question is when I call that method I want to call only once but why it calls more than one time?
Are there any way we can determine that onReceive receive only one ???
EDIT
my method that I call in onReceiver is update map location in class extend MapActivity
I also register my broadcast service in manisfest.xml
I want to add comment to the answer but i can't i don't know why
how can we determine to our broadcast receiver to receiver only one??
thanks
you can maintain a bool variable and when you get call first time in the method, make this bool true and next time before calling the method you can put check there using this bool variable.
If you are not registering your receiver in the manifest file then you can use this bool value before registering the receiver.

Categories

Resources