Calling SetContentView() from broadcast receiver

非 Y 不嫁゛ 提交于 2019-12-03 20:15:03
Olegas

You need to put your reciever into InternetActivity class, register it there and use already defined local variables. You need not to create separate public BroadcastReceiver implementation, just do a local one.

Like this:

import android.content.BroadcastReceiver;
import android.content.Context;

public class InternetActivity extends Activity {

    private ImageView image;
    private TextView text;

    private BroadcastReceiver reciever = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
           // do all the checking
           // interact with image and text
        }    
    };
    @Override
    public void onCreate(Bundle state) {
        setContentView(R.layout.....);
        // fill in image and text variables
    }
    @Override
    public void onStart() {
        registerReceiver(receiver, /* your intent filter here */);
    }
    @Override
    public void onStop() {
        unregisterReceiver(receiver);
    }
}

You are nowhere adding the inflated view to your activity content view?!

You should have everything inflated and set as the content view in the onCreate method. Then your broadcast receiver should only be setting the visibility of the selected views.

class MyActivity extends Activity {

  private ImageView wifiIcon;

  public void onCreate() {
    setContentView(...);
    wifiIcon = (ImageView) findViewById(...);
  }

  private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      // ...
      wifiIcon.setVisibility( isWifiEnabled ? View.VISIBLE : View.GONE);
    }
  };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!