Android權限的正確設置

#這個,是我第一次寫個個人心得的博客吧
我在程序員的道路上,一步一個腳印吧

Android 開發中,在自己做的音樂播放器中,一個權限的問題,讓我糾結了好幾天,今天終於解開了。國內網站,確實搜不出什麼有用的。

Android API 23以上,對權限的使用,不光是在Mainfeast中了,需要在運行的代碼塊中加入
具體代碼:


代碼塊

代碼塊語法遵循標準markdown代碼,例如:

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}
  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章