Android – call setcontentview() from broadcast receiver

In my application, two classes are Internetactivity, which only extends activity and sets contentview to main. And extends MyClass. Of broadcast receiver

I have two textviews and two WiFi and GPRS imageviews in the main.xml file. When the connectivity changes, the broadcast receiver will be called and set the visibility of textview and ImageView according to the enabled content and whether I want to set it. But it only displays images instead of changes. This is the myclass.java file. What should I do??

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: it correctly enters mobile. Isconnected() and! Mobile. Isconnected() and displays it in the log file, but its visibility has not changed. I have not set the view correctly? Can setcontentview (view) be called from this broadcast receiver?

resolvent:

You need to put the receiver into the Internetactivity class, register and use the defined local variables there. You don't need to create a separate public broadcastreceiver implementation, just execute the local implementation

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>