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",
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: