7

在Android操作系統中,「設置」 - >「位置服務」,有一個名爲「訪問我的位置」,它可以用來切換按鈕禁用&從應用程序啓用位置信息訪問。聆聽位置接入禁用/啓用設置

目前,我正在開發一個位置服務應用程序。我想知道,我如何在Android項目中聽這個設置?當用戶禁用或啓用「訪問我的位置」時,是否有任何廣播接收器可以立即使用?

如果沒有任何廣播接收器,我怎樣才能在我的Android項目中聽到這種變化?

+1

@ GrlsHu,不,我不想這樣做。我的問題的關鍵點是如何聽這個位置訪問設置。 – Mellon

+0

http://stackoverflow.com/a/20673766/1994950和http://stackoverflow.com/a/23756293/1994950將幫助 – Kushal

回答

0

您可以使用下面的代碼來檢查位置服務是否啓用或不

LocationManager lm = null; 
boolean gps_enabled,network_enabled; 
    if(lm==null) 
     lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    try{ 
    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    }catch(Exception ex){} 
    try{ 
    network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    }catch(Exception ex){} 

    if(!gps_enabled && !network_enabled){ 
     dialog = new AlertDialog.Builder(context); 
     dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled)); 
     dialog.setPositiveButton(context.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       // TODO Auto-generated method stub 
       Intent myIntent = new Intent(Settings.ACTION_SECURITY_SETTINGS); 
       context.startActivity(myIntent); 
       //get gps 
      } 
     }); 
     dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       // TODO Auto-generated method stub 

      } 
     }); 
     dialog.show(); 

    } 
+2

嗨,我的問題不是關於如何檢查值,而是關於如何聽這意味着如何在代碼中獲取用戶已更改此位置訪問權限的通知並獲取更改後的值。 – Mellon

+0

@GrIsHu這是不正確的。你有沒有嘗試下面的usman解決方案? –

5

這個工作對我來說:

添加接收到清單文件:

<receiver 
     android:name="com.eegeo.location.LocationProviderChangedReceiver"> 
     <intent-filter> 
      <action android:name="android.location.PROVIDERS_CHANGED" /> 
     </intent-filter> 
    </receiver> 

檢查兩個位置提供者在接收者:

public class LocationProviderChangedReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     boolean anyLocationProv = false; 
     LocationManager locationManager = (LocationManager) MyMainActivity.context.getSystemService(Context.LOCATION_SERVICE); 

     anyLocationProv |= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     anyLocationProv |= locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     Log.i("", "Location service status" + anyLocationProv); 


    } 

} 

雖然這個接收器由於顯而易見的原因被多次調用,但這會告訴你狀態。

+1

僅當應用程序需要位於後臺時才應使用清單廣播接收器。否則它應該使用運行時廣播接收器 –

1

這樣你可以做得更簡單。在你的廣播接收器的的onReceive()方法

ContentResolver contentResolver = getContext().getContentResolver(); 
    // Find out what the settings say about which providers are enabled 
    int mode = Settings.Secure.getInt(
      contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF); 

    if (mode == Settings.Secure.LOCATION_MODE_OFF) { 
     // Location is turned OFF! 
    } else { 
     // Location is turned ON! 
    } 

〜感謝這段代碼:LocationManagerTest.java