In my application I need to run some code every midnight. This is the first time I am working on alarm service and Searched the internet for some snippets and examples.
Below is my code and its not working as expected, please help my pointing out my mistake:
SET A REPEATING ALARM
private static void initiateMidnightAlarm(Context context, AlarmManager alarmManager) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent midnightIntent = PendingIntent.getService(context, 0,
new Intent("com.alarms.MidnightProcess"), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, midnightIntent);
}
DECLARE THE RECEIVER IN THE MANIFEST:
<receiver
android:name=".receivers.MidnightProcessReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.alarms.MidnightProcess"/>
</intent-filter>
</receiver>
RECEIVER CODE:
public class MidnightProcessReceiver extends BroadcastReceiver {
public MidnightProcessReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
processMidnightLogic(context);
}
public static void processMidnightLogic(Context context) {
Toast.makeText(context,"MidnightProcessReceiver",Toast.LENGTH_LONG).show();
}
}
UPDATE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appname">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<permission
android:name="com.appname.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.appname.permission.C2D_MESSAGE"/>
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
.
.
..
..
</activity>
<receiver
android:name=".receivers.MidnightProcessReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.alarms.MidnightProcess"/>
</intent-filter>
</receiver>
<receiver
android:name=".receivers.OnBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Please help!
Thanks in advance.
Since you need to fire an implicit broadcast, rather than start a Service, you're retrieving the wrong type of PendingIntent. Change:
PendingIntent midnightIntent = PendingIntent.getService(context, 0,
new Intent("com.alarms.MidnightProcess"), 0);
to:
PendingIntent midnightIntent = PendingIntent.getBroadcast(context, 0,
new Intent("com.alarms.MidnightProcess"), 0);