在android中编辑launcher.db

时间:2014-03-07 16:26:21

标签: android android-sqlite

我有一个root用户设备和一个系统应用程序。我想从我的应用程序编辑launcher.db文件。启动器db的路径是

  

/data/data/com.android.launcher/databases/launcher.db

我正在尝试编辑此文件,就像普通的* .db文件一样,但它不起作用。我该怎么做才能编辑这个文件?

我的 DBHelper.java 类:

public class DBHelper extends SQLiteOpenHelper{
    static final String DATABASE = "/data/data/com.android.launcher/databases/launcher.db";
    static final int VERSION = 13;
    static final String TABLE = "favorites";
    static final String TABLE_DEPT = "dept";

    static final String C_ID = "_id";
    static final String C_TITLE = "title";
    static final String C_INTENT = "intent";
    static final String C_CONTAINER = "container";
    static final String C_SCREEN = "screen";
    static final String C_CELL_X = "cellX";
    static final String C_CELL_Y = "cellY";
    static final String C_SPAN_X = "spanX";
    static final String C_SPAN_Y = "spanY";
    static final String C_ITEM_TYPE = "itemType";
    static final String C_APP_WIDGET_ID = "appWidgetId";
    static final String C_IS_SHORTCUT = "isShortcut";
    static final String C_ICON_TYPE = "iconType";
    static final String C_ICON_PACKAGE = "icon";
    static final String C_URI = "uri";
    static final String C_DISPLAY_MODE = "displayMode";
    static final String C_ICON = "icon";
    static final String C_ICON_RESOURCE = "iconResource";
    public DBHelper(Context context) {
        super(context, DATABASE, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {


    }

 public void onDowngrade(SQLiteDatabase db) {


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }

}

我的 ShortcutInstaller.java 类:

public class ShortcutInstaller {

    DBHelper helper;
    SQLiteDatabase db;



    Context context;

    public ShortcutInstaller(Context context) {
        this.context = context;
    }

    public void installShortcut() {
        try
        {
            /*Uri data = intent.getData();
            String packageName = data.getEncodedSchemeSpecificPart();
            Log.i("some", packageName);*/
            //Runtime.getRuntime().exec("sqlite3 /data/data/com.android.launcher/databases/launcher.db \"DELETE FROM favorites WHERE _id=100; INSERT INTO favorites VALUES(100,'ImViewer','#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.navigator.imviewer/org.vudroid.core.MainBrowserActivity;end',-100,0,2,3,1,1,0,-1,NULL,0,NULL,NULL,NULL,NULL,NULL);\"");
            //Log.i("some", "Done1!");


            ContentValues values = new ContentValues();
            values.put(DBHelper.C_ID, 100);
            values.put(DBHelper.C_TITLE, "ImViewer");
            values.put(DBHelper.C_INTENT, "#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.navigator.imviewer/org.vudroid.core.MainBrowserActivity;end");
            values.put(DBHelper.C_CONTAINER, -100);
            values.put(DBHelper.C_SCREEN, 2);
            values.put(DBHelper.C_CELL_X, 0);
            values.put(DBHelper.C_CELL_Y, 2);
            values.put(DBHelper.C_SPAN_X, 1);
            values.put(DBHelper.C_SPAN_Y, 1);
            values.put(DBHelper.C_CELL_X, 0);
            values.put(DBHelper.C_ITEM_TYPE, 0);
            values.put(DBHelper.C_APP_WIDGET_ID, -1);
            values.putNull(DBHelper.C_IS_SHORTCUT);
            values.put(DBHelper.C_ICON_TYPE, 0);
            values.putNull(DBHelper.C_ICON_PACKAGE);
            values.putNull(DBHelper.C_ICON_RESOURCE);
            values.putNull(DBHelper.C_ICON);
            values.putNull(DBHelper.C_URI);
            values.putNull(DBHelper.C_DISPLAY_MODE);

            // Call insert method of SQLiteDatabase Class and close after
            // performing task
            db = helper.getWritableDatabase();
            db.insert(DBHelper.TABLE, null, values);
            db.close();


            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            Log.i("some", "Done2!");
            am.restartPackage("com.android.launcher");
            Log.i("some", "Done3!");
        }catch (Exception e)
        {
            Log.i("some", e.toString());
        }
    }

}

1 个答案:

答案 0 :(得分:2)

解决方案如下:

public void executeQuery(int x, int y)
{
    try
            {
                String query = getQuery(2, x, y); 
                Runtime.getRuntime().exec(new String[] {"su", "-c", query});
            }
            catch (Exception e)
            {
                //do something;
            }
}

    public String getQuery(int screen, int x, int y)
        {
            String res = "sqlite3 /data/data/com.android.launcher/databases/launcher.db \"INSERT INTO favorites VALUES (";
            res+=currentId;
            currentId++;
            res+=",'ImViewer','#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.navigator.imviewer/org.vudroid.core.MainBrowserActivity;end',-100,";
            //res+=",'ImViewer','#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;end',-100,";
            res+=screen + "," + x + "," +y;
            res +=",1,1,0,-1,NULL,0,NULL,NULL,NULL,NULL,NULL);\"";
            return res;
        }