android - changing the system settings of a rooted device from inside the app

前端 未结 3 1492
我寻月下人不归
我寻月下人不归 2020-12-13 22:51

I am trying to change the sms limit described here from inside the app. The app is assumed to be running on a rooted device. I\'m using the RootTools to check if the device

相关标签:
3条回答
  • 2020-12-13 23:17

    Yes, You can do this if you have root access. Its a lengthy process but you can do this :

    Step : 1 copy settings.db from /data/data/com.android.providers.settings/databases/ using RootBrowser from the device and than change the value using sqliteBrowser which you want to update than update settings.db and put in assets package of your project

    Step 2 : Copy this file into your application folder (anywhere where you can access) using asset manager for that you need

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

    this function will copy file from assets

    public static void copyAssets(Context context, String assetPath, String outFilename) {
            AssetManager assetManager = context.getAssets();
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(assetPath);
                File outFile = new File(context.getExternalFilesDir(null), outFilename);
    
                out = new FileOutputStream(outFile);
                copyFile(in, out);
            } catch (IOException e) {
                Log.e(TAG, "Failed to copy asset: " + outFilename, e);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
    
    public static void copyFile(InputStream in, OutputStream out) throws IOException {
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
        }
    

    Step 3 : overwrite the system settings.db file system path (destPath) is /data/data/com.android.providers.settings/databases/

    public static void copyToSystem(final String sourceFilePath, final String destPath) {
            Thread background = new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        Process process = Runtime.getRuntime().exec("su");
                        DataOutputStream os = new DataOutputStream(process.getOutputStream());
    //                    
                        os.writeBytes("cp -f " + sourceFilePath + " " + destPath + "\n");
                        os.flush();
                        os.writeBytes("exit\n");
                        os.flush();
                        process.waitFor();
                        process.waitFor();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Log.e(TAG, e.toString());
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.e(TAG, e.toString());
                    }
                }
            });
            background.start();
        }
    

    Step 4 : Reboot device

    That's all done.

    0 讨论(0)
  • 2020-12-13 23:27

    I got this solved by doing this:

    • copy the /data/data/com.android.providers.settings/databases/settings.db to my application folder /data/data/my_app_directory/.
    • update the table in the database using the SQLiteDatabase class.
    • copy back the database from /data/data/my_app_directory/settings.db to /data/data/com.android.providers.settings/databases/

    The copy operations were done by using RootTools.sendShell

    0 讨论(0)
  • 2020-12-13 23:28

    Late answer, but you don't need to copy settings.db, you can update it with RootTools like

    CommandCapture commandLogcat = new CommandCapture(0, "sqlite3 /data/data/com.android.providers.settings/databases/settings.db \"UPDATE secure SET value = '192.168.2.140' WHERE name = 'eth_ip';\"");
    RootTools.getShell(true).add(commandLogcat).getExitCode();
    
    0 讨论(0)
提交回复
热议问题