How can I monitor another application's database for changes?

前端 未结 4 2086
北荒
北荒 2021-02-19 10:35

I\'d like to monitor changes to another applications SQLite database. Since the Android doesn\'t allow me to access another application\'s internal data, I need a root applicati

相关标签:
4条回答
  • 2021-02-19 11:36

    Iff you have root access, why not update file permissions to allow group read access to the file in question? i.e.

    • Create a new group if needed e.g. SQLLiteGroup
    • Update group ownership of SQLLite db file to SQLLiteGroup
    • Set file permissions for group read for SQLLite db file
    • App2 add user to group SQLLiteGroup

    App2 user should now have read access to file, FileObserver will work.

    FYI symbolic links mirror (i.e. respect) the permissions of the file that is being pointed to.

    0 讨论(0)
  • 2021-02-19 11:38

    You can't access it from Java.

    One way is poll: to run ls -l /data/data/com.target.app/databases/data.db and get time-stamp periodically.

    Or perhaps use watch or tail commands.

    May be a background service can run this and issue a local broadcast.

    Other way is to ship SQLite3 binary with your app, start process as root, and supply commands to it. I think many apps like titanium etc, do this.

    Update: Here's an SQLite3 project: http://code.google.com/p/sqlite3-android/

    0 讨论(0)
  • 2021-02-19 11:38

    One option would be to encapsulate your database with a content provider, and then the other application can subscribe for changes in the tables/URIs with the ContentObserver class

    0 讨论(0)
  • 2021-02-19 11:42

    inotify is the way to go here but you'll need something like the command-line inotifywait utility from inotify-tools, so that you can elevate it with su.

    You can either write your own (it's super easy) or use an existing port.

    Then, you run it elevated, in a separate thread, and either wait for it to tell you there's a change or to close down (depending on whether you run inotifywatch or inotifywait).

    P.S. This will likely fail miserably on 4.3 onwards with the SELinux contexts and whatnot.

    0 讨论(0)
提交回复
热议问题