安卓应用无法修改系统设置

4

我正在寻找一些帮助,通过应用程序修改Android系统设置。

我在Kitkat 4.4.2下运行自定义Android应用程序时遇到了一些问题。我不确定我的挫败感的来源,但我认为我在清单中缺少某种安全设置或技巧。

背景:我不是开发人员,但这个项目已经落到了我的手上。在部署我们的平板电脑之前,我们会将我们的MDM和公司apk安装到平板电脑上,并运行一个简单的程序来配置一些系统设置。我们在此过程中对平板电脑进行了root并安装了SU二进制文件。

我已经在我的清单中添加了ACCESS_SUPERUSER,但我没有被SuperSU提示要求为此应用程序请求SU访问权限。此外,如果我尝试运行该应用程序,我会收到“应用程序未安装”的toast消息。(我用APPLICATION替换了应用程序名称)

Java代码

Settings.Global.putInt(MainActivity.this.getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS, 1);
Settings.Global.putInt(MainActivity.this.getContentResolver(), Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 1);
Settings.Secure.putInt(MainActivity.this.getContentResolver(), Settings.Secure.LOCK_PATERN_ENABLED, 0);
Settings.Secure.putInt(MainActivity.this.getContentResolver(), Settings.Secure.LOCK_PATTERN_VISIBLE, 0);
Settings.System.putInt(MainActivity.this.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 300000);
Settings.Secure.putInt(MainActivity.this.getContentResolver(), Settings.Global.ADB_ENABLED, 0);

清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.APPLICATION"
    android:versionCode="1"
    android:versionName="1.2" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:persistent="false"
        android:permission="android.permission.WRITE_SECURE_SETTINGS"
        android:exported="true" >
        <activity
            android:name="com.example.APPLICATION.MainActivity"
            android:label="@string/app_name"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

Logcat错误

11-20 17:17:51.749: E/Launcher(686): 启动器没有权限启动意图{act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.APPLICATION/.MainActivity}。请确保为相应的活动创建一个MAIN意图过滤器,或者使用此活动的导出属性。标记=ApplicationInfo(title=SetSet id=-1 type=0 container=-1 screen=-1 cellX=-1 cellY=-1 spanX=1 spanY=1 dropPos=null) intent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.setset/.MainActivity }

11-20 17:32:16.169: E/AndroidRuntime(4744): java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.setset/com.example.APPLICATION.MainActivity}:java.lang.SecurityException:权限拒绝:写入安全设置需要android.permission.WRITE_SECURE_SETTINGS

2个回答

4

看起来你需要将你的应用程序作为系统应用运行,除了你正在做的所有事情。你遇到了WRITE_SECURE_SETTINGS权限问题,这在这里有详细的解释。

此外,你可能还需要将以下内容添加到你的清单中:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
coreApp="true"
android:sharedUserId="android.uid.system">

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
...

抱歉,我漏掉了一点,我已经尝试将其安装到/system/app/system/priv-app文件夹中,但是我收到了类似的结果。我还尝试在清单中添加标签android:sharedUserId="android.uid.system",但似乎没有什么区别。 - DontCopyThatFloppy
1
太好了,谢谢@The-Metal-Beard。不过遇到了一个新情况。将apk推送到/system/app并重新启动后,我在设备上没有看到该应用程序。它也未列在“设置>>应用程序>>预装”下。可能有一些残留的东西来自以前的安装,阻止了这个吗?此外,尝试进行adb install时出现以下错误:Failure [INSTALL_FAILED_SHARED_USER_INCOMPATIBLE] - DontCopyThatFloppy

1
我的解决方案需要涵盖几个不同的方面。最终我采取的解决方法是以超级用户身份运行应用程序,现在似乎可以正确设置系统设置了。请注意,我正在根设备上运行此应用程序。
首先,在清单文件中,我需要从应用程序标签中删除这一行:
android:permission="android.permission.WRITE_SECURE_SETTINGS"

不确定原因。看起来你不能在应用标签中调用安全权限。
在我的MainActivity中,我在try语句中添加了以下su调用,并添加了设置命令:
Process p = Runtime.getRuntime().exec("su");

感谢@The-Metal-Beard提供的指导!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接