java 基本活动检测互联网连接GPS禁用。它显示了启用它们的对话框:)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 基本活动检测互联网连接GPS禁用。它显示了启用它们的对话框:)相关的知识,希望对你有一定的参考价值。

package com.djhs16.location;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

/**
 * Base activity to check GPS disabled and Internet <br/>
 * this activity requires following permission(s) to be added in the AndroidManifest.xml file:
 * 
 * <ul>
 * <li>android.permission.ACCESS_FINE_LOCATION</li>
 * <li>android.permission.INTERNET</li>
 * <li>android.permission.ACCESS_NETWORK_STATE</li>
 * 
 * </ul>
 */
public class BaseActivity extends Activity implements LocationListener {

	protected LocationManager mLocationManager;
	private static final int GPS_ENABLE_REQUEST = 0x1001;
	private static final int WIFI_ENABLE_REQUEST = 0x1006;

	private BroadcastReceiver mNetworkDetectReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			checkInternetConnection();
		}
	};
	private AlertDialog mInternetDialog;
	private AlertDialog mGPSDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

		registerReceiver(mNetworkDetectReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
	}

	@Override
	protected void onDestroy() {
		mLocationManager.removeUpdates(this);
		unregisterReceiver(mNetworkDetectReceiver);
		super.onDestroy();
	}

	private void checkInternetConnection() {
		ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = manager.getActiveNetworkInfo();

		if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {

		} else {
			showNoInternetDialog();
		}
	}

	@Override
	public void onLocationChanged(Location location) {
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	}

	@Override
	public void onProviderEnabled(String provider) {
	}

	@Override
	public void onProviderDisabled(String provider) {
		if (provider.equals(LocationManager.GPS_PROVIDER)) {
			showGPSDiabledDialog();
		}
	}

	private void showNoInternetDialog() {

		if (mInternetDialog != null && mInternetDialog.isShowing()) {
			return;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Internet Disabled!");
		builder.setMessage("No active Internet connection found.");
		builder.setPositiveButton("Turn On", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
				startActivityForResult(gpsOptionsIntent, WIFI_ENABLE_REQUEST);
			}
		}).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		mInternetDialog = builder.create();
		mInternetDialog.show();
	}

	public void showGPSDiabledDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("GPS Disabled");
		builder.setMessage("Gps is disabled, in order to use the application properly you need to enable GPS of your device");
		builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), GPS_ENABLE_REQUEST);
			}
		}).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		mGPSDialog = builder.create();
		mGPSDialog.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == GPS_ENABLE_REQUEST) {
			if (mLocationManager == null) {
				mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
			}

			if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
				showGPSDiabledDialog();
			}

		} else if (requestCode == WIFI_ENABLE_REQUEST) {

		} else {
			super.onActivityResult(requestCode, resultCode, data);
		}
	}
}

以上是关于java 基本活动检测互联网连接GPS禁用。它显示了启用它们的对话框:)的主要内容,如果未能解决你的问题,请参考以下文章

使用kotlin从android中的BroadcastReceiver类访问edittext

如何检测互联网连接[重复]

禁用后虚假 GPS 位置仍然存在

Alamofire - 检测互联网连接

Gcloud 无法检测到网络连接

使用对话框禁用 GPS (Android)