我有几个屏幕,其中有上传按钮。刷新连接状态变化的Android视图
我也有检查是否存在3g或wifi连接的功能,以及如果没有连接将按钮设置为灰色并且将按钮设置为灰色(如果存在)的功能。唯一的问题是,这只会在布局首次创建时触发,而我希望在连接发生变化时触发它。
有没有办法做到这一点?从概念上讲,我想在我的主菜单类中的广播接收器触发刷新按钮功能,但我不知道如何做到这一点。
这里的建议BroadcastReceiver as inner class,但我有没有运气
广播接收机的伟大工程时,它是一个单独的类,但我想整合它进入各种活动,我不知道如何进行。
任何方向将是有益的
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延伸活动{ 私人BHBluetoothService mBluetoothService =空值;
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();在任何连接状态改变。
你能发布更多的代码吗?你想用什么方法注册接收器? – Sam