Brian
Brian

Reputation: 7

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>

Upvotes: 0

Views: 161

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006539

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.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93542

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.

Upvotes: 0

Related Questions