简体   繁体   English

有没有办法获取有关Android中每个权限的信息

[英]Is there a way to get information about each permission in android

Android provides the developer to declare the permission before an app can uses tools or hardware, I have created a class to store each permission's description like the permission name, nice name , description like what that permission does. Android为开发人员提供了在应用程序可以使用工具或硬件之前声明权限的方法,我创建了一个类来存储每个权限的描述,如权限名称,好看的名称,该权限的描述。 Now is there anyway i can initialize each object programmatically, getting the information from http://developer.android.com/reference/android/Manifest.permission.html . 现在无论如何我都可以通过编程方式初始化每个对象,并从http://developer.android.com/reference/android/Manifest.permission.html获取信息。

The code for the class is 该类的代码是

package org.owasp.seraphimdroid.customclasses;

public class PermissionData {
    private String permission;
    private String permissionName;
    private String description;
    private String regularUseDescription;
    private String maliciousUseDescription;
    private int weight;

    public PermissionData(String permission){
        this.permission = permission;
        setData();
    }

    private void setData(){
        weight = 0;
    }   

    //Getters and setter
    public String getPermissionName() {
        return permissionName;
    }
    public void setPermissionName(String permissionName) {
        this.permissionName = permissionName;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getRegularUseDescription() {
        return regularUseDescription;
    }

    public void setRegularUseDescription(String regularUseDescription) {
        this.regularUseDescription = regularUseDescription;
    }

    public String getMaliciousUseDescription() {
        return maliciousUseDescription;
    }

    public void setMaliciousUseDescription(String maliciousUseDescription) {
        this.maliciousUseDescription = maliciousUseDescription;
    }



}

Also should i store these objects as hashmap or in database? 我还应该将这些对象存储为哈希图还是存储在数据库中? These will mostly be used to display information in activity according to the permission. 这些将主要用于根据许可在活动中显示信息。

Use the Context.check* methods (methods from the Context object that start with "check") for checking if a given permission is granted. 使用Context.check*方法(来自Context对象的以“ check”开头的方法)检查​​是否授予给定的权限。 See an example here . 在这里查看示例。

Please note that permissions cannot be added at runtime. 请注意,不能在运行时添加权限。

The simplest ways to store your objects' data that I can think of at the moment are writing them to a database, serializing them into a file, or writing key-value pairs to SharedPreferences. 我现在想到的最简单的存储对象数据的方法是将它们写入数据库,将它们序列化为文件或将键值对写入SharedPreferences。 It will depend on what you think is more appropriate. 这将取决于您认为更合适的内容。 A hashmap has nothing to do with persistence; 哈希图与持久性无关; you may choose it as the approach to keep your data in memory and access it during the app's execution. 您可以选择它作为将数据保存在内存中并在应用执行期间访问数据的方法。

Training for Android developers has a section on writing key-value pairs and database persistence . 针对Android开发人员的培训中有一节专门介绍键值对和数据库持久性 If you wish to use serialization, the methods below might be useful: 如果您希望使用序列化,则以下方法可能会有用:

private void _serializeObject(Object object, String fileName) {
    String aboluteFilePath = getApplicationContext().getFilesDir().getPath() + File.separator + fileName;
    try {

        FileOutputStream fileOut = new FileOutputStream(absoluteFilePath);
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(object);
        out.close();
        fileOut.close();
    } catch (IOException e) {
        Log.e(TAG, "Error while serializing data to " + absoluteFilePath, e);
    }
}

private Object _deserializeObject(String fileName) {
    Object object = null;
    String absoluteFilePath = getApplicationContext().getFilesDir().getPath() + File.separator + fileName;
    try {
        FileInputStream fileIn = new FileInputStream(absoluteFilePath);
        ObjectInputStream in = new ObjectInputStream(fileIn);
        object = in.readObject();
        in.close();
        fileIn.close();
    } catch (FileNotFoundException e) {
        // You may want to ignore this exception as it will occur the first time the
        // data is deserialized unless you make sure serialization occurs before it.
    } catch (IOException e) {
        Log.e(TAG, IOException.class.getSimpleName() + " while deserializing from " + absoluteFilePath, e);
    } catch (ClassNotFoundException e) {
        Log.e(TAG, ClassNotFoundException.class.getSimpleName() + " while deserializing from " + absoluteFilePath, e);
    }

    return object;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM