安卓的BroadcastReceiver将无法正常工作无法正常、工作、BroadcastReceiver

2023-09-06 00:05:10 作者:心上人

我有一个非常简单的问题,这是推动我坚果。我创建一个BroadcastReceiver,对舱单申报,但它只是不会运行。我试图使它在设备启动触发。这里的code:

I have a really simple problem which is driving me nuts. I am creating a BroadcastReceiver, declaring it on the manifest but it just wont run. I'm trying to make it trigger on device boot. Here's the code:

package com.vullnetdyla.bcreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("ftw", "It worked");
    }
}

和清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vullnetdyla.bcreceiver"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">

    <receiver android:name="com.vullnetdyla.bcreceiver.Receiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

</application>

推荐答案

你有一些活动哪些用户可以启动?

Do you have some Activity which user can launch?

如果这是不是你的问题!由于Android 3.1之后安装的应用程序(包要更具体)是处于停止状态,不接收任何广播。用户必须在以免手动启动一次,使其工作。 请参见关于停止应用程序启动控制释放部分说明的Andr​​oid 3.1 。照片 又见标志 FLAG_INCLUDE_STOPPED_PACKAGES , FLAG_EXCLUDE_STOPPED_PACKAGES 。

If not this is your problem! Since android 3.1 after installation application (package to be more specific) is in stopped state and doesn't receive ANY broadcast. User have to launch it manually at lest once to make it work. See section "Launch controls on stopped applications" in release notes of Android 3.1. See also flags FLAG_INCLUDE_STOPPED_PACKAGES, FLAG_EXCLUDE_STOPPED_PACKAGES.