2012-08-23 11 views
1

我有几个屏幕,其中有上传按钮。刷新连接状态变化的Android视图

我也有检查是否存在3g或wifi连接的功能,以及如果没有连接将按钮设置为灰色并且将按钮设置为灰色(如果存在)的功能。唯一的问题是,这只会在布局首次创建时触发,而我希望在连接发生变化时触发它。

有没有办法做到这一点?从概念上讲,我想在我的主菜单类中的广播接收器触发刷新按钮功能,但我不知道如何做到这一点。

我经过这里的代码去: http://www.netmite.com/android/mydroid/frameworks/base/core/java/android/net/NetworkConnectivityListener.java

这里的建议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();在任何连接状态改变。

+0

你能发布更多的代码吗?你想用什么方法注册接收器? – Sam

回答

0

您可以设置BroadcastReceiver来侦听连接警报。查看this SO帖子作为例子。

+0

我得到错误“的方式返回类型缺失”为下面的代码 “\t广播接收器networkStateReceiver =新的广播接收器(){ \t @覆盖 \t公共无效的onReceive(上下文语境,意图意图){ \t日志。w(“网络监听器”,“网络类型改变”); \t UpdateUI(); \t} \t}; \t IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); \t registerReceiver(networkStateReceiver,filter);' –

+0

我的歉意,但我发布后不久编辑我的答案,因为原来的SO帖子链接是非常多的伪代码(这似乎是你在跟随)。我会查看新链接的SO帖子以获得更好的示例。 – Neeko

+0

我在看代码 - 它看起来像是有一个扩展BroadcastReceiver的类。如果我尝试将这些代码放在另一个类中 - 我的应用程序将崩溃。我前一阵子试图这样做,没有运气。 任何建议 –