I have two acitivties and want to reset values in them everyday at 00:00.
Question:
Is there any way to link two activities to onReceive method of broadcastReceiver so I can update activities(TextView and EditText) from onReceive?
Create a BroadcastReceiver class and register it in your manifest. Also pass the instances of your activities to the BroadcastReceiver when you declare it so you can know which one you are clearing/using.
Now make your clearing methods public in both of your activities and call them from your BroadcastReceiver using the instances of each activity.
Check THIS tutorial and THIS one on how to use BroadcastReceiver class.
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.
In MainActivity I have few buttons like start,stop and runInBackground..
I am using a service to send location updates..If the phone is restarted then my app will start automatically,I've achieved this by broadcastreceiver.
Now,I need to disable the start button of my app after restarting phone as because service is already started.
Leave the part of checking whether service is running or not,I am checking that using fileInputStream..That is another task..
But,How do I manage the Views which belongs to MainActivity class from a receiver class.
I've tried few things like this::
In MainActivity.java
public static void disable()
{
btnShowLocation.setEnabled(false);
btnStopLocation.setEnabled(true);
runbackground.setEnabled(true);
}
MyReceiver.class
public class MyReceiver extends BroadcastReceiver
{
MainActivity.disable()
}
it was not working..tried another like this:
MainActivity.btnShowLocation.setEnabled(false);
MainActivity.btnStopLocation.setEnabled(true);
MainActivity.runbackground.setEnabled(true);
even this crashing my app..
Now how do I manage button views from BroadcastReceiver..
You could:
Save the state in SharedPreferences (or similar) and query it on Activity's onCreate.
Fire an explicit intent from your receiver to start the Activity and put some extra data into that intent to tell the Activity to disable the button.
Somehow register your Activity with an Application class or a singleton and then call methods on it.
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.
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.