Sending Intent to Broadcastreceiver from Adb

BroadcastReceiver receive extra with ADB --es not working

I would recommend that you try without whitespace in the first place, or just escape the whitespace, seen also here:

adb command fail to execute if path contain spaces

How can I trigger a BroadcastReceiver on Android 11 using adb?

Try this.

adb -s deviceid shell am broadcast -a android.intent.action.VIEW -n com.mypackage.broadcast/com.mypackage.broadcast.Broadcaster

Example of broadcast class.

import android.content.*;
import android.widget.*;

public final class Broadcaster extends BroadcastReceiver
{
@Override
public final void onReceive(final Context context, final Intent intent) {
intent.setClass(context, Starter.class);
//Note: without this flag android will throw a runtime exception.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {
context.startActivity(intent);
} catch (final Exception e) {
Toast.makeText(context, e.getMessage(), 1) .show();
forceStop();
}
forceStop();
}

private final void forceStop() {
clearAbortBroadcast();
//throw new RuntimeException();
System.exit(0);
}

}

Starter.java //Class that you want to start

public final class Starter extends Activity {
@Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Do something
}
}

And dont forget to put this inside your manifest.

 <application
android:noHistory="true"
android:launchMode="singleInstance"
android:excludeFromRecents="true"
android:exported="true"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar">

<!-- RECEIVER -->
<receiver
android:name="com.mypackage.broadcast.Broadcaster"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<data android:mimeType="*/*" />
</intent-filter>
</receiver>
...

Static BroadcastReceiver not Working after Installation from ADB

Finally got this fixed.

Since Honeycomb, all the freshly installed apps will get into a STOP stage until they are launched for at least one time. Android adds a flag "FLAG_EXCLUDE_STOPPED_PACKAGES" for all broadcast intents, which stops them from reaching the stopped apps.
http://droidyue.com/blog/2014/01/04/package-stop-state-since-android-3-dot-1/

To solve this issue, just simply add the flag "FLAG_INCLUDE_STOPPED_PACKAGES" to the intents we send. In my case, I modify the adb command as

adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages

Sending Intent using ADB

After some trial and error - I figured out the adb command:

adb shell am start -a barcodescanner.RECVR -c android.intent.category.DEFAULT -n WMSMobileApp.WMSMobileApp/wmsmobileapp.activities.MainActivity -e com.motorolasolutions.emdk.datawedge.source scanner -e com.motorolasolutions.emdk.datawedge.data_string 508919007526

adb shell broadcast not work while sendBroadcast(intent) works well

The LocalBroadcastManager is a helper to register for and send broadcasts of Intents to local objects within your process. This has a number limitation compared to Contenxt.sendBroadcast(), one of them is that it is not possible for other applications to send these broadcasts to your app.



Related Topics



Leave a reply



Submit