gpt4 book ai didi

android - 从广播接收器调用 SetContentView()

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:33 26 4
gpt4 key购买 nike

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

我在 main.xml 文件中有 2 个 TextView 和 2 个 WIFI 和 GPRS 的 ImageView。当连接性发生变化时,将调用广播接收器,并根据启用的内容和未启用的内容设置 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() 并在日志文件中显示它,但它的可见性没有改变。Am我没有正确设置 View ?是否可以从此广播接收器调用 setContentView(view)?

最佳答案

您需要将接收器放入 InternetActivity 类,在那里注册并使用已经定义的局部变量。您无需创建单独的公共(public) 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);
}
}

关于android - 从广播接收器调用 SetContentView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5375881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com