Brandon White
Brandon White

Reputation: 1005

AlarmManager broadcast not being received

I have been trying to slowly learn the Android API over the past month. I have read documentation, viewed examples and can't seem to figure out what I'm doing incorrectly. Basically the app sets an alarm 1 minute exactly after the app was started.

The issue I am currently having is after setting an alarm, it isn't being received. Here's what I have:

SetAlarmActivity.java

public class SetAlarmActivity extends AppCompatActivity {

    private final String TAG = "AlarmTest";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_alarm);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, 1);
        Log.d(TAG, "Alarm time is " + c.getTime());

        // set the intent to launch our SetAlarmActivity
        Intent alarmIntent = new Intent(SetAlarmActivity.this, SetAlarmActivity.class);

        // retrieve a PendingIntent to perform a Broadcast
        PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(SetAlarmActivity.this, 0, alarmIntent, 0);

        // grab the Alarm Service and tell it to call our Intent at the set time
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingAlarmIntent);
        }else{
            alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingAlarmIntent);
        }
    }
}

AlarmReceiver.java

public class AlarmReceiver extends WakefulBroadcastReceiver {
    private final String TAG = "AlarmTest";

    @Override
    public void onReceive(Context context, Intent intent){
        Log.d(TAG, "Received broadcast!");
    }
}

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".SetAlarmActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".AlarmReceiver" />
    </application>

</manifest>

Upvotes: 0

Views: 150

Answers (2)

Baqir
Baqir

Reputation: 717

change

Intent alarmIntent = new Intent(SetAlarmActivity.this, SetAlarmActivity.class);

to

Intent alarmIntent = new Intent(SetAlarmActivity.this, AlarmReceiver.class);

then its works fine

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

You have to create the intent between the Current activity and receiver class. change your code like this.

Intent intent = new Intent(SetAlarmActivity.this, AlarmReceiver.class);

Upvotes: 1

Related Questions