没有互联网连接时禁用按钮

时间:2016-10-09 09:49:50

标签: android

我的代码如下所示:

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    new PlayerTask().execute(stream);

    b_play.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View view) {
         if (started) {
             started = false;
             mediaPlayer.pause();
             b_play.setText(R.string.start);
         } else {
             started = true;
             mediaPlayer.start();
             b_play.setText(R.string.pause);

         }
     }
});

如果没有互联网连接,如何禁用b_play按钮?

3 个答案:

答案 0 :(得分:3)

如果您想要不断检查是否有互联网连接,请根据该按钮禁用/启用按钮。请使用以下解决方案:

首先创建扩展BroadcastReceiver的Receiver类(让它命名为NetworkStateReceiver)。 NetworkStateReceiver根据互联网的可用性调用接口的不同方法:

public class NetworkStateReceiver extends BroadcastReceiver {

public interface NetworkListener {
    void onNetworkAvailable();
    void onNetworkUnavailable();
}

private NetworkListener networkListener;

public NetworkStateReceiver(NetworkListener networkListener) {
    this.networkListener = networkListener;
}

public void onReceive(Context context, Intent intent) {
    if (intent.getExtras() != null) {
        NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
         Log.i("app", "Network " + ni.getTypeName() + " connected");
            networkListener.onNetworkAvailable();
        }
    }
    if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
       Log.d("app", "There's no network connectivity");
        networkListener.onNetworkUnavailable();
    }
  }
}

然后创建实现NetworkStateReceiver.NetworkListener接口的NetworkStateReceiver类的实例:

NetworkStateReceiver networkStateReceiver = new NetworkStateReceiver(new NetworkStateReceiver.NetworkListener() {
        @Override
        public void onNetworkAvailable() {
            b_play.setEnabled(true); 
        }

        @Override
        public void onNetworkUnavailable() {
            b_play.setEnabled(false); 
        }
    });

创建NetworkStateReceiver的实例后,将其注册为:

registerReceiver(networkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

别忘了在onDestroy()方法中取消注册:

public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(networkStateReceiver);
}

答案 1 :(得分:2)

您需要检查是否有互联网连接:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

之后,您必须禁用(或启用)按钮

myButton.setEnabled(isNetworkAvailable()); 

修改 你需要在清单

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

答案 2 :(得分:0)

public final boolean isInternetOn()
{
    ConnectivityManager connec = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);

    // ARE WE CONNECTED TO THE NET
    if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
            connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED )
    {
        // MESSAGE TO SCREEN FOR TESTING (IF REQ)
        //Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
        return true;
    }
    else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
            ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  )
    {
        return false;
    }

    return false;
}
//-----------------------
if(isInternetOn()){
   btn.setEnabled(true);
}
else{
   btn.setEnabled(false);
}