1

我有一个检查互联网连接的活动1,如果用户没有互联网连接,应用程序会将他发送到无线设置。

我想知道如何检查用户在返回调用无线设置的活动 1 时是否激活了互联网数据。

我希望你的帮助。谢谢你。

  public void myThread(){
    Thread th=new Thread(){

        @Override
        public void run(){
            splash_screen.this.runOnUiThread(new Runnable() {

                public void run() {                 
                    dialog.conectarRed().show();
                }
            });
        }
    };

    th.start();      
  }

 }

 public Dialog conectarRed() {
     AlertDialog.Builder builder = new AlertDialog.Builder(context);
      return builder
            .setTitle("No hay Conexi�n a Internet")
            .setMessage("� Desea activar la Conexi�n de Datos ?")                        
            .setCancelable(false)
            .setPositiveButton("Aceptar",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);

                            ((Activity) context).startActivity(i);

                            dialog.cancel();
                        }
                    })
            .setNegativeButton("Cancelar",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            ((Activity) context).finish();

                            dialog.cancel();
                        }
                    }).create();
}
4

2 回答 2

1

检查中的连通性onStart()。您可以确定您是否有 wifi 或移动连接处于活动状态。如果没有启动无线设置活动,当你回来时你会发现用户是否已经激活。

@Override
protected void onStart() {
    super.onStart();
    if (!CheckConnectivity()){
        Toast.makeText(this, R.string.toast_enable_connectivity,Toast.LENGTH_SHORT).show();
        Handler launchHandler = new Handler();
        Log.d(TAG,"launching EnableConnectivity()");
        launchHandler.postDelayed(new Runnable() { 
             public void run() { 
                Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                startActivity(intent);
             } 
        }, 2000);           
    }
}


private boolean CheckConnectivity(){
    NetworkInfo networkInfo;
    ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean connectivityAvailable = false;

    if (connManager == null){
        Toast.makeText(this, R.string.toast_conn_no_service ,Toast.LENGTH_SHORT).show();
    } else {
        networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        connectivityAvailable |= networkInfo.isConnected();
        networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        connectivityAvailable |= networkInfo.isConnected();
    }

    return connectivityAvailable;
}
于 2013-07-20T07:43:28.243 回答
0

你应该使用这个

((Activity) context).startActivityForResult(i, 0);

然后您可以在 Activity1 中使用 onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //Check result

}
于 2013-07-20T07:09:43.720 回答