在我的应用程序中,如果关闭,我将调用Intent GPS

private void GPS() {
     Intent GPSSetting = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
     startActivity(GPSSetting);
}


我在按钮中使用它

myButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
           if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
               method();
           }else{
               GPS();
           }
        }
    });


它是可行的,但是现在我有编辑此问题以检查互联网是否打开

首先,我做这个:

private void internet() {
    Intent internet = new Intent(android.provider.Settings.ACTION_NETWORK_OPERATOR_SETTINGS);
    startActivity(internet);
}


但是如何像使用GPS这样的按钮一样使用它呢?

最佳答案

以下是用于检查Determining and Monitoring the Connectivity Status是否可用的网络连接的代码段:

ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

关于java - 上网意向,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41107177/

10-10 01:33