Search code examples
androidalarmmanagerintentservice

Android Alarm Manager : OnRecive and PendingIntent Service does not get called


I try to use alarm manager to send an intent at 8:30 but the alarm manager never send an intent to the receiver around 8:30.

I there is something messed up in the setAlarm() method but I could not figure it out. Also many posts here also mentioned the service/receiver might be declared wrong in the manifest file, but I am not sure how to properly include receiver and service in the manifest. I have tried to incorporate the package name but it still does not work.

public class EventAlarmReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
private PendingIntent alarmIntent1;

@Override
public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, EventSchedulingService.class);
    startWakefulService(context, service);
}

public void setAlarm(Context context) {
        alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, EventSchedulingService.class);
        intent.putExtra("silent", true);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        // Set the alarm's trigger time to 8:30 a.m.
        calendar.set(Calendar.HOUR_OF_DAY,8);
        calendar.set(Calendar.MINUTE, 30);
        System.out.println(calendar.getTimeInMillis());
        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
}

Here is my service file

package edu.ssui.smartsilent;

public class EventSchedulingService extends IntentService {

public EventSchedulingService() {
    super("EventSchedulingService");
}
@Override
protected void onHandleIntent(Intent intent) {
    Log.d("SService", "m1");
    AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    if(intent.getExtras().getBoolean("silent")==true){
     mgr.setStreamVolume(AudioManager.STREAM_RING, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
     mgr.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
     mgr.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
     mgr.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
    }
    else
    {
         mgr.setStreamVolume(AudioManager.STREAM_RING, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_RING)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
         mgr.setStreamVolume(AudioManager.STREAM_ALARM, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_ALARM)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
         mgr.setStreamVolume(AudioManager.STREAM_SYSTEM, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_SYSTEM)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
         mgr.setStreamVolume(AudioManager.STREAM_NOTIFICATION, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
    }
    Log.d("SService", "m1");
    EventAlarmReceiver.completeWakefulIntent(intent);

}

}

here is my manifest

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.ssui.smartsilent"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 
<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" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

Solution

  • You are calling:

    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    

    PendingIntent.getBroadcast(context, intent) will retrieve a pendingintent that will perform a broadcast with the context and intent that you passed. So for example it will call Context.sendBroadcast(intent), which will send the intent to all interested BroadcastReceivers.

    Instead of doing:

    Intent intent = new Intent(context, EventSchedulingService.class);
    

    Try passing a BroadcastReceiver to the intent constructor.