Sending intent with bundle using console

2019-01-09 16:58发布

问题:

I'm developing an Android app listening for specific intent containing a bundle with some data. I would like to send an intent to my app using adb. I have tried with:

adb shell am startservice -a com.INTENT_NAME -e myBundleName myBundleData com.pkg/com.pkg.cls

but my app recognised it as list of string not as a bundle. Does anyone know how to send intent with bundles using am application? Unfortunately documentation says only about sending lists of string or numbers, nothing about bundle.

回答1:

According to the source code am has no way of accepting input data of the bundle type

Update: In Android 7.0 the intent parameters parsing code has been moved from Am.java to Intent.java and support for more data types (like Array[] and ArrayList<> of basic types) has been added. Unfortunately there is still no support for the Bundle type extras in am command.



回答2:

I was facing the same issue, trying to fake the situation where you've just installed an app after receiving a Facebook App Invite. Couldn't get the shell to work, ended up building a really simple test rig that had one button and handler code like:

    Button button  = (Button)findViewById(R.id.trigger_button);
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setData(Uri.parse("myapp://fb-app-invite"));

            Bundle bundle = new Bundle();
            bundle.putString("target_url", "myapp://fb-app-invite?fromuser=673");
            intent.putExtra("al_applink_data", bundle);

            MainActivity.this.startActivity(intent);
        }
    });


回答3:

You can start it by using the following command: adb shell am startservice -a android.intent.action.MAIN -e "key" "value" -n com.example.test/.TestService Key and value should be your bundle values you want to send. TestService should be your ServiceName Add for your Service in androidmanifest.xml Snippet:

<service android:name=".TestService" android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

</service>