[英]How can i check user really enabled the gps location on his phone in android
My application needs GPS,so i have redirected the user to enable the GPS in case if it's off. 我的应用程序需要GPS,因此我已重定向用户以启用GPS,以防它关闭。
AlertDialog.Builder dialog = new AlertDialog.Builder(contxt);
dialog.setMessage("Enable GPS Location Service");
dialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
contxt.startActivity(myIntent);
}
});
The problem is that the user goes to the location setting from the pop up window and if the user don't activate the GPS from the location setting,instead of that the user press the back button and goes back to the application.If the person again goes back to the app instead of activating the GPS means, the application have to show the dialog box again. 问题是用户从弹出窗口转到了位置设置,并且如果用户未从位置设置激活GPS,而是用户按下了返回按钮并返回到应用程序。再次返回到应用程序而不是激活GPS手段,该应用程序必须再次显示该对话框。
So i have ensured it again like below, 所以我再次确保如下所示,
GetLocationService();
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(statusOfGPS==false)
GetLocationService();
but again the user, from the location settings, could not enable the GPS and by clicking on the "back" button and going back to the application. 但是用户再次通过位置设置无法启用GPS,并且无法通过单击“后退”按钮并返回应用程序来启用GPS。 How can i force the user to enable the GPS and work with the application.
我如何强迫用户启用GPS并与应用程序一起使用。
How can i force the user to enable the GPS and work with the application.
我如何强迫用户启用GPS并与应用程序一起使用。
To force user enable GPS before moving next screen in Application: 要在移动应用程序中的下一个屏幕之前强制用户启用GPS:
1. Create a method in which check is GPS is enabled or not and start next Activity accordingly : 1.创建一种方法,在该方法中启用或禁用GPS检查,并相应地开始下一个活动:
private void isGPSEnabled(){
LocationManager manager;
boolean statusOfGPS =
manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(statusOfGPS){
// start next Activity
}else{
// show alert for enabling GPS
}
}
2. On AlertDialog
Positive Button click start GPS setting screen using startActivityForResult
: 2.在
AlertDialog
肯定按钮上,使用startActivityForResult
单击启动GPS设置屏幕:
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
contxt.startActivityForResult(myIntent,100);
}
3. Override onActivityResult
to check GPS is enabled or not when user back to app from Settings: 3.当用户从“设置”返回到应用程序时,重写
onActivityResult
以检查GPS是否启用:
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
isGPSEnabled();
}
Also make AlertDialog
Cancelable false
form both back and touch of outside Dialog: 同时使
AlertDialog
可取消false
形式返回并触摸外部Dialog:
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.