android-RuntimePermissions示例代码不起作用

时间:2017-12-28 10:34:59

标签: java android android-studio intellij-idea

https://github.com/googlesamples/android-RuntimePermissions/

克隆后,我在Android Studio 2.3.3中导入了代码但是Project> Android中没有显示app文件夹,也没有Android Monitor等其他Android特定选项。 Build Project无法正常工作。

我如何导入如下。 文件>新>导入项目> android-RuntimePermissions> build.gradle ,然后点击确定

我已将Kotlin插件添加到android studio,因为该项目有Kotlin模块。

5 个答案:

答案 0 :(得分:4)

有一个无效的settings.gradle文件,只有tries to import Application个模块(它还不是有效的gradle模块,只要它没有自己的build.gradle文件)。

内部根文件夹settings.gradle文件放入以下行:

include ':kotlinApp'

现在,而不是导入build.gradle导入kotlinApp文件夹:

  

档案 - >打开...... - >机器人-RuntimePermissions主/ kotlinApp

答案 1 :(得分:4)

不要打开repo的根文件夹尝试打开(文件 - >打开)应用程序的kotlin文件夹

enter image description here

如果它提示下载kotlin库,请单击下载

完成后,等待gradle同步完成,之后app模块将可见,您将能够构建和部署应用程序

enter image description here

奖励!

答案 2 :(得分:1)

https://github.com/googlesamples/android-RuntimePermissions/

是一个以Application为模块的android项目的根目录。 打开root android项目,如下所示

Click ok button to open root project

点击确定按钮打开root project

但问题是应用程序模块没有build.gradle文件。所以它不会作为一个模块加载。 在Application文件夹中创建一个build.gradle文件并粘贴以下代码

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.example.android.system.runtimepermissions"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "com.android.support:support-v13:26.0.0"
    compile "com.android.support:appcompat-v7:26.0.0"
    compile 'com.android.support:design:26.0.0'
    testCompile 'junit:junit:4.12'
}
repositories {
    mavenCentral()
}

styles.xml中有关于找不到资源的错误。请更正并使用。

https://github.com/googlesamples/android-RuntimePermissions/tree/master/kotlinApp

kotlinApp是另一个与上述项目无关的文件夹。这是另一个以app为模块的android项目 打开kotlinApp android项目,如下所示:

Open kotlinApp project

点击确定按钮,打开kotlinApp android项目

简而言之,这个存储库包含两个android项目。一个是根文件夹,另一个是 kotlinApp 文件夹

答案 3 :(得分:0)

仅当您想要检查运行时权限时,才能运行此代码

if (ActivityCompat.checkSelfPermission(SplashActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(SplashActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions((SplashActivity.this),
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS);
    }else{
    //ToDo write your code here which will work once you granted The permission.
   Log.e(TAG,"Permission already granted"); 
    }

和on permission permission for result

  @NonNull
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
//                    databaseHelper = new DatabaseHelper(getContext());
//                    getCategory();
                loadDB();
            } else if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) &&
                    ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) &&
                    ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
                // Permission Denied
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("Need Storage Permission");
                builder.setMessage("This app needs storage permission");
                builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();


                        ActivityCompat.requestPermissions(getActivity(),
                                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                                        Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, REQUEST_CODE_ASK_PERMISSIONS);


                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

答案 4 :(得分:0)

在onCreate方法中调用它 if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.KITKAT){

        checkForPermission();
    }

将此代码放在OnCreate之外,然后根据您的需要修改此代码。这是经过验证的代码

@RequiresApi(api = Build.VERSION_CODES.KITKAT)

public boolean checkForPermission() {

    List<String> permissionsNeeded = new ArrayList<String>();

    final List<String> permissionsList = new ArrayList<String>();


    addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    addPermission(permissionsList,Manifest.permission.READ_PHONE_STATE);
    addPermission(permissionsList,Manifest.permission.CALL_PHONE);
    addPermission(permissionsList,Manifest.permission.RECORD_AUDIO);
    addPermission(permissionsList,Manifest.permission.PROCESS_OUTGOING_CALLS);
    addPermission(permissionsList,Manifest.permission.CAPTURE_AUDIO_OUTPUT);
    addPermission(permissionsList,Manifest.permission.READ_SMS);



    if (permissionsList.size() > 0) {
        if (permissionsNeeded.size() > 0) {
            // Need Rationale
            ActivityCompat.requestPermissions(MainActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
                    REQUEST_PERMISSION);

            return true;
        }
        ActivityCompat.requestPermissions(MainActivity.this, permissionsList.toArray(new String[permissionsList.size()]),
                REQUEST_PERMISSION);
        return false;
    }

    return true;

}
private boolean addPermission(List<String> permissionsList, String permission) {
    if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
        permissionsList.add(permission);
        // Check for Rationale Option
        if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission))
            return false;
    }
    return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_PERMISSION: {
            Map<String, Integer> perms = new HashMap<String, Integer>();
            // Initial
            perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.READ_EXTERNAL_STORAGE,PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.READ_SMS,PackageManager.PERMISSION_GRANTED);

            // Fill with results
            for (int i = 0; i < permissions.length; i++)
                perms.put(permissions[i], grantResults[i]);
            if (perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.READ_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.READ_SMS)==PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this, "You can enjoy all features of app now", Toast.LENGTH_SHORT).show();
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    checkForPermission();
                }
            }

        }
        break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}