Accessing R from a BroadcastReceiver - android

I want to change a TextView in the onReceive method of my BroadcastReceiver, but I can't access it with findViewById because it's not an activity.
I don't want to create a private class for the BroadcastReceiver in my activity.
How can I get access?

Define an interface and use a callback to let the activity know that a broadcast event has been received.
public Interface BroadcastReceiverListener {
void onReceive(int arg1, String arg2); ..<----add arguments you want to pass back
}
In your BroadcastReceiver class
ArrayList<BroadcastReceiveListener > listeners = new ArrayList<BroadcastReceiveListener >();
...
public void addBroadcastReceiveListener (BroadcastReceiveListener listener){
if(!listeners.contains(listener)){
listeners.add(listener);
}
}
public void removeBroadcastReceiveListener (BroadcastReceiveListener listener){
if(listeners.contains(listener)){
listeners.remove(listener);
}
}
In your OnReceive
for (BroadcastReceiveListener listener:listeners){
listener.onReceive(arg1, arg2);
}
In your Activity:
public class MyActivity extends Activity implements BroadcastReceiveListener {
...
broadcastReceiver.addBroadcastReceiveListener(this); <---- the instance of your receiver
...
}
public void onReceive(int arg1, String arg2){
// do whatever you need to do
}
Note. Because you use an interface, any class (not just an Activity) can implement it so you can update anywhere in your app. The BroadcastReceiver class doesn't know or care. It just calls the listeners, if any are registered.
Now, you don't need to access R, or anything to do with the UI since your Activity is the only class that knows about, and can change, your UI - which is the Android way!
[EDIT]
The arguments are whatever you need them to be.
Think of the Interface as a contract. It says that anyone who implements it, must implement the onReceive() method and that the method will be called with an integer and a String. It's up to you what arguments you need, if any.
BroadcastReceiver.onReceive() calls the onReceive callback of the interface and passes in the int and String as arguments.
You could change the Interface definition to pass a bool for example.
public Interface BroadcastReceiverListener {
void onReceive(boolean arg1); ..<----add arguments you want to pass back
}
Then your caller looks like this:
for (BroadcastReceiveListener listener:listeners){
listener.onReceive(someBooleanValue);
}
And your callback looks like this:
public void onReceive(boolean theCallerIsReady){
if(theCallerIsReady){
// do interesting stuff
}
}

Related

Notify Activity from onClick of View object

I seem to be stuck with a problem with an object communicating with my activity class. The object is a view object with an onClick method that when called I would like it to notify my activity class so that it can perform said action. Below is some example code of my situation (assume all conventional setup operations have already been made):
public class MainActivity extends AppCompatActivity{
//...other global methods and objects
//Does not have access to instantiated Entry object(s)
public void entryObjectWasClicked(){
//perform said action
}
}
public class Entry extends View implements View.OnClickListener{
//...other global methods and objects
//Does not have access to the MainActivity object
#Override
public void onClick(View v){
//send a message to the MainActivity to
//somehow call the entryObjectWasClicked() method
}
}
The only way (off the top of my head) that I could think about dealing with this problem is by creating a static method in MainActivity and then calling it from an anonymous MainActivity object in the onClick method of Entry. The problem with the static method approach is that any subsequent method/object/primitive usages in the static method force those methods/objects/primitives to be static. This defeats the purpose of then being able to have two different instances of the MainActivity object.
After some looking I came across using Broadcast messages, specifically using the LocalBroadcastManager to send an intent to the activity. This code example works for my model, but I want to know: is this the best way for me to go about sending messages to my MainActivity from my Entry object?
If there is a more effective way of doing all this, what would it be?
You're overcomplicating things. Don't override onClick for this. Instead, have your activity call setOnClickHandler on your view, which sets a callback that's called when the view is clicked. Then use the default implementation.
Since you extend view, i guess you want to use it inside a layout. That means you may want to create a Listener for that. Example:
public class Entry extends View implements View.OnClickListener{
private OnClickListener listener;
public void setListener(OnClickListener listener) {
this.listener = listener;
}
#Override
public void onClick(){
if (this.listener != null) this.listener.onClick(this);
}
}
How you can inflate your layout in your Activity and access your custom view.
public class MainActivity extends AppCompatActivity{
public void onCreate( ...) {
Entry entry = findViewById(R.id.entry);
entry.setListener(new OnClickListener(...));
}
}

Can I use a callback method from a BroadcastReceiver?

