从广播接收器调用SetContentView()

时间:2011-03-21 09:40:14

标签: android broadcastreceiver

在我的应用程序中,有两个类是InternetActivity,它只扩展Activity并将contentview设置为main。和扩展广播接收器的MyClass。

我在main.xml文件中有2个TextView和2个WIFI和GPRS ImageView。 当连接性发生变化时,brodcast接收器将被调用,并根据启用的内容和不想要设置TextView和ImageView的可见性。但它只显示图像而不是变化。 这是MyClass.java文件。我该怎么办?

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",
                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:它正确地进入Mobile.isConnected()!Mobile.isConnected()并在日志文件中显示它但其可见性没有改变。我没有正确设置视图?是否可以从这个广播接收器调用setContentView(view)?

2 个答案:

答案 0 :(得分:11)

您需要将接收器放入InternetActivity类,在那里注册并使用已定义的局部变量。您无需创建单独的公共BroadcastReceiver实现,只需执行本地实现。

像这样:

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

答案 1 :(得分:5)

您无法将膨胀的视图添加到您的活动内容视图中?!

您应该将所有内容都充气并设置为onCreate方法中的内容视图。然后,您的广播接收器应该只设置所选视图的可见性。

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