简体   繁体   中英

Broadcast Receiver not receivng Broadcasts

I seem to be having trouble getting my onReceive class receive any broadcasts I send out. Im not sure if its my code thats the problem or its a problem with the 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>

In addition to Gabe's answer, the Intent that you are using in setDayOfWeekAlarm() points to AlarmScreenActivity . That is not your BroadcastReceiver , nor is it any other component registered in your manifest.

BOOT_COMPLETED receivers cannot recieve a broadcast until an activity in the apk has been launched at least once. Its a weird rule google added to prevent people from downloading an app by mistake and having it run at boot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM