Android call methods of activity from broadcast receiver

When you use BroadcastReceiver to receive a broadcast data sent from a server, you might want to call a method of an Activity up on receiving the data. In this Android tip, I am going to show you how to call a method of an Activity when there is an incoming message.



First, you need a class that extends the Observable class. The Observable class has a method called notifyObservers() to notify all registered observer objects of a change that occurred. In this example, there is only MainActity that is registered as observer object. Every observer object must implement the Observer interface, override the update method, and be added to the observers list using the addObserver() method of the Observable class. When an observer object is notified, the update() method will be called. So the method of the observer object that will be called has to be placed in the update() method.
Now to have an example application on calling a method of the activity from a BroadcastReceiver, you create a new Android project. Then, you declare RECEIVE_SM permission and broadcast receiver as shown below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.examples"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
     android:minSdkVersion="8"
     android:targetSdkVersion="21" />
   <uses-permission android:name="android.permission.RECEIVE_SMS"/>

   <application
     android:allowBackup="true"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/AppTheme" >
     <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
           <action android:name="android.intent.action.MAIN" />

           <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     </activity>
      <!-- Declare Broadcast receiver -->
   <receiver android:name=".MyReceiver">
   <intent-filter android:priority="999" >      
     <action android:name="android.provider.Telephony.SMS_RECEIVED" />
   </intent-filter>
</receiver>
   </application>

</manifest>

Then in the src folder, you create two classes: MyReceiver and BroadcastObserver. On the onReceive() method of the MyReceiver class, you call the triggerObservers() method of BroadcastObserver to notify all registered observer objects when there is a new incoming message.

public class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent)
{
Log.i("Reciever","Received SMS");
// trigger change
BroadcastObserver broadcastObserver=new BroadcastObserver();
broadcastObserver.triggerObservers();


}

}

public class BroadcastObserver extends Observable {
public void triggerObservers() {
// Sets the changed flag for this Observable
setChanged();
// notify registered observer objects by calling the update method
notifyObservers();

}

}

The MainActivity class that is a registered observer object must implements the Observer interface, and be added to the observers list. The method of the MainActivity class to be called from the MyReceiver is methodToCall().

public class MainActivity extends Activity implements Observer{

   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     // Add this activity to the observer list
     BroadcastObserver broadcastObserver = new BroadcastObserver();
     broadcastObserver.addObserver(this);
   }
  
   @Override
   public void update(Observable obs, Object obj) {
   methodToCall();
   }
  
   private void methodToCall(){
   //Toast.makeText(this, "A method of activity called",Toast.LENGTH_SHORT).show();
   Log.i("MainActivity","The method is called.");
   }
}

Android-call-methods-of-activity-from-broadcast-receiver

Posted by: Dara | post date: 01-24-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.