首页 > 解决方案 > 通过android中的adb shell命令行将json字符串发送到模拟器|设备

问题描述

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

ActivityBundle 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 extrasJSON字符串),

然后,我无法获得正确的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);
        }
    }

}

标签: androidshellcommand-lineadb

解决方案


以下在 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}


推荐阅读