2017-03-08 20 views
0

我正在開發一個Android應用程序,我需要跟蹤用戶的位置,當他/她打開應用程序。我正在使用GoogleApiClient的LocationService。我使用FusedLocationApi的requsetLocationUpdates()來更新位置。但是當我運行我的應用程序時,onLocationChanged()回調不會被調用,直到我手動打開設備位置跟蹤器。onLocationChanged()回調是永遠不會被調用,直到我手動打開我的設備位置跟蹤器

有沒有辦法更新用戶的位置,而無需手動打開設備位置跟蹤器?

+0

https://kjthumar.blogspot.de/2016/04/android-google-fusedlocationproviderapi.html?view = flipcard 檢查此鏈接。 –

+0

使用NETWORK_PROVIDER如果你不想把你的Gps .. – rafsanahmad007

+0

最終轉向NETWORK_PROVIDER。這很簡單。我的應用已經需要互聯網。我發現GoogleApiClient LocationService具有更高的準確性。這就是我爲什麼要使用它的原因。感謝您寶貴的回覆。 –

回答

0

如果你想跟蹤android上的位置,你必須在設置中打開跟蹤器。據我所知,沒有辦法自動做到這一點。

因此,直接回答你的問題,沒有辦法做到這一點。標準解決方案是要求用戶打開跟蹤並打開設置菜單。

例如:

/** 
* Function to show settings alert dialog 
* */ 
public void showSettingsAlert(){ 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

    // Setting Dialog Title 
    alertDialog.setTitle("GPS is settings"); 

    // Setting Dialog Message 
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

    // Setting Icon to Dialog 
    //alertDialog.setIcon(R.drawable.delete); 

    // On pressing Settings button 
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog,int which) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      mContext.startActivity(intent); 
     } 
    }); 

    // on pressing cancel button 
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel(); 
     } 
    }); 

    // Showing Alert Message 
    alertDialog.show(); 
} 

這是鏈接到教程: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

相關問題