通过android中的adb shell命令行将json字符串发送到emulator | device

时间:2019-03-14 07:53:11

标签: android shell command-line adb

我正在使用adb shell命令行在Android StudioEmulator之间进行通信。

Activity (例如 String Int ...)开头Bundle extras 我从adb命令行发送,可以

adb shell am start -W -a android.intent.action.VIEW -d "sheme://host/pathPrefix" --es extra_video "videoKey=xxx"

现在我正尝试执行相同的操作,但是使用另一个Bundle extras JSON字符串),

然后,我无法正确使用JSON 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}]

我要获得与输入相同的输出。 (正确的日志输出:data = Bundle[{extra_recommendation_video={"name":"abc","place":"xyz"}]

知道正确的JSON string格式的人们请在命令行中填写,

谢谢

enter image description here

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);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

以下内容在我的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}"

因此,基本上只有空格和逗号似乎需要转义。可能也要撇号。 BroadcastReceiver代码:


    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());
            }

        }
    }

Logcat输出:


    D/MEOW33: MyReceiver: onReceive: action - com.example.TEST_ACTION
    D/MEOW33: MyReceiver: onReceive: jsonStr - {field1: value1, field2:123}
    D/MEOW33: MyReceiver: onReceive: json object created - {"field1":"value1","field2":123}