1

In my broadcast receiver, I'm accessing a method that calls another method which is in my MainActivity class. The method from my MainActivity class uses variables that are set in the onCreate() method.

The problem I'm having is that when the broadcast receiver tries to access this method when the app has been closed, it finds the variables null because the onCreate method hasn't ran to set the variables.

Should I try to trigger the onCreate method from my broadcast receiver, or do I have this whole setup wrong? I tried to condense the code a bit so it's not too long but below you can see the gist of what I'm trying to do.

The error I'm getting is that "text" and "ringer" are null when trying to setText. This only happens when the broadcast receiver runs while the app is closed and not running.

WifiScanCompleteReceiver code:

public class WifiScanCompleteReceiver extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {
        ...
        }    
        public static void activate() {
            ...
            MainActivity.statusText();
        }
}

Activity code:

public class MainActivity extends Activity  {

    protected void onCreate(Bundle savedInstanceState) {
            ...

            setContentView(R.layout.activity_main);

            text = (TextView)findViewById(R.id.state);
            ringer = (TextView)findViewById(R.id.mode);
            state = mSettings.getString("state", "init");
            mode = mSettings.getInt("ringer", 0);
            statusText();
    }

    public static void statusText() {
            text.setText(state);
            if (mode == 1) {
                ringer.setText("Vibrate");
            } else if (mode == 2) {
                ringer.setText("Normal");
            } else {
                ringer.setText("Unkown");
            }
        }
}
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Oliver84
  • 307
  • 2
  • 14
  • Are the null in onCreate()? – Peri Hartman Jan 03 '17 at 23:01
  • If onCreate runs, it's not null. But when my onReceive runs after the app is closed, onCreate hasn't run so it's null. – Oliver84 Jan 03 '17 at 23:09
  • I'm not sure what you want. Obviously if the app is closed, those views won't exist. Are you intending that the app launches spontaneously when the broadcast receiver calls statusText()? I don't know if this is possible from a B.R. but you might try calling startActivity(). – Peri Hartman Jan 04 '17 at 05:02
  • Instead of startActivity, I ended up going with startService and left the textview in the main activity. I didn't need the views to show up every time the BR received something, I just needed to update some states that the view used in case the user opens up the app. Thank you! – Oliver84 Jan 04 '17 at 05:07

3 Answers3

0

Instead of accessing the TextView from the receiver, I just triggered the main activity as a service with context.startService(Intent); and that seemed to get me what I wanted.

I needed to trigger the code in my main activity to run even if the main activity had been stopped. So this solved that for me.

Oliver84
  • 307
  • 2
  • 14
0

You can solve this using interface: 1) Create an interface

interface MyListener {
    public void doSomething();
}

2) Initialize the Listener in BroadcastReceiver

public class WifiScanCompleteReceiver extends BroadcastReceiver {

    private MyListener listener;

    public void onReceive(Context context, Intent intent) {

          listener = (MyListener)context; 
          listener.doSomething();   // Call interface method

    }         
}

3) Implement interface in the Activity and override the method

public class MainActivity extends Activity  {

      // Your Activity code 

    public static void statusText() {
        text.setText(state);
        if (mode == 1) {
            ringer.setText("Vibrate");
        } else if (mode == 2) {
            ringer.setText("Normal");
        } else {
            ringer.setText("Unkown");
        }
    }

    @Override
    public void doSomething(){
        statusText();
    }

 }

Relevant Link:

If you want to read the advantage of using interface Read this

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
0

Its not a good approach to call activity's method directly from the receiver. App will crash in a case when your activity is not visible, but due to receiver's call it will try to execute activity's code.

You can use local broadcast here. Instead of calling activity's method from receiver send local broadcast, which you need to register in your activity and in receiver of local broadcast call your activity method. This method(Activity's method) call from local broadcast will only execute when your activity will be visible and will not result in app crash.

Dhruv
  • 91
  • 7