android应用内获取系统权限,来设置一些系统开关

来源:互联网 发布:淘宝购物拒签会怎样 编辑:程序博客网 时间:2024/05/24 00:30
1,需要在 Android.mk 文件内添加属性LOCAL_CERTIFICATE := platform
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := RestartMediaServer
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)


2,需要在 AndroidManifest.xml 文件中添加属性 android:sharedUserId="android.uid.system"
注意添加位置为 manifest 属性,而非里面的 application 属性
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.restartmediaserver"
    android:sharedUserId="android.uid.system"
    android:versionCode="1"
    android:versionName="1.0" >

3,设置函数(这里是通过反射来实现的,要import java.lang.reflect.Method),如set("dump.camera.raw","1")
public static String set(String key, String defaultValue) {
        try {
            final Class<?> systemProperties = Class.forName("android.os.SystemProperties");
            final Method get = systemProperties.getMethod("set", String.class, String.class);
            return (String) get.invoke(null, key, defaultValue);
        } catch (Exception e) {
            // This should never happen
            Log.e(TAG, "Exception while getting system property: ", e);
            return defaultValue;
}
    }

这里只是介绍了其中的一种设置方法。还可以通过 SystemProperties.set(key,value) 来设置。
0 0