调用从广播接收器的setContentView() [英] Calling SetContentView() from broadcast receiver
问题描述
在我的应用程序有一二类InternetActivity只延伸活动,并设置内容查看主。和MyClass的延伸广播接收机。
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.
我有2个TextView的和main.xml中的文件2 ImageView的WIFI和GPRS的。 当改变连通性正在发生,brodcast接收器获取调用,并根据是什么启用,什么不是我想要设置的TextView和ImageView的知名度。但它是仅示出两个图像而不是更改。 这里是MyClass.java文件。我该怎么办呢??
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();
}
}
}
PS:这是正确朝着 Mobile.isConnected()
和 Mobile.isConnected()
和展示!这在日志文件中,但它的知名度不changing.Am我没有正确设置视图?并有可能从该广播接收器调用的setContentView(视图)?
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?
推荐答案
您需要把你的reciever到InternetActivity类,注册它,并使用已定义的局部变量。你不必创建单独的公共BroadcastReceiver的实施,只是做了局部的。
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.
这样的:
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);
}
}
这篇关于调用从广播接收器的setContentView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!