1. 程式人生 > >android6.0百度地圖授權

android6.0百度地圖授權

private void requestRunTimePermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.READ_PHONE_STATE)) { /* * 如果沒有獲得過使用者的許可權許可,則向用戶申請 */

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("確認退出嗎?");
    builder.setTitle("提示");
    builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{
                            Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_PHONE_STATE
                    },BAIDU_READ_PHONE_STATE);
            dialog.dismiss();
        }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    builder.create().show();
} else {
/*
 *  如果已經獲得過使用者的許可權許可了,則直接申請執行時許可權即可
 */
    Toast.makeText(MainActivity.this,"我的許可權",Toast.LENGTH_LONG).show();
    ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{
                    Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_PHONE_STATE
            },BAIDU_READ_PHONE_STATE);
}

}

//Android6.0申請許可權的回撥方法 @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { // requestCode即所宣告的許可權獲取碼,在checkSelfPermission時傳入 case BAIDU_READ_PHONE_STATE: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 獲取到許可權,作相應處理(呼叫定位SDK應當確保相關許可權均被授權,否則可能引起定位失敗) Toast.makeText(MainActivity.this,“已授權,直接定位3”, Toast.LENGTH_LONG).show(); } else { // 沒有獲取到許可權,做特殊處理 Toast.makeText(getApplicationContext(), “獲取位置許可權失敗,請手動開啟”, Toast.LENGTH_SHORT).show(); } break; default: break; } }