【问题标题】:BroadcastReceiver works only after first execution of adb shellBroadcastReceiver 仅在第一次执行 adb shell 后工作
【发布时间】:2014-12-16 00:04:43
【问题描述】:

我尝试了许多不同的示例,经过一段时间(长时间)后,我终于(几乎)设法从 BroadcastReceiver 接收日志,之后是通知(通过服务)。尽管如此,问题仍然存在。

当我安装 apk 或构建它并在 USB 上运行它时,BroadcastReceiver 没有收到意图。我尝试手动运行应用程序数十次,然后重新启动(打开/关闭)我的手机 - 没有。之后,我尝试用 adb shell 调试它:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.myapp.example/.BootCompletedReceiver

而且,瞧!有效。我检查了重新启动 - 它有效!在那之后,我重新安装了应用程序,再次 - 没有工作。在我尝试使用 adb shell 之后 - 再次,一切正常。 类似的接缝应该可以工作,但只有在我第一次从上面运行 adb shell 命令之后。

我没有尝试使用 AVD(它在我的机器上太慢),只是使用我的 HTC ONE(我还检查了 HTC ONE 的问题,知道这是某些版本的 HTC 手机的问题 - 没有任何帮助) .

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.example" >

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppThemeWhiteActionBar" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"

        android:screenOrientation="portrait"
        android:theme="@style/AppThemeWhite" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    ...
    <receiver android:name=".BootCompletedReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>
    <receiver android:name=".util.AlarmReceiver"/>
    <service android:name=".NotifyingDailyService" >
    </service>
</application>

这些是 BootCompletedReceiver 和 NotifyingDailyService(与这里的许多示例几乎相同):

public class BootCompletedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    Log.w("boot_broadcast_poc", "starting service...");
    context.startService(new Intent(context, NotifyingDailyService.class));
}

}

public class NotifyingDailyService extends Service {

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setContentTitle(getString(R.string.random_string));
    builder.setContentText(getString(R.string.random_string));
    builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
    mNotificationManager.notify(12345, builder.build());
    return super.onStartCommand(pIntent, flags, startId);
}
}

【问题讨论】:

    标签: android broadcastreceiver android-manifest notificationmanager bootcompleted


    【解决方案1】:

    安装不足以让您的应用开始收听BOOT_COMPLETED 广播。应用程序需要启动一次,即您的自定义应用程序类或条目 Activity 需要启动一次,BroadcastReceiver 才能听到BOOT_COMPLETED 系统广播。出于安全原因,此行为是设计使然。

    您可以在 CommonsWare 的 blog 上阅读更多相关信息。

    【讨论】:

    • 是的,我从 Android 3.1 开始就知道这个问题。问题是我在每次第一次启动之前已经启动了应用程序并运行了几乎所有的活动。只是 adb shell 以某种方式“强制”它开始监听 BOOT_COMPLETED。
    • 我明白了。尝试将 添加到清单中的接收者。
    【解决方案2】:

    有一个选项可以限制 Android 中的启动应用程序。我想您的应用程序也受到限制。转到设置-> 个人-> 启动管理器。在那里您可以允许或限制 boot_completed 广播接收。

    【讨论】:

    • 很少有安卓设备有那个启动管理器。
    猜你喜欢
    • 2012-03-05
    • 2016-05-01
    • 2015-01-24
    • 2020-03-25
    • 2016-02-17
    • 2017-09-19
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    相关资源
    最近更新 更多