gpt4 book ai didi

java - 当用户禁用 GPS 时如何在 MapActivity 上收到警报

转载 作者:行者123 更新时间:2023-12-01 23:33:10 24 4
gpt4 key购买 nike

我正在使用java在android中制作一个应用程序,它在 map 上显示当前地址,并在gps关闭时发出警报。

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 启动时显示警报,但在 activity 期间不显示警报。我只想在 GPS 关闭时向我显示警报 bo或者在 MapActivity 打开期间由用户禁用,请帮助我找到正确的代码,提前致谢。

最佳答案

您可能想知道用户是否打开/关闭 GPS。这个question可能有帮助

关于java - 当用户禁用 GPS 时如何在 MapActivity 上收到警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58284517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com