Android adb shell am broadcast: Bad component name

六眼飞鱼酱① 提交于 2019-12-28 08:09:49

问题


I am trying to 'emulate' a reboot (or anything else with the adb shell am) and am unable to figure out how to reference my component. Then again, maybe I don't even understand what is meant by a component. Below I first include a few example commands that don't work, then my manifest. Note that StartupReceiver is successfully called when the 'phone' boots. I just want to re-trigger it without a full reboot.

Failed ADB commands:

$ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n net.fstab.checkit_android.StartupReceiver
<help snipped>
Error: Bad component name: net.fstab.checkit_android.StartupReceiver

$ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n .StartupReceiver
<help snipped>
Error: Bad component name: .StartupReceiver

$ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n StartupReceiver
<help snipped>
Error: Bad component name: StartupReceiver

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.fstab.checkit_android" android:installLocation="internalOnly"
    android:versionCode="1" android:versionName="1.0">
    <application android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity android:name=".BaseActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="BasePreferences" />
        <activity android:name="EditActivity" />

        <receiver android:name="StartupReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

        <receiver android:name="NotificationReceiver">
            <intent-filter>
                <action android:name="net.fstab.checkit_android.NotificationReceiver" />
            </intent-filter>
        </receiver>

        <service android:name="StartupService">
            <intent-filter>
                <action android:name="net.fstab.checkit_android.StartupService" />
            </intent-filter>
        </service>
    </application>
    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>

回答1:


You need to specify the package name before the class name (then you may write it without the package) like this:

./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n net.fstab.checkit_android/.StartupReceiver

Practically it turns out that you just have to add a slash after the package name.

You helped me start, I helped you finish :)




回答2:


Broadcast does not require specify any Receiver. This case, please just stroke

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

Hope this help.




回答3:


Some apps may misbehave if BOOT_COMPLETED is received twice, instead limit broadcast to your app only:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p com.example.package



回答4:


Try

adb shell am broadcast \ -a android.intent.action.BOOT_COMPLETED \ -n net.fstab.checkit_android/.StartupReceiver

(note the -n net.fstab.checkit_android/.StartupReceiver) to aim at a specific receiver.

Also make sure your app uses permission to receive specific broadcast intents - in this case it would be

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />



来源:https://stackoverflow.com/questions/5171354/android-adb-shell-am-broadcast-bad-component-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!