簡體   English   中英

我如何檢查用戶是否在Android手機上真正啟用了GPS位置

[英]How can i check user really enabled the gps location on his phone in android

我的應用程序需要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);
                    }
                });

問題是用戶從彈出窗口轉到了位置設置,並且如果用戶未從位置設置激活GPS,而是用戶按下了返回按鈕並返回到應用程序。再次返回到應用程序而不是激活GPS手段,該應用程序必須再次顯示該對話框。

所以我再次確保如下所示,

GetLocationService();

        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
        boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if(statusOfGPS==false)
            GetLocationService();

但是用戶再次通過位置設置無法啟用GPS,並且無法通過單擊“后退”按鈕並返回應用程序來啟用GPS。 我如何強迫用戶啟用GPS並與應用程序一起使用。

我如何強迫用戶啟用GPS並與應用程序一起使用。

要在移動應用程序中的下一個屏幕之前強制用戶啟用GPS:

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.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.當用戶從“設置”返回到應用程序時,重寫onActivityResult以檢查GPS是否啟用:

@Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        isGPSEnabled();
    }

同時使AlertDialog可取消false形式返回並觸摸外部Dialog:

dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM