wifi和数据连接的判断

来源:互联网 发布:身份证淘宝子账号验证 编辑:程序博客网 时间:2024/05/22 05:09
  public static boolean NetWorkStatus(final Activity activity) {
        final SharedPreferences mSharedPref;
        mSharedPref = PreferenceManager.getDefaultSharedPreferences(activity);
        boolean netSataus = false;
        ConnectivityManager cwjManager = (ConnectivityManager) activity
                .getSystemService(Context.CONNECTIVITY_SERVICE);


        cwjManager.getActiveNetworkInfo();


        if (cwjManager.getActiveNetworkInfo() != null) {
            netSataus = cwjManager.getActiveNetworkInfo().isConnectedOrConnecting();
        }


        if (!netSataus) {
            android.app.AlertDialog.Builder b = new AlertDialog.Builder(activity).setTitle(
                    "没有可用的网络").setMessage("是否对网络进行设置?");
            b.setPositiveButton("是", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Intent mIntent = new Intent("android.net.wifi.PICK_WIFI_NETWORK");
                    activity.startActivityForResult(mIntent, 0); // 如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写
                }
            }).setNeutralButton("否", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            }).show();
        }
        if (mSharedPref.getBoolean("gprs", false)) {
        } else {
            State gprs = cwjManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
            State wifi = cwjManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
            if (gprs == State.CONNECTED || gprs == State.CONNECTING) {
                LayoutInflater inflater = LayoutInflater.from(activity);
                final View textEntryView = inflater.inflate(R.layout.tydtech_alert_custom, null);
                final AlertDialog dlg = new AlertDialog.Builder(new ContextThemeWrapper(activity,android.R.style.Theme_Holo_Light)).create();
                dlg.setView(textEntryView);
                dlg.show();
                Button mSetwifi = (Button) textEntryView.findViewById(R.id.setwifi);
                mSetwifi.setOnClickListener(new OnClickListener() {


                    @Override
                    public void onClick(View v) {
                        Intent mIntent = new Intent("android.net.wifi.PICK_WIFI_NETWORK");
                        activity.startActivityForResult(mIntent, 0);
                        dlg.cancel();
                    }
                });
                final CheckBox box = (CheckBox) textEntryView.findViewById(R.id.displaybox);
                Button mContinue = (Button) textEntryView.findViewById(R.id.mcontinue);
                mContinue.setOnClickListener(new OnClickListener() {


                    @Override
                    public void onClick(View v) {
                        if (box.isChecked()) {
                            Log.i("zhang", "isChecked");
                            SharedPreferences.Editor editor = mSharedPref.edit();
                            editor.putBoolean("gprs", true);
                            editor.commit();
                        }
                        dlg.cancel();
                    }
                });


            }
        }


        return netSataus;
    }
0 0
原创粉丝点击