Learning to use the BroadcastReceiver class in Android, I have written a small program to receive the battery charge state and write it to three TextView fields in an activity.
However, I have made the BroadcastReceiver as a separate class to make it more simple and separate from the activity. Therefore I have to find a method to tell my Activity class that the battery data has been updated, or, which is my solution, to pass in references to the TextView fields from the Activity to the BroadcastReceiver class.
Does anyone know whether it is possible to make a callback method from the BroadcastReceiver to start a function, f.ex. updateTextViews(); in the Activity?
Here is the source code - note there are two java files:
http://pastebin.com/qjCTsSuH
Regards, Niels.
What worked a charm for me is simply declaring the interface objects as static. Bear in mind though that statics can cause as many problems as they solve as statics persist therir values accross instances.
public class MainActivity extends AppCompatActivity implements SocketMessageReceiver.ISocketMessageReceiver {
//Declare the cb interface static in your activity
private static SocketMessageReceiver.ISocketMessageReceiver iSocketMessageReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//Assign this
iSocketMessageReceiver = this;
socketMessageReceiver.registerCallback(iSocketMessageReceiver);
}
#Override
public void sendSocketMessage(String socketMessage) {
lblEchoMessage.setText(socketMessage);
}
}
And in your Receiver ....
public class SocketMessageReceiver extends BroadcastReceiver {
interface ISocketMessageReceiver {
void sendSocketMessage(String socketMessage);
}
//Also declare the interface in your BroadcastReceiver as static
private static ISocketMessageReceiver iSocketMessageReceiver;
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("com.WarwickWestonWright.SocketExample.RECEIVE")) {
iSocketMessageReceiver.sendSocketMessage(intent.getBundleExtra("DATA").getString("DATA"));
}
}
public void registerCallback(ISocketMessageReceiver iSocketMessageReceiver) {
this.iSocketMessageReceiver = iSocketMessageReceiver;
}
}
I have made the BroadcastReceiver as a separate class to make it more simple
IMHO, you made it more complex.
Therefore I have to find a method to tell my Activity class that the battery data has been updated, or, which is my solution, to pass in references to the TextView fields from the Activity to the BroadcastReceiver class.
Option #1: Just go back to using an inner class for the BroadcastReceiver. ACTION_BATTERY_CHANGED can only be used via registerReceiver() anyway. Just have onReceive() call some method on the activity to do the work of updating the UI.
Option #2: Pass your activity into the constructor of the BroadcastReceiver, and call the method as in option #1.
Option #3: Use an event bus, like Square's Otto or greenrobot's EventBus.

Passing Android Acceleromter values from MainActivity to My GameView class

I am working on a game involving measuring accelerometer values and manipulating my character's position based on those values. I am only working with the X axis. I initiate my accelerometer manager in my main activity, but I need to use these values in another class. Obviously these values are constantly changing. How could I pass these values to my GameView class?
You can save them as static members of your activity, or pass them to GameView class each time there's a change in them (new value)
If you've used a sensorlistener you might think about moving that logic to the GameView class. Not totally sure without looking at your code though. Do you have any samples?
Why not do it the Android way? :)
When you implement a touch listener, you do something set like this:
myView.setOnTouchlistener(this):
This tells the view to call you back when it's touched. Here's how to do your own:
Define an interface and use a callback to let the class know that a value has changed.
public interface AccelerometerUpdateListener {
void onUpdate(int arg1, string arg2); ..<----add arguments you want to pass back
}
In your Activity:
public class MainActivity extends Activity implements AccelerometerUpdateListener {
ArrayList<AccelerometerUpdateListener > listeners = new ArrayList<AccelerometerUpdateListener >();
...
#Override
public void onCreate(Bundle savedInstanceState)
{
...
myGameClass.setResponseReceivedistener(this);
...
}
public void setUpdateListener(AccelerometerUpdateListener listener){
if (!listeners.contains(listener){
listeners.add(listener);
}
}
public void removeUpdateListener(AccelerometerUpdateListener listener){
if (listeners.contains(listener){
listeners.remove(listener);
}
}
When you update the values
for (AccelerometerUpdateListener listener:listeners){
listener.onUpdate(arg1, arg2);
}
In your receiving class:
public class MyGameClass implements AccelerometerUpdateListener {
...
#Override
public void onUpdate(int arg1, string arg2){
// do whatever you need to do
}
All from memory so please excuse typos.

How to notify an activity when GlobalVariables are changed

I have an android application that is connected to the computer via USB cable. I use a TCPServer Class to send messages and listen. For example:
When I send a message like: request:x
I get the response: response:x:55
I need to make changes on my activity according to the response I get. At the moment I temporarily solved the problem by passing activity and activity class object to the TCPServer's constructor
public TCPServer(int portNum, Activity activity, IntroActivity ia) {
super();
port = portNum;
this.activity = activity;
this.ia = ia;
}
Then after I receive the response:
void updateButton(final int color, final String txt) {
activity.runOnUiThread(new Runnable() {
public void run() {
ia.getConnectionButton().setBackgroundColor(color);
ia.getConnectionButton().setText(txt);
}
});
}
As you see, this is not effective at all. I need to somehow notify the activity whenever a relevant variable is received. I use a Class for GlobalVariables and change those static variables after listen(), however I am having troubles notifying the activity.
First of all, it is almost always bad practice to pass Activity instances around. This is a time when it's bad.
Define an interface and use a callback to let the activity know that a response has been received.
public interface ResponseReceivedListener {
void onResponseReceived(int arg1, string arg2); // <- add arguments you want to pass back
}
In your TCPServer class:
ArrayList<ResponseReceivedListener> listeners = new ArrayList<>();
// ...
public void setResponseReceivedListener(ResponseReceivedListener listener) {
if (!listeners.contains(listener) {
listeners.add(listener);
}
}
public void removeResponseReceivedListener(ResponseReceivedListener listener) {
if (listeners.contains(listener) {
listeners.remove(listener);
}
}
When you receive a response:
for (ResponseReceivedListener listener : listeners) {
listener.onResponseReceived(arg1, arg2);
}
In your Activity:
public class MainActivity extends Activity implements ResponseReceivedListener {
// ...
#Override
public void onCreate(Bundle savedInstanceState) {
// ...
tcpServer.setResponseReceivedListener(this);
// ...
}
public void onResponseReceived(int arg1, string arg2) {
// do whatever you need to do
}
// ...
}
All from memory so please excuse typos.
This approach decouples the classes. The TCP Server has no knowledge of the activities. It simply calls back to any listeners registered. Those listeners might be Activities, they might be services. They might be instances of MySparklyUnicorn. The server neither knows nor cares. It simply says "if anyone's interested, I've received a response and here are the details".

How can I call a function in my main Activity class from a custom Gallery view in Android?

I have a custom gallery view in which I am overriding some methods. I would like to be able to call a function in my main activity from this class. How do I make a reference back to my main class?
I thought I'd just push the class reference into CustomGallery by creating a setter function ---> g.setBaseClass(this);
CustomGallery g = (CustomGallery) findViewById(R.id.playSelectionGallery);
g.setSpacing(10);
g.setCallbackDuringFling(false);
g.setAdapter(new ImageAdapter(this));
g.setSelection(1);
registerForContextMenu(g);
g.setBaseClass(this);
Problem is this is of type Context and someFunctionToCall() will result in a not a member of this class error. In my custom class I have:
public void setBaseClass(Context baseClass)
{
_baseClass = baseClass;
}
private void callSomeFuntionOnMyMainActivityClass()
{
_baseClass.someFunctionToCall();
}
All I want to do is call back to my main class, called ViewFlipperDemo. This would be easy in As3. Any thoughts? Hopefully I'm missing something really simple.
That's actually not a good idea... but you can do it this way:
private void callSomeFuntionOnMyMainActivityClass()
{
((ViewFlipperDemo)_baseClass).someFunctionToCall();
}
What you should do instead is implementing a simple observer which allows you to notify the Activity that something happened. That's one of the main OO principles, your custom class shouldn't know anything about your activity class.
Observer pattern example
The Observer interface:
// TheObserver.java
public interface TheObserver{
void callback();
}
Your custom view:
public class CustomGallery{
private TheObserver mObserver;
// the rest of your class
// this is to set the observer
public void setObserver(TheObserver observer){
mObserver = observer;
}
// here be the magic
private void callSomeFuntionOnMyMainActivityClass(){
if( mObserver != null ){
mObserver.callback();
}
}
// actually, callSomeFuntionOnMyMainActivityClass
// is not a good name... but it will work for the example
}
This is the activity that will benefit of the observer (notice that now you can use your custom view on different activities not just one, that's one of the key reasons to implement it this way):
public class YourActivity extends Activity{
// your normal stuff bla blah
public void someMethod(){
CustomGallery g=(CustomGallery)findViewById(R.id.playSelectionGallery);
g.setObserver(new TheObserver(){
public void callback(){
// here you call something inside your activity, for instance
methodOnYourActivity();
}
});
}
}
You will notice that this design pattern (observer) is widely used in Java and Android... almost any kind of UI event is implemented using observers (OnClickListener, OnKeyListener, etc.). By the way, I didn't test the code, but it should work.

Categories

Resources