Reputation: 71
I've made an android app and I want to get local notifications every day at a specific time. I used AlarmManager and BroadcastReceiver to do this. But the problem is the AlarmReceiver class which extends BroadCastReceiver is not being called. I know this is a repeated question in stackoverflow. I've gone through all the answers and tried everything, but still not able to solve the issue.
Here is my Main Activity
package com.dileepmanuballa224.alarm_test;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR,9);
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(getApplicationContext(),AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
}
My AlarmReceiverClass
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "alarm_test_check";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"AlarmReceiver called",Toast.LENGTH_SHORT).show();
Log.d(TAG, "onReceive: called ");
}
}
My Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dileepmanuballa224.alarm_test">
<uses-permission android:name="android.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<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>
I'm testing this app on Android 'Nougat'.
Upvotes: 2
Views: 2908
Reputation: 1144
I was having problem to work with that to. For me it works like this
AlarmReceiver.kt
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
class AlarmReceiver : BroadcastReceiver() {
private var logTag = "logTag-AlarmReceiver"
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == "restart_app") {
val restartApp = context?.packageManager?.getLaunchIntentForPackage(context.packageName)
restartApp?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
restartApp?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context?.startActivity(restartApp)
Log.d(logTag, "${intent.action}")
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="restart_app"/>
</intent-filter>
</receiver>
Set var
import android.app.AlarmManager
import android.app.PendingIntent
import android.os.SystemClock
import android.content.Context
private var alarmMgr: AlarmManager? = null
private lateinit var alarmPendingIntent: PendingIntent
onCreate()
alarmMgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
set alarm
alarmPendingIntent = Intent(this, AlarmReceiver::class.java).let { intent ->
intent.action = "restart_app"
PendingIntent.getBroadcast(this, 0, intent, 0)
}
alarmMgr?.set(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 10 * 1000,
alarmPendingIntent
)
in 10 seconds, our BroadcastReceiver will be fired
Upvotes: 0
Reputation: 71
I found the root cause of the problem and posted another ques. If you want to check the question,you can check from here .
Android studio auto-suggested the wrong permission String. It suggested "android.permission.SET_ALARM", but it should be "com.android.alarm.permission.SET_ALARM". Now everything is working fine.
Upvotes: 0
Reputation: 417
To have alarm in API 23 and greater you have to bypass doze. And to repeat that alarm set alarm for next day at 9 in BroadcastReciver's onReceive()
same way you added this alarm. Check sample code below (taken from this answer) to bypass doze in M and above.
AlarmManager am;
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// Check if current time is smaller than cal time
// otherwise notifications will come same day.
if (System.currentTimeMillis() <= calendar.getTimeInMillis()) {
SyncAlarm(context, calendar.getTimeInMillis());
}
private void syncAlarm(Context context, long time) {
Intent intent = new Intent(this, YourBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
if (Build.VERSION.SDK_INT >= 23) {
// Wakes up the device in Doze Mode
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, // time in millis
pending);
} else if (Build.VERSION.SDK_INT >= 19) {
// Wakes up the device in Idle Mode
am.setExact(AlarmManager.RTC_WAKEUP, time, pending);
} else {
// Old APIs
am.set(AlarmManager.RTC_WAKEUP, time, pending);
}
}
Upvotes: 1
Reputation: 2393
The complete code to schedule Alarm.
Put AlarmScheduler.java
in your project.
AlarmScheduler.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import java.util.Calendar;
import static android.content.Context.ALARM_SERVICE;
public class AlarmScheduler {
public static final int DAILY_REMINDER_REQUEST_CODE = 100;
public static final String TAG = "AlarmScheduler";
public static void setReminder(Context context, int hour, int min) {
Calendar calendar = Calendar.getInstance();
Calendar setcalendar = Calendar.getInstance();
setcalendar.set(Calendar.HOUR_OF_DAY, hour);
setcalendar.set(Calendar.MINUTE, min);
setcalendar.set(Calendar.SECOND, 0);
// cancel already scheduled reminders
cancelReminder(context, AlarmReceiver.class);
if (setcalendar.before(calendar))
setcalendar.add(Calendar.DATE, 1);
// Enable a receiver
ComponentName receiver = new ComponentName(context, AlarmReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent intent1 = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DAILY_REMINDER_REQUEST_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, setcalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
public static void cancelReminder(Context context, Class<?> cls) {
// Disable a receiver
ComponentName receiver = new ComponentName(context, cls);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Intent intent1 = new Intent(context, cls);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DAILY_REMINDER_REQUEST_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
am.cancel(pendingIntent);
pendingIntent.cancel();
}
}
You can schedule Alarm by calling following method in your Activity/Fragment.
int mHour = 10, mMin = 20; // Set hours and minute you want to call onReceive method of your BroadcastReceiver.
AlarmScheduler.setReminder(mContext, mHour, mMin); // Here mContext is Context of Activity. If you use this code in Activity then you can pass ActivityName.this and in Fragment you can do getActivity()
AlarmReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "alarm_test_check";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"AlarmReceiver called",Toast.LENGTH_SHORT).show();
Log.d(TAG, "onReceive: called ");
}
}
Upvotes: 0
Reputation: 2191
As documented same can be done like this
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
This will wake up the device to fire the alarm at approximately 2:00 p.m., and repeat once a day at the same time.
Check official dcoumentation for detailed information.
Hope this helps.
Upvotes: 0
Reputation: 1332
You have to change set timing of calendar and add this, so it will called AlarmReceiver
at 9:00 AM every day
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, (24+9)-(cal.get(Calendar.HOUR_OF_DAY))); // <-- changes this
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(getApplicationContext(),AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
Upvotes: 0