Android許可權獲取
阿新 • • 發佈:2018-12-12
開發中,很多時候要去動態的獲取許可權,效果圖如下:
直接看程式碼,首先加入許可權:
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.REORDER_TASKS" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" /> <!-- 訪問網路的許可權 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_LOGS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-feature android:name="android.hardware.location.gps" /> <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- 閱讀訊息 --> <uses-permission android:name="android.permission.READ_SMS" /> <!-- 寫入訊息 --> <uses-permission android:name="android.permission.WRITE_SMS" /> <!-- 接收訊息 --> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- iBeancon功能所需許可權 --> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
然後加入許可權管理的工具類:
import android.content.Context; import android.content.pm.PackageManager; import android.support.v4.content.ContextCompat; /** * author: wu * date: on 2018/11/30. * describe:檢查許可權的工具類 */ public class PermissionsChecker { private final Context mContext; public PermissionsChecker(Context context) { mContext = context.getApplicationContext(); } // 判斷許可權集合 public boolean lacksPermissions(String... permissions) { for (String permission : permissions) { if (lacksPermission(permission)) { return true; } } return false; } // 判斷是否缺少許可權 private boolean lacksPermission(String permission) { return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_DENIED; } }
最後在程式碼中應用工具類:
public class MainActivity extends AppCompatActivity { private PermissionsChecker mPermissionsChecker; // 許可權檢測器 private final int RESULT_CODE_LOCATION = 0x001; private Button button; //定位許可權,獲取app內常用許可權 String[] permsLocation = {"android.permission.READ_PHONE_STATE" , "android.permission.ACCESS_COARSE_LOCATION" , "android.permission.ACCESS_FINE_LOCATION" , "android.permission.READ_EXTERNAL_STORAGE" , "android.permission.WRITE_EXTERNAL_STORAGE"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); mPermissionsChecker = new PermissionsChecker(MainActivity.this); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mPermissionsChecker.lacksPermissions(permsLocation)) { ActivityCompat.requestPermissions(MainActivity.this, permsLocation, RESULT_CODE_LOCATION); } } }); } }
這樣就可以了~~