我正在尝试使用adb与android应用程序shared_prefs进行交互。
我们在此示例中使用 VLC。 共享首选项存储在:/data/data/org.videolan.vlc/shared_prefs/org.videolan.vlc_preferences.xml
文件示例是:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="app_theme">1</string>
<boolean name="VideoPaused" value="true" />
<float name="VideoSpeed" value="1.0" />
<string name="media_list">file:////sdcard/ssds/Go.mkv</string>
<long name="position_in_media" value="0" />
<string name="current_media">file:////sdcard/ssds/Go.mkv</string>
<int name="current_settings_version" value="1" />
<boolean name="media_shuffling" value="false" />
<long name="VideoResumeTime" value="0" />
<int name="position_in_media_list" value="0" />
</map>
据此:
--------------------------------------------------------------------------------
Shared Preferences
# replace org.example.app with your application id
# Add a value to default shared preferences.
adb shell 'am broadcast -a org.example.app.sp.PUT --es key key_name --es value "hello world!"'
# Remove a value to default shared preferences.
adb shell 'am broadcast -a org.example.app.sp.REMOVE --es key key_name'
# Clear all default shared preferences.
adb shell 'am broadcast -a org.example.app.sp.CLEAR --es key key_name'
# It's also possible to specify shared preferences file.
adb shell 'am broadcast -a org.example.app.sp.PUT --es name Game --es key level --ei value 10'
# Data types
adb shell 'am broadcast -a org.example.app.sp.PUT --es key string --es value "hello world!"'
adb shell 'am broadcast -a org.example.app.sp.PUT --es key boolean --ez value true'
adb shell 'am broadcast -a org.example.app.sp.PUT --es key float --ef value 3.14159'
adb shell 'am broadcast -a org.example.app.sp.PUT --es key int --ei value 2015'
adb shell 'am broadcast -a org.example.app.sp.PUT --es key long --el value 9223372036854775807'
# Restart application process after making changes
adb shell 'am broadcast -a org.example.app.sp.CLEAR --ez restart true'
-------------------------------------------------------------------------------
我应该能够做类似的事情:
# remove entry position_in_media_list
adb shell am broadcast -a org.videolan.vlc.sp.remove --es map position_in_media_list
# add int entry
adb shell am broadcast -a org.videolan.vlc.sp.PUT --es key bob --ei value 2000
adb shell am broadcast -a org.videolan.vlc.sp.PUT --es map bob --ei value 2000
这个和变体不起作用。我想我可能错误地处理了地图。请问有什么建议吗?
我也看到了那个备忘单。我认为这取决于这个:
https://github.com/jfsso/PreferencesEditor的这种用法并不适用于所有应用程序。仅当另一个应用程序集成了该存储库中的仪器时,您才可以通过这种方式更改另一个应用程序的共享首选项。
可调试应用程序共享首选项文件的简单选项,无需实现其他库。尝试使用 sed
:操作 .xml 文件
$pkgname=com.example.yourapp
adb shell "run-as $pkgname sed -i \"s/old_string/new_string/\" /data/data/$pkgname/shared_prefs/MyPrefs.xml"