2016-05-02 38 views
0

的Android版本低於6只將這些許可請求的清單作品:應用程序,可以讓位置,運行在Android 4.0.3+

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

但對於Android版本6,我需要明確要求許可太獲得位置:

requestPermissions(new String[]{ Manifest.permission.ACCESS_FINE_LOCATION}, 1340); 

的問題是,如果我添加上面的代碼的應用程序僅適用於Android 6和失敗作爲requestPermissions較低版本運行在較低版本的不存在。而如果我刪除它,我將無法得到位置在Android 6.

我能做些什麼,我的應用程序能夠在獲取位置的Android 4.0.3+

回答

0

您可以檢查運行你的應用程序像這樣的電話API級別:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    //later than 4.x - so here you can run the code 
    //ideally you want to make this call in a helper method which you then annotate with the @TargetApi 
    requestPermissions(new String[]{ Manifest.permission.ACCESS_FINE_LOCATION}, 1340); 
    } 
else{ 
    //nothing to do in your case ? 
} 

您還可以使用@TargetApi(Build.VERSION_CODES.LOLLIPOP)的方法,你想使「棒棒堂+」電話。

相關問題