gpt4 book ai didi

android - 当用户禁用位置访问时显示弹出窗口(Android Google map )

转载 作者:行者123 更新时间:2023-12-04 15:06:22 26 4
gpt4 key购买 nike

我如何显示弹出窗口以启用您的位置访问并打开位置访问 Intent 。现在我正在创建包含 map 的应用程序。我已启用(map.setMyLocationEnabled(true))。并且应用程序显示当前位置按钮,但当禁用位置访问时,它不会显示任何响应。我希望如果位置访问被禁用,则会显示一个弹出窗口,提示用户启用位置访问

提前致谢

最佳答案

此代码虽然为您提供了您想要的一切,但是否有任何类型的位置服务可用?、提供商,以及错误消息(如果不是位置管理器提供商)

public class LocationManager_check {

LocationManager locationManager;
Boolean locationServiceBoolean = false;
int providerType = 0;
static AlertDialog alert;

public LocationManager_check(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 activityObj) {

// show alert dialog if Internet is not connected
AlertDialog.Builder builder = new AlertDialog.Builder(activityObj);

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);
activityObj.startActivity(intent);
dialog.dismiss();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert = builder.create();
alert.show();
}

}

如何使用

LocationManager_check locationManagerCheck = new LocationManager_check(
this);
Location location = null;

if(locationManagerCheck .isLocationServiceAvailable){

if (locationManagerCheck.getProviderType() == 1)
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
else if (locationManagerCheck.getProviderType() == 2)
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}else{
locationManagerCheck .createLocationServiceError(your_activity.this);
}

}

关于android - 当用户禁用位置访问时显示弹出窗口(Android Google map ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24160472/

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