Java 当用户禁用gps时,如何获取MapActivity警报
我正在用java在android上制作一个应用程序,在地图上显示当前地址,并在gps关闭时发出警报Java 当用户禁用gps时,如何获取MapActivity警报,java,android,google-maps-api-3,Java,Android,Google Maps Api 3,我正在用java在android上制作一个应用程序,在地图上显示当前地址,并在gps关闭时发出警报 public class LocationManagerCheck { LocationManager locationManager; Boolean locationServiceBoolean = false; int providerType = 0; static AlertDialog alert; public LocationManage
public class LocationManagerCheck {
LocationManager locationManager;
Boolean locationServiceBoolean = false;
int providerType = 0;
static AlertDialog alert;
public LocationManagerCheck(Context context) {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
boolean gpsIsEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkIsEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (networkIsEnabled == true && gpsIsEnabled == true) {
locationServiceBoolean = true;
providerType = 1;
} else if (networkIsEnabled != true && gpsIsEnabled == true) {
locationServiceBoolean = true;
providerType = 2;
} else if (networkIsEnabled == true && gpsIsEnabled != true) {
locationServiceBoolean = true;
providerType = 1;
}
}
public Boolean isLocationServiceAvailable() {
return locationServiceBoolean;
}
public int getProviderType() {
return providerType;
}
public void createLocationServiceError(final Activity activity) {
// show alert dialog if Internet is not connected
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(
"You need to activate location service to use this feature. Please turn on network or GPS mode in location settings")
.setTitle("LostyFound")
.setCancelable(false)
.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivity(intent);
dialog.dismiss();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert = builder.create();
alert.show();
}
我使用的代码仅在
Mapactivity
启动时显示警报,但在活动期间不显示警报。我只想在gps关闭时显示警报bo,或在Mapactivity打开期间由用户禁用,请帮助我找到正确的代码,谢谢。您可能想知道用户是否打开/关闭GPS。这可能会有所帮助为此,您必须使用广播接收器
首先,你必须注册广播
private var receiverRegistered: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
registerBroadcast()
}
private fun registerBroadcast() {
val filter = IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)
filter.addAction(Intent.ACTION_PROVIDER_CHANGED)
this.registerReceiver(broadCastReceiver, filter)
receiverRegistered = true
}
private val broadCastReceiver = object : BroadcastReceiver() {
override fun onReceive(contxt: Context?, intent: Intent?) {
if (LocationManager.PROVIDERS_CHANGED_ACTION == intent?.action) {
val locationManager = contxt?.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
if (!isGpsEnabled) {
// call your alert here
}
}
}
}
最重要的是取消广播接收器的注册
override fun onDestroy() {
if (receiverRegistered) {
this.unregisterReceiver(broadCastReceiver)
}
super.onDestroy()
}
每次用户关闭GPS时,它都会显示警报。您可以从这里进行检查,以在java中注册接收器