0

In My application i checked the internet connection before the application start If the connection is not available then it displayed a alert box with YES and NO button if the user click the No button of alert box then it close the application.

If the user click the Yes button of alert box then I start the an Activity by using the following code

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Info");
alertDialogBuilder.setMessage("Internet connection not available check ur internet connection");
alertDialogBuilder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
        dialog.cancel();
    }
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        System.exit(0);
        return;
    }
});

I want to check the user on the connection or not if the user on the internet connection then it will display the main Activity when the user press the Back Button otherwise Application Exited.

4

4 に答える 4

2

NetworkInfoクラスを使用してインターネット接続を確認します。ユーザーが [戻る] ボタンをクリックすると、この関数が呼び出されます。

public boolean checkInternet(){
 NetworkInfo network = connectionManager.getActiveNetworkInfo();
  if (network == null)
     return false;
  if (!network.isConnected())
     return false;
 return true;
}
于 2013-01-08T09:42:20.600 に答える
1

次の関数を使用して、ユーザーがオンラインかどうかを確認できます。

public static boolean isOnline(Context context) {
   ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getActiveNetworkInfo();
   if (netInfo != null && netInfo.isConnectedOrConnecting()) {
       return true;
   }
   return false;
}

これを AndroidManifest.xml に追加する必要もあります。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
于 2013-01-08T09:43:42.177 に答える
1

これを行うには、onKeyDown をオーバーライドしてカスタム ダイアログを作成する必要があります。

[編集1]

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

        // back buton pressed , now you can check connection here

       if(isOnline)
       {
           // go to main activity
       }
       else
       {
          System.exit(0);
       } 

        return true;
    }
    return super.onKeyDown(keyCode, event);
}



public static boolean isOnline(Context context) {
   ConnectivityManager cm = (ConnectivityManager)     
   context.getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getActiveNetworkInfo();

   if (netInfo != null && netInfo.isConnectedOrConnecting()) {
       return true;
   }

   return false;
}
于 2013-01-08T09:40:37.027 に答える
0

ボタンをクリックしてこのメ​​ソッドを呼び出し、インターネット許可を追加して、メニフェストのネットワーク状態にアクセスします。

protected boolean checkConnection(Context mContext) {
    final NetworkInfo info = ((ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
        return false;
    }
    if (info.isRoaming()) {
        return true;
    }
    return true;
}
于 2013-01-08T09:50:08.180 に答える