开发者

Calling SetContentView() from broadcast receiver

In my application there are two class one is InternetActivity which only extends Activity and sets contentview to main. and MyClass that extends broadcast receiver.

I have 2 TextView and 2 ImageView of WIFI and GPRS in main.xml file. When changes in connectivities are happening,brodcast receiver is getting called and according to what is enabled and what is not i want to set visibility of TextView and ImageView. But it is only showing both the images and not the changes. here is MyClass.java file. how can i do it??

public class MyClass extends BroadcastReceiver {

private static ImageView wifi_image, gprs_image;
private static TextView wifi_text, gprs_text;

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

    Log.i("IntrntActivity", "Broadcast message receivved");

    LinearLayout layout = new LinearLayout(context);
    LinearLayout.LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    View view = View.inflate(context, R.layout.main, layout);

    wifi_image = (ImageView) view.findViewById(R.id.wifi_image);
    gprs_image = (ImageView) view.findViewById(R.id.gprs_image);

    wifi_text = (TextView) view.findViewById(R.id.wifi_text);
    gprs_text = (TextView) view.findViewById(R.id.gprs_text);

    wifi_image.setVisibility(View.GONE);
    wifi_text.setVisibility(View.GONE);
    gprs_image.setVisibility(View.GONE);
    gprs_text.setVisibility(View.GONE);

    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(context.CONNECTIVITY_SERVICE);
    NetworkInfo WIFI = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo Mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (!WIFI.isConnected() && WIFI.isAvailable()) {
        Toast.makeText(context, "WIFI is available but not connected",
  开发者_如何学C              Toast.LENGTH_LONG).show();
    }

    if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isAvailable()) {
        wifi_image.setVisibility(View.VISIBLE);
        wifi_text.setVisibility(View.VISIBLE);

    }

    if (Mobile.isConnected()) {
        gprs_image.setVisibility(View.VISIBLE);
        gprs_text.setVisibility(View.VISIBLE);
        Log.i("IntrntActivity", "Mobile isConnected");

        // Toast.makeText(context,"GPRS is available",
        // Toast.LENGTH_LONG).show();
    }

    if (!Mobile.isConnected()) {
        gprs_image.setVisibility(View.GONE);
        gprs_text.setVisibility(View.GONE);
        Log.i("IntrntActivity", "Mobile is Not Connected");
        // Toast.makeText(context,"GPRS is available",
        // Toast.LENGTH_LONG).show();
    }


}

}

P.S : It is correctly going in Mobile.isConnected() and !Mobile.isConnected() and showing it in Log file but its Visibility is not changing.Am i not setting the view correctly? and is it possible to call setContentView(view) from this broadcast receiver?


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);
    }
  };
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