广播接收器未接收广播

时间:2014-04-06 16:50:32

标签: java android broadcastreceiver

我似乎无法让我的onReceive课程收到我发出的任何广播。我不确定它的代码是否与Android Manifest的问题有关。

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("BROADCAST_RECEIVED", intent.getDataString());
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "");
        wakeLock.acquire();
        wakeLock.release();
        context.startActivity(intent);
    }

}

public void setDayOfWeekAlarm(DayOfWeek day){
    long alarmInMili = 0;
    Intent intent = new Intent(context,AlarmScreenActivity.class);
    alarmInMili = System.currentTimeMillis() + 1000*10;
    Log.i("REGISTER ALARM", String.valueOf(alarmInMili));
    AlarmManager alarmManager = (AlarmManager)  context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() +
            10 * 1000,pi);
}

AndroidManifest

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

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="18" />

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

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

        <receiver
            android:name="com.brianlindsey.AlarmReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>
            </intent-filter>
        </receiver>

    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

除了Gabe的回答,您在Intent中使用的setDayOfWeekAlarm()指向AlarmScreenActivity。这不是您的BroadcastReceiver,也不是您的清单中注册的任何其他组件。

答案 1 :(得分:0)

BOOT_COMPLETED接收器无法接收广播,直到apk中的某个活动至少启动一次。谷歌添加了一个奇怪的规则,以防止人们错误地下载应用程序并让它在启动时运行。