1

アップロードボタンがある画面がいくつかあります。

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

何か不足していますか?

編集:より多くのコード

これはメイン メニュー アクティビティです。ユーザーが必要なアクションを実行したり、設定を変更したりできる画面です。

public class MenuMainActivity extends Activity { private 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(); をトリガーしたい 接続状態が変化したとき。

4

1 に答える 1

0

BroadcastReceiver接続アラートをリッスンするように をセットアップできます。例については、このSO 投稿をご覧ください。

于 2012-08-23T21:31:38.893 に答える