我有几个屏幕,其中有上传按钮。
我还具有检查是否存在 3g 或 wifi 连接的功能,以及将按钮设置为灰色(如果没有连接)和绿色(如果有)的功能。唯一的问题是这仅在首次创建布局时触发,但我希望在连接发生变化时触发它。
有没有办法做到这一点?从概念上讲,我希望在我的主菜单类中有一个广播接收器来触发刷新按钮功能,但我不知道该怎么做。
我在这里浏览了代码:http: //www.netmite.com/android/mydroid/frameworks/base/core/java/android/net/NetworkConnectivityListener.java
以及这里的建议BroadcastReceiver 作为内部类,但我没有运气
当广播接收器是一个单独的类时,它工作得很好,但我想将它集成到各种活动中,我不知道如何进行。
任何方向都会有所帮助
private void updateUI(){
File[] allDir = DataWriter.getRootDir(profile).listFiles();
if(allDir==null){
uploadButton.setBackgroundResource(R.drawable.gray_button);
uploadButton.setEnabled(false);
return;
}
//MAXIM
boolean anyfiles = false;
outerloop:
for(File dir: allDir){
if(dir.getName().contains(profile[12])){
File[] allFiles = dir.listFiles();
for(File f:allFiles){
if(f.isDirectory())continue;
anyfiles = true;
break outerloop;
}
}
}
//
if (!anyfiles) {
uploadButton.setBackgroundResource(R.drawable.gray_button);
uploadButton.setEnabled(false);
} else {
if (intOpt==0) {
if(checkWifi()==true)
{
uploadButton.setBackgroundResource(R.drawable.red_button);
uploadButton.setEnabled(true);
}
else {
//MAXIM-changed blue_button to red_button
uploadButton.setBackgroundResource(R.drawable.gray_button);
uploadButton.setEnabled(false);
}
//uploadButton.setEnabled(true);
}
else if(intOpt==1)
{
if(check3G()||checkWifi())
{
uploadButton.setBackgroundResource(R.drawable.red_button);
uploadButton.setEnabled(true);
}
else {
//MAXIM-changed blue_button to red_button
uploadButton.setBackgroundResource(R.drawable.gray_button);
uploadButton.setEnabled(false);
}
//uploadButton.setEnabled(true);
}
else {
//MAXIM-changed blue_button to red_button
uploadButton.setBackgroundResource(R.drawable.gray_button);
uploadButton.setEnabled(false);
}
//uploadButton.setEnabled(true);
}
}
public boolean checkWifi() {
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo.State wifi = conMan.getNetworkInfo(1).getState();
if (wifi == android.net.NetworkInfo.State.CONNECTED) {
return true;
} else {
return false;
}
}
public boolean check3G() {
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo.State mobile = conMan.getNetworkInfo(0).getState();
if (mobile == android.net.NetworkInfo.State.CONNECTED) {
return true;
} else {
return false;
}
}
基本上我需要updateUI();
在连接发生变化时被触发。
谢谢
跟进:
我收到以下代码的错误“方法的返回类型丢失”
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("Network Listener", "Network Type Changed");
UpdateUI();
}
};
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter); // This is the line that gives me an error
我错过了什么吗?
编辑:更多代码
这是主菜单活动 - 用户可以在其中执行他们想要的任何操作、更改设置等的屏幕
公共类 MenuMainActivity 扩展 Activity { 私有 BHBluetoothService mBluetoothService = null;
private String[] profile = null;
private String username = null;
private String password = null;
public static String upass = null;
public static String uuname = null;
private double[] params = new double[2];
private double[] heart = null;
private double[] stride = null;
private double[] speed = null;
private List<File> files = new ArrayList<File>();
public static int intOpt=0;
private Button baselineButton,exerciseButton,fireTrainButton,
fireSuppButton,emerButton,rehaButton,uploadButton;
private static final int PROGRESS_DIALOG = 0;
private ProgressThread progressThread;
private ProgressDialog progressDialog;
private RadioButton wifiButton, button3g;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
initializeBluetooth();
Bundle extras = this.getIntent().getExtras();
if (extras.containsKey("profile")) {
try {
profile = extras.getStringArray("profile");
username = profile[0];
password = profile[1];
upass=password;
uuname=username;
initializeUI();
} catch (Exception e) {
e.printStackTrace();
}
} else {
finish();
}
if (extras.containsKey("params") && extras.containsKey("heart") && extras.containsKey("stride") && extras.containsKey("speed")) {
params = extras.getDoubleArray("params");
heart = extras.getDoubleArray("heart");
stride = extras.getDoubleArray("stride");
speed = extras.getDoubleArray("speed");
}
updateUI();
}
出色地; 这是主菜单活动的起始代码。
我想触发 updateUI(); 在任何连接状态更改时。