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

时间:2014-01-24 13:08:13

标签: android permissions

Android提供开发人员在应用程序可以使用工具或硬件之前声明权限,我创建了一个类来存储每个权限的描述,如权限名称,名称,描述,如权限所做的。无论如何我现在可以以编程方式初始化每个对象,从http://developer.android.com/reference/android/Manifest.permission.html获取信息。

该课程的代码是

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;
    }



}

我还应该将这些对象存储为hashmap还是存储在数据库中? 这些将主要用于根据权限显示活动中的信息。

1 个答案:

答案 0 :(得分:0)

使用Context.check*方法(Context对象中以“check”开头的方法)来检查是否授予了给定的权限。查看示例here

请注意,无法在运行时添加权限。

目前我能想到的存储对象数据的最简单方法是将它们写入数据库,将它们序列化为文件,或者将键值对写入SharedPreferences。这取决于您认为更合适的内容。 hashmap与持久性无关;您可以选择它作为将数据保存在内存中并在应用程序执行期间访问它的方法。

针对Android开发人员的培训有一个关于writing key-value pairs and database persistence的部分。如果您希望使用序列化,下面的方法可能会有用:

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;
}