Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

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

I have two receivers I want to trigger manually and I can't seem to do it. These are the commands I'm using:

adb -s deviceid shell am broadcast -a action android.intent.action.PHONE_STATE

or

adb -s deviceid shell am broadcast -a action android.intent.action.MEDIA_BUTTON

I've tried adding -p mypackage or -n mypackage/myreceiver and they never get triggered. I also don't see anything on logcat. Adb returns result=0, not sure what that means.

question from:https://stackoverflow.com/questions/65901553/how-can-i-trigger-a-broadcastreceiver-on-android-11-using-adb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...