Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between BroadcastReceiver and Activity - android

I have a broadcast receiver in my app which is fired every time the user gets an incoming call. Now, when it happens, I need the broadcast receiver to invoke a specific method in a specific activity. Now, I tried to make this method static and therefore available, but something tells me it is a very bad idea.

Accordingly, I tried to instantiate the broadcast receiver inside my activity without declaring it in my manifest but the problem is - when the app is off, the activity is not exist and therefore I can't invoke my method.

So my question is - How can I invoke this method when the broadcast receiver is fired up, without making it "public static"?

Here is my activity code(I have deleted the irrelevant parts)

package com.silverfix.ringo.activities;  import com.silverfix.ringo.R; import com.silverfix.ringo.activities.fragments.DataManagerFragment;  import android.app.ActionBar; import android.app.Activity; import android.app.FragmentTransaction; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter;  public class RingtonesActivity extends Activity{      private DataManagerFragment dataManagerFragment;     private IntentFilter filter;     private BroadcastReceiver phoneCall;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_ringtones);         ActionBar ab = getActionBar();         ab.setDisplayShowTitleEnabled(false);         ab.setDisplayHomeAsUpEnabled(true);         dataManagerFragment = new DataManagerFragment();         FragmentTransaction ft = getFragmentManager().beginTransaction();         ft.add(dataManagerFragment, "DataManagerFragment");         ft.commit();         filter = new IntentFilter();         filter.addAction("android.intent.action.PHONE_STATE");         phoneCall = new BroadcastReceiver() {              @Override             public void onReceive(Context context, Intent intent) {                 dataManagerFragment.act();             }         };         registerReceiver(phoneCall, filter);     } } 
like image 290
David Lasry Avatar asked Jan 22 '15 07:01

David Lasry


People also ask

How do you communicate between service and activity?

This example demonstrates how do I communicate between Activity and Service in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I send data from BroadcastReceiver to activity?

Intent intent = getIntent(); String message = intent. getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme.

How can I communicate between two services in Android?

You have to use BroadcastReceiver to receive intents, and when you want to communicate simply make an Intent with appropriate values. This way you should be able to make a 2-way communication between any component.

What will be used if the user wants to communicate between two activities inside the same application?

Communicating between activitiesIntent intent = new Intent(Activity1. this, Activity2. class);


1 Answers

You can use observers , like

public class MyReceiver extends BroadcastReceiver {     public MyReceiver() {     }      @Override     public void onReceive(Context context, Intent intent) {         ObservableObject.getInstance().updateValue(intent);     } } 

public class MainActivity extends Activity implements Observer {     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          ObservableObject.getInstance().addObserver(this);     }      @Override     public void update(Observable observable, Object data) {                     Toast.makeText(this, String.valueOf("activity observer " + data), Toast.LENGTH_SHORT).show();     } } 

public class ObservableObject extends Observable {     private static ObservableObject instance = new ObservableObject();      public static ObservableObject getInstance() {         return instance;     }      private ObservableObject() {     }      public void updateValue(Object data) {         synchronized (this) {             setChanged();             notifyObservers(data);         }     } } 

Receiver can be used via manifest. ObservableObject - must be singleton.

like image 57
ilw Avatar answered Nov 08 '22 06:11

ilw