Calling an Activity method from the BroadCastReceiver class without creating an instance of Activity

I want to do the same as indicated in the question in the following link. Call the action method from the BroadcastReceiver class . But the solution given there does not work for me, and I can not comment there because I do not have enough reputation. I did everything that is mentioned in the solution, but my broadcast receiver does not work with this code.

I just want to call a method in action when I receive a broadcast request without creating an instance of the action. It can be done?

EDIT: Now I am adding code.

Here is my MainActivity class

public class MainActivity extends Activity {

    public static final String TAG="App1";
    private ActionReceiver receiver = null;

    //private YourBroadcastReceiverClassName yourBR = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // receiver= new ActionReceiver();
        //IntentFilter filter = new IntentFilter();
        //filter.addAction("com.example.app1.printSomething");

        // printSomething();

    }

    public void printSomething(){
        Toast.makeText(this,"Hello World",Toast.LENGTH_LONG);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

  }

This is my ActionReceiver class, which is BroadcastReceiver

public class ActionReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if("com.example.app1.printSomething".equalsIgnoreCase(intent.getAction()))
            Toast.makeText(context,"Hello World", Toast.LENGTH_LONG).show();
    }
}

This is my file AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.app1.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>
        <receiver android:name=".ActionReceiver">
            <intent-filter>
                <action android:name="com.example.app1.printSomething"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

, printSomething() , MainActivity onReceive() ActionReceiver . MainActivity . ?

+4
5

MainActivity, . . :

((MainActivity) getActivity()).printSomething();

MainActivity:

public void printSomething() {
      doStuff();
}
+1

, .

, .

EDIT:

, . , u . , static.

:

public static void printSomething(){
    Toast.makeText(this,"Hello World",Toast.LENGTH_LONG);
}

:

@Override
    public void onReceive(Context context, Intent intent) {
        if("com.example.app1.printSomething".equalsIgnoreCase(intent.getAction()))
          MainActivity.printSomething();
    }
0

, , .

EventBus , Otto GreenDroid... , , :)

0

Activity (.. Activity).

1) CallBack

public interface ReceiverCallback{

    public void doSomeTask();

} 

2)

public class ActionReceiver extends BroadcastReceiver{

    private ReceiverCallback callback; 

    public ActionReceiver(ReceiverCallback callback){
        this.callback = callback;         //<--- Initialse callback
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        callback.doSomeTask();

    } 
}

3) doSomeTask() doSomeTask()

public class MainActivity extends Activity implements ReceiverCallback{

    // Activity related Code //


   public void printSomething(){
    Toast.makeText(this,"Hello World",Toast.LENGTH_LONG);
   }

   @Override
   public void doSomeTask(){
       printSomething()        //<-- call your method
   }

?

BroadcastReceiver () . BroadcastReceiver BroadcastReceiver. .

Relevant link:

If you want to read an even deeper explanation, you can READ THIS

0
source

You can do this in the BroadcastReceiver class: -

   MainActivity mainActivity;

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        mainActivity= new MainActivity();
        mainActivity.printSomething();



    }
-3
source

All Articles