android - 通过android中的adb shell命令行将json字符串发送到模拟器|设备
问题描述
我正在使用命令行在和adb shell
之间进行通信。Android Studio
Emulator
我从Activity
Bundle extras
从命令行发送的( ex. String , Int ...) 开始,没关系。adb
adb shell am start -W -a android.intent.action.VIEW -d "sheme://host/pathPrefix" --es extra_video "videoKey=xxx"
现在我正在尝试做同样的事情,但是使用另一个Bundle extras
(JSON字符串),
然后,我无法获得正确的LOG OUTPUTJSON string
格式。
我不确定我是否准确填写了命令行。
adb shell am start -W -a android.intent.action.VIEW -d "sheme://host/pathPrefix"
--es extra_video "{\"name\":\"abc\",\"place\":\"xyz\"}"
// LOG OUTPUT : data = Bundle[{extra_recommendation_video=name:abc}]
adb shell am start -W -a android.intent.action.VIEW -d "sheme://host/pathPrefix"
--es extra_video "{"name":"abc","place":"xyz"}"
// LOG OUTPUT : data = Bundle[{extra_recommendation_video=name:abc}]
adb shell am start -W -a android.intent.action.VIEW -d "sheme://host/pathPrefix"
--es extra_video "{'name':'abc','place':'xyz'}"
// LOG OUTPUT : data = Bundle[{extra_recommendation_video=name:abc}]
我想得到与 Input 相同的 Output。(正确的日志输出data = Bundle[{extra_recommendation_video={"name":"abc","place":"xyz"}]
:)
知道正确JSON string
格式的人在命令行填写,请详细说明,
谢谢,
p/s:下面的代码是完全正确的,问题只是input
来自命令行。
Manifest.xml
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<!--scheme://host/pathPrefix-->
<data
android:host="host"
android:pathPrefix="/pathPrefix"
android:scheme="scheme" />
</intent-filter>
</activity>
MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(contentView);
if (getIntent() != null) {
Video video = null;
if (getIntent().getExtras() != null) {
String data = getIntent().getExtras().getString(EXTRA_VIDEO);
// LOG OUTPUT HERE IS WRONG FORMAT, NOT IS JSON STRING FORMAT
Log.d(LogcatConstants.LIFE_CYCLE, " data = " + data);
video = TVApp.GSON.fromJson(data, Video.class);
}
if (video != null) {
Intent i = new Intent(this, VideoActivity.class);
i.putExtra(VideoPlayerActivity.EXTRA_VIDEO, gson.toJson(video));
startActivity(i);
}
}
}
解决方案
以下在 BroadcastReceiver 示例中对我有用(也应该对您有用):
adb shell am broadcast -n com.example.myapp/com.example.myapp.MyReceiver -a com.example.TEST_ACTION --es extra_video "{field1:\ value1\,\ field2:123}"
所以基本上只有空格和逗号似乎需要转义。可能还有撇号。广播接收器代码:
public class MyReceiver extends BroadcastReceiver {
public static final String TAG = "MEOW33";
public static final String EXTRA_KEY_VIDEO = "extra_video";
@Override
public void onReceive(Context context, Intent intent) {
if (null == intent)
return;
Log.d(TAG, "MyReceiver: onReceive: action - "+intent.getAction());
if (!intent.hasExtra(EXTRA_KEY_VIDEO)) {
Log.w(TAG, "MyReceiver: onReceive: NO EXTRA, abort");
return;
}
String jsonStr = intent.getStringExtra(EXTRA_KEY_VIDEO);
Log.d(TAG, "MyReceiver: onReceive: jsonStr - "+jsonStr);
try {
JSONObject json = new JSONObject(jsonStr);
Log.d(TAG, "MyReceiver: onReceive: json object created - "+json);
}
catch (JSONException je){
Log.e(TAG, "MyReceiver: onReceive: SHAME ON YOU! exception: "+je.getMessage());
}
}
}
日志输出:
D/MEOW33: MyReceiver: onReceive: action - com.example.TEST_ACTION D/MEOW33: MyReceiver: onReceive: jsonStr - {field1: value1, field2:123} D/MEOW33: MyReceiver: onReceive: json 对象创建 - {"field1":"value1","field2":123}
推荐阅读
- c - What is int (*fp)()
- javascript - 如何在没有控制器的情况下访问视图中的 dataModel
- python - 如何抑制py.test单字符测试结果输出?
- angular - 应该存在的功能未定义?
- mysql - utf-8 charset txt 文件转换为 mysql 无法在 macos 上正确显示
- r - 在 R 中添加两个核密度对象?
- python - Python ftplib:以写入模式打开文件?
- excel - 后期绑定 Microsoft Windows 公共控件 (TreeView) 对象
- azure-active-directory - Azure AD B2C 中的按钮输入类型
- owasp - 使用 ZAP 时出现 SOAP 异常