如何显示弹出窗口,以启用您的位置访问权限并打开位置访问意图。现在我正在创建包含地图的应用程序。我启用了(map.setMyLocationEnabled(true))。并且应用程序显示当前位置按钮,但是当位置访问被禁用时,它不会显示任何响应。 我想如果位置访问禁用了弹出窗口,其中显示用户启用位置访问
提前致谢
答案 0 :(得分:3)
此代码虽然可以提供您想要的任何类型的位置服务,但是,如果不是位置管理器提供商,则提供者和错误消息
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);
}
}
答案 1 :(得分:2)
您可以在onResume中添加以下语句:
// Make sure that GPS is enabled on the device
LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!enabled) {
showDialogGPS();
}
在您的活动中添加以下方法:
/**
* Show a dialog to the user requesting that GPS be enabled
*/
private void showDialogGPS() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Enable GPS");
builder.setMessage("Please enable GPS");
builder.setInverseBackgroundForced(true);
builder.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startActivity(
new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
builder.setNegativeButton("Ignore", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
答案 2 :(得分:0)
您可以使用AlertDialog弹出窗口。
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked do some stuff
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();