1.動(dòng)態(tài)權(quán)限申請(qǐng)
創(chuàng)新互聯(lián)建站是專業(yè)的南澳網(wǎng)站建設(shè)公司,南澳接單;提供成都網(wǎng)站制作、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行南澳網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
模糊的位置信息android.permission.ACCESS_COARSE_LOCATION權(quán)限為例
在AndroidManifest文件中加入權(quán)限
然后java代碼中動(dòng)態(tài)申請(qǐng)
//動(dòng)態(tài)申請(qǐng)權(quán)限的測(cè)試方法 public void test() { // 要申請(qǐng)的權(quán)限 數(shù)組 可以同時(shí)申請(qǐng)多個(gè)權(quán)限 String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION}; if (Build.VERSION.SDK_INT >= 23) { //如果超過(guò)6.0才需要?jiǎng)討B(tài)權(quán)限,否則不需要?jiǎng)討B(tài)權(quán)限 //如果同時(shí)申請(qǐng)多個(gè)權(quán)限,可以for循環(huán)遍歷 int check = ContextCompat.checkSelfPermission(this,permissions[0]); // 權(quán)限是否已經(jīng) 授權(quán) GRANTED---授權(quán) DINIED---拒絕 if (check == PackageManager.PERMISSION_GRANTED) { //寫入你需要權(quán)限才能使用的方法 run(); } else { //手動(dòng)去請(qǐng)求用戶打開權(quán)限(可以在數(shù)組中添加多個(gè)權(quán)限) 1 為請(qǐng)求碼 一般設(shè)置為final靜態(tài)變量 requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1); } } else { //寫入你需要權(quán)限才能使用的方法 run(); } }
其中run()為你自己需要權(quán)限才能執(zhí)行的方法
然后重寫申請(qǐng)權(quán)限的回掉方法
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); //回調(diào),判斷用戶到底點(diǎn)擊是還是否。 //如果同時(shí)申請(qǐng)多個(gè)權(quán)限,可以for循環(huán)遍歷 if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //寫入你需要權(quán)限才能使用的方法 run(); } else { // 沒(méi)有獲取 到權(quán)限,從新請(qǐng)求,或者關(guān)閉app Toast.makeText(this,"需要獲得XXX權(quán)限",Toast.LENGTH_SHORT).show(); } }
2.跳轉(zhuǎn)GPS設(shè)置界面
相關(guān)字符串
提示 當(dāng)前應(yīng)用缺少必要權(quán)限。\n\n請(qǐng)點(diǎn)擊\"設(shè)置\"-\"權(quán)限\"-打開所需權(quán)限。 當(dāng)前應(yīng)用需要打開定位功能。\n\n請(qǐng)點(diǎn)擊\"設(shè)置\"-\"定位服務(wù)\"-打開定位功能。 設(shè)置 取消
java代碼
private int GPS_REQUEST_CODE = 10; /** * 檢測(cè)GPS是否打開 * * @return */ private boolean checkGPSIsOpen() { boolean isOpen; LocationManager locationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); isOpen = locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER); return isOpen; } /** * 跳轉(zhuǎn)GPS設(shè)置 */ private void openGPSSettings() { if (checkGPSIsOpen()) { initLocation(); //自己寫的定位方法 } else { //沒(méi)有打開則彈出對(duì)話框 new AlertDialog.Builder(this) .setTitle(R.string.notifyTitle) .setMessage(R.string.gpsNotifyMsg) // 拒絕, 退出應(yīng)用 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setPositiveButton(R.string.setting, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //跳轉(zhuǎn)GPS設(shè)置界面 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, GPS_REQUEST_CODE); } }) .setCancelable(false) .show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == GPS_REQUEST_CODE) { //做需要做的事情,比如再次檢測(cè)是否打開GPS了 或者定位 openGPSSettings(); } }
最后在需要的地方調(diào)用 openGPSSettings()方法。
效果(是在相關(guān)權(quán)限是已經(jīng)申請(qǐng)好的情況下的效果)
3.下面是我在高德地圖Demo中看見的檢測(cè)權(quán)限的Activity
public class CheckPermissionsActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback { /** * 需要進(jìn)行檢測(cè)的權(quán)限數(shù)組 */ protected String[] needPermissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE }; private static final int PERMISSON_REQUESTCODE = 0; private static final int SETTING_REQUESTCODE = 1; /** * 判斷是否需要檢測(cè),防止不停的彈框 */ private boolean isNeedCheck = true; @Override protected void onResume() { super.onResume(); if (isNeedCheck) { checkPermissions(needPermissions); } } /** * @param permissions * @since 2.5.0 */ private void checkPermissions(String... permissions) { ListneedRequestPermissonList = findDeniedPermissions(permissions); if (null != needRequestPermissonList && needRequestPermissonList.size() > 0) { ActivityCompat.requestPermissions(this, needRequestPermissonList.toArray( new String[needRequestPermissonList.size()]), PERMISSON_REQUESTCODE); } } /** * 獲取權(quán)限集中需要申請(qǐng)權(quán)限的列表 * * @param permissions * @return * @since 2.5.0 */ private List findDeniedPermissions(String[] permissions) { List needRequestPermissonList = new ArrayList (); for (String perm : permissions) { if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED || ActivityCompat.shouldShowRequestPermissionRationale( this, perm)) { needRequestPermissonList.add(perm); } } return needRequestPermissonList; } /** * 檢測(cè)是否說(shuō)有的權(quán)限都已經(jīng)授權(quán) * * @param grantResults * @return * @since 2.5.0 */ private boolean verifyPermissions(int[] grantResults) { for (int result : grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] paramArrayOfInt) { if (requestCode == PERMISSON_REQUESTCODE) { if (!verifyPermissions(paramArrayOfInt)) { showMissingPermissionDialog(); isNeedCheck = false; } } } /** * 顯示提示信息 * * @since 2.5.0 */ private void showMissingPermissionDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.notifyTitle); builder.setMessage(R.string.notifyMsg); // 拒絕, 退出應(yīng)用 builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); builder.setPositiveButton(R.string.setting, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startAppSettings(); } }); builder.setCancelable(false); builder.show(); } /** * 啟動(dòng)應(yīng)用的設(shè)置 * * @since 2.5.0 */ private void startAppSettings() { Intent intent = new Intent( Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, SETTING_REQUESTCODE); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { this.finish(); return true; } return super.onKeyDown(keyCode, event); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == SETTING_REQUESTCODE) { checkPermissions(needPermissions); } } }
繼承于CheckPermissionsActivity即可
不允許就跳轉(zhuǎn)系統(tǒng)設(shè)置界面,若沒(méi)設(shè)置再次檢測(cè)權(quán)限并申請(qǐng),直到允許為止??珊虶PS設(shè)置搭配使用
以上這篇Android 6.0動(dòng)態(tài)權(quán)限及跳轉(zhuǎn)GPS設(shè)置界面的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。