gpt4 book ai didi

android - 如何使用 adb 在 Android 11 上触发 BroadcastReceiver?

转载 作者:行者123 更新时间:2023-12-05 00:18:51 24 4
gpt4 key购买 nike

我有两个接收器,我想手动触发,但我似乎做不到。这些是我正在使用的命令:

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

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

我试过添加 -p mypackage 或 -n mypackage/myreceiver 但它们从未被触发。我在 logcat 上也没有看到任何内容。 Adb 返回 result=0,不知道是什么意思。

最佳答案

试试这个。

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

广播类示例。

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//你要启动的类

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

并且不要忘记将其放入您的 list 中。

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

关于android - 如何使用 adb 在 Android 11 上触发 BroadcastReceiver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65901553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com