I have an application with several activities and I want to be notified when the phone goes online (doesn't matter which activity is the user in). I've found a Broadcast Receiver to do it, but I'd like to know if there is a way to declare this BroadcastReceiver only once, I want to avoid having to place the code in each activity (I have more than 20 activities!).
Thank you.
Create a parent abstract class which extends Activity and define the broadcast receiver implementation there. Later, modify your activity classes to extend that parent class.
As in documentation, If we declares the broadcast receiver in manifest file then it automatically runs behind.
Thing is that now you need to handle the onReceive() method of the receiver.
Another way is that you make a separate java class and import wherever you want.
Related
Is there any way how to get a reference for a BroadcastReceiver defined in Manifest.xml from code?
In my case, we are using a BroadcastReceiver that needs to be included in the Manifest.xml. However it has functionality I would like to reuse from within our code. So instead of creating another similar BroadcastReceiver and instantiating it from the code I would like to obtain a reference to the existing one.
Additional information:
My goal is to subscribe to an event on my BroadcastReceiver from my activity - an event that I would like to reuse - instead of creating another instance of this receiver in my activity I would like to obtain a reference to the existing one.
When registering a BroadcastReceiver in the manifest, you're registering the class, not an instance of it. Each time a broadcast occurs that your <receiver> needs handle, a new instance is created to do so, so you can't really get a reference to one as you're describing.
It's perfectly fine to dynamically instantiate and register an instance of a Receiver class that you've also statically registered in the manifest. I would note, though, that if the statically registered class is going to be run anyway - that is, if it's going to handle the same broadcasts as the dynamically registered one - you might consider just notifying your Activity from the Receiver class - e.g., with LocalBroadcastManager, another event bus implementation, etc. - instead of essentially duplicating Receivers.
There's no need to 'obtain a reference' to BroadcastReceiver which is already registered.
Just send Intent which can be handled by that BroadcastReceiver to trigger its action from any point of the code where you have a Context.
context.sendBroadcast(intent);
If you want to call 'pure logic' without calling BroadcastReceiver you have to extract logic from it to some POJO class and call that class directly omitting BroadcastReceiver.
class LocationReceiver extends BroadcastReceiver {
private SomeAction action;
public LocationReceiver(){
action = new SomeAction();
}
#Override
public void onReceive(Context context, Intent intent) {
action.execute();
}
};
BroadcastReceiver can simply call execute() but it doesn't know anything about how it works.
You can reuse SomeAction anywhere in your code without having a knowledge about BroadcastReceiver at all.
Try to avoid putting a logic inside Android classes.
It's better to have logic in POJO Java classes because it helps to keep SRP principle alive and makes testing easier.
I have a extended broadcastreceiver class that listens for bluetooth connection/disconnection. I want it to change a color of some text in my GUIActivity. I don't have it as an inner class on purpose: to keep the GUI code more manageable/modular.
I know of one way to do this: register the receiver dynamically and pass it in the context of the activity. Then do the normal registering/unregistering in onResume and onPause. This solution can be seen at this post
However, I was hoping I could eliminate just a little bit more code by having my receiver registered in the manifest and not worry about registering/unregistering.
I have tried casting the context in the onReceive on the Broadcastreceiver as follows
((SmokinoGUI) context).indicateBTConnection();
This throws an exception saying that context cannot be cast to SmokinoGUI. indicateBTConnection() is a method in the SmoinoGUI activity that does what it says.
So, is there a way to call a method in an activity from a broadcastreceiver that has been registered in the manifest and has not been dynamically instantiated?
So, is there a way to call a method in an activity from a broadcastreceiver that has been registered in the manifest and has not been dynamically instantiated?
There is lot of ways. My favorite is:
Extends Application class and assign that class to your Application name attribute in Manifest.
Add instance of your Activity in that class and create getter and setter to it, that you could reference to in Activity onCreate() .
Get application in your BroadcasetReceiver and call the getter to the Activity.
If all this sound complicated, well, it really not, and it good logic to use in every app. I could add some code for example.
I am new to android programming. I want to create a very simple widget in which I just have a single button and on click of that button, I want to execute some code.
My question is, is it necessary to create an activity for this?
Or can I just extend AppWidgetProvider class and write execution code in onUpdate or onReceive method?
Yes it is necessary to create a class that extends activity to accomplish interaction with a button.
In theory the provider is enough to handle appwidgets. In practice you will always need some kind of configuration, which is an Activity.
It is also very likely that the code to run, is too much for onUpdate and onReceive. At this moment you'll send Intent's out to some Actvity or Service.
I have a utility class that needs to be able to receive broadcast intents from another class. This utility class does not extend either Activity or Service. How do I instantiate and register the broadcast receiver in this class?
Because the class is not an Activity class, there is no onCreate or onDestroy callback methods. The only place to put register receiver is in the constructor, but there is no onDestroy method so i cannot find a place to put the unregister receiver call.
Pass the utility class an instance of your application Context and you can use it to register receivers. If you need to run code that responds to an intent, you must use and Activity or Service. That's Android.
I don't know of a way to do this. You are essentially asking how to instantiate a class that needs to rely on the activity lifecycle without relying on the activity lifecycle. I suggest you refactor to have your code get called in onCreate() and onDestroy() instead. Keep in mind though, that Activities can be created and destroyed at any time. If you are using multiple Activities in your application, you will run into problems receiving broadcasts as you switch from one Activity to another.
Scenario :
An activity is displayed. It has a text box and a button. If you enter a special code in the textbox and click the button the activity closes after comparing the text logic. Simple! This has been implemented in the OnCLick() of the the activity.
Problem :
I have a library that handles all the the SMS receiver functionalities. The receiver remotely listens for a special incoming SMS. Once the special sms arrives, a library function (closeActivity) should either
finish() the activity(described above) straightaway
imitate the action of entering text + clicking button by getting a reference to the activity somehow?
I am fine with either. which ever is simpler and safer!
I just need to know how/where/what code to write in the library so that it can finish() the activity!
Assumption: The activity is always displayed and the system is not stacking it automatically!
Why dont you try to use intents? esp broadcast intents.. Fire the broadcast intent from library and get it in ur activity /another class registered as intent reciever . You can get the instance of activity in the broadcast reciever easily.
public static <Your activity class> activitySingleTon
and set the singleton in appropriate lifecycle method.
OnCreate(){ activitySingleTon = this; }
and use in broadcast reciever like this
if(<your activity class>.activitySingleTon)
{ <your activity class>.activitySingleTon.finish(); }
Can there be several instances of the Activity?
If not, make it a singleton, then reference it from anywhere with MyActivity.getInstance().
Best regards.