简体   繁体   English

我如何检查用户是否在Android手机上真正启用了GPS位置

[英]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.

相关问题 我向用户显示了启用gps设置的提示,但是如何检查用户是否在手机上真正启用了gps位置? - i showed prompt to user to enable gps settings,but how can i check user really enabled the gps location on his phone? 如何检查用户是否真的在Android中启用了GPS定位? - How to check if the user really enables the GPS location in Android? 如何在应用程序中检查GPS支持以为启用了定位服务的用户添加功能? - How can I check for GPS support in-App to add a feature for those with Location services enabled? 在尝试使用GPS之前,如何检查GPS是否已启用 - How can I check if GPS is enabled before I try to use it Android 4.3:如何检查用户是否启用了锁定? - Android 4.3 : How can I check if user has lock enabled? 如何手动将GPS数据发送到Android手机上的“位置管理器”(已root) - How can I manually send GPS data to Location Manager on my android phone(rooted) 如何检查我的Android手机是否丢失了GPS信号? - How can i check if my android phone lost its GPS signal? 是否每个Android手机都启用了GPS? - Is every Android phone GPS Enabled or not? Android:如何使用他的电话号码登录用户 - Android: How to login an user with his phone number 如何查看手机是否有 GPS 设备? - How can I check whether the phone has GPS device or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM