android使开发人员在应用程序可以使用工具或硬件之前声明许可,我创建了一个类来存储每个权限的描述,例如权限名称,nice Name,nice Seffict许可确实。现在,我可以通过编程性初始化每个对象,从http://developer.android.com/reference/android/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还是数据库存储?这些主要用于根据许可在活动中显示信息。
使用Context.check*
方法(以"检查"开头的Context
对象的方法)检查是否授予给定权限。在此处查看示例。
请注意,在运行时不能添加权限。
目前我能想到的对象数据的最简单方法是将它们写入数据库,将它们序列化为文件,或将键值对编写为共享Preferences。这将取决于您认为更合适的东西。哈希图与持久性无关。您可以选择它作为将数据保存在内存中并在应用程序执行期间访问的方法。
对Android开发人员的培训有一个有关编写键值对和数据库持久性的部分。如果您想使用序列化,以下方法可能很有用:
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;
}