Android piles of RuntimePermissions requests, code improving

简介: Android piles of RuntimePermissions requests, code improvingfor example:AndroidManifest.

Android piles of RuntimePermissions requests, code improving


for example:

AndroidManifest.xml:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA" />


java:

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    private final String TAG = "权限";

    //请求码。
    private final int REQUEST_CODE = 0xa01;

    //批量权限组。
    private String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (int i = 0; i < permissions.length; i++) {
                if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, permissions[i] + "未获得授权,请求权限...");
                    ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE);
                } else {
                    Log.d(TAG, permissions[i] + "已获得授权,无需重复请求权限。");
                }
            }
        } else {
            Log.d(TAG, "Android版本低于23,无需运行时请求权限。");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (grantResults != null && permissions != null) {
                    for (int i = 0; i < grantResults.length; i++) {
                        /**
                         * PackageManager.PERMISSION_GRANTED:该值为常量值0,表示权限已经授予。
                         */
                        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                            Log.d(TAG, permissions[i] + " 获得授权。");

                            //在这里开始启动获得授权后的业务逻辑代码。
                        }

                        /**
                         * PackageManager.PERMISSION_DENIED:该值为常量值-1,表示权限未被授予。
                         */
                        if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                            Log.d(TAG, permissions[i] + " 未获得授权。");

                            //在这里开始启动未获得授权后的业务逻辑代码。
                        }
                    }
                }

                break;
        }
    }
}


相关文章
|
5月前
|
Java 开发工具 Android开发
解决flutter doctor出现Android license status unknown或cmdline-tools component is missing
解决flutter doctor出现Android license status unknown或cmdline-tools component is missing
124 4
解决flutter doctor出现Android license status unknown或cmdline-tools component is missing
|
5月前
|
开发工具 Android开发
Android Studio resource linking failed
Android Studio resource linking failed
107 1
|
Java API 开发工具
Building Android notifications 2.3 through to 6.0
Building Android notifications 2.3 through to 6.0
71 0
|
XML API Android开发
Android’s PreferenceActivity for all API versions
Android’s PreferenceActivity for all API versions
66 0
|
Android开发 iOS开发
搞定Android Studio cannot open this project, please retry with Android Studio 3.6 or newer
搞定Android Studio cannot open this project, please retry with Android Studio 3.6 or newer
185 0
搞定Android Studio cannot open this project, please retry with Android Studio 3.6 or newer
|
Android开发
Android Studio 解决Error:(781) Multiple substitutions specified in non-positional format; did you ...
异常信息: /Users/mazaiting/AndroidStudioProjects/WisdomSite/app/build/intermediates/res/merged/debug/values/values.
1506 0
|
安全 Java Android开发
PhotoSharing Part I: Setting up the Photo Sharing Android Application
We will build a photo sharing Android app with real-time image uploading and downloading functionality using Alibaba Cloud OSS.
1796 0
|
测试技术 Android开发
Android Unit Tests
本文主要通过样例来简要介绍Android单元测试基于AndroidTestCase类,主要针对sdk这种不涉及ui的但是需要依赖AndroidSDK的场景,并可以生成代码的覆盖率报告,这也是我们oss-android-sdk中的一个应用,通过这种测试可以保证sdk的稳定运行和性能上的保证,可以增强测试的粒度,目前我们oss-android-sdk覆盖率已达95%。
1732 0