网络判断及跳转

工具类

public class NetWorkUtils {
    public static boolean getType(Context context) {
        //判断不为空
        if (context != null) {
            //得到当前的网络判断值
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            if (networkInfo != null) {
                return networkInfo.isAvailable();
            }
        }
        return false;
    }
}
跳转DiaLog设置
 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:padding="20dp"
        android:text="当前没有网络连接,请选择 "/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="取消"
            android:id="@+id/btn1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="设置"
            android:id="@+id/btn2"/>
    </LinearLayout>
</LinearLayout>
MainActivity
 
 
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private AlertDialog dialog;
    private Button btn1;
    private Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (NetWorkUtils.getType(MainActivity.this) == true){
            Toast.makeText(MainActivity.this,"流量速度还行",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(MainActivity.this,"兄弟没网啊!!!",Toast.LENGTH_SHORT).show();
            View view = View.inflate(MainActivity.this, R.layout.dialog, null);
            dialog = new AlertDialog.Builder(MainActivity.this).create();
            dialog.setIcon(R.mipmap.ic_launcher);
            dialog.setTitle("提示");
            dialog.setView(view);
            dialog.show();

            btn1 = view.findViewById(R.id.btn1);
            btn2 = view.findViewById(R.id.btn2);

            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this,"取消关闭!!!",Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    finish();
                }
            });
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Settings.ACTION_SETTINGS);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this,"这里是网络设置界面!!!",Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            });
        }

    }
}

猜你喜欢

转载自blog.csdn.net/e_d_i_e/article/details/79095916