Reputation: 863
I want to ring alarm on exact time but below code is not working on Android 10.
If app is in stack alarm ring successfully but when its not in recent app stack it never run on any os version.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
manager.setAlarmClock(
new AlarmManager.AlarmClockInfo(
timeMillis,
PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0)
),
getIntent(context)
);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
manager.setExact(AlarmManager.RTC_WAKEUP, timeMillis, getIntent(context));
else
manager.set(AlarmManager.RTC_WAKEUP, timeMillis, getIntent(context));
Intent intent = new Intent("android.intent.action.ALARM_CHANGED");
intent.putExtra("alarmSet", true);
context.sendBroadcast(intent);
}
I have added below permission
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SET_ALARM" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.learntodroid.simplealarmclock.permission.ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
What can i do to ring alarm on Android 10?
Upvotes: 1
Views: 1192
Reputation: 945
To schedule an alarm at a specific time try this:
First Schedule an alarm at your desired time using Alarm Manager
void scheduleAlarm(Context context) {
// scheduling an alarm at 05:00 PM
//getting instance of a calendar
Calendar calendar = Calendar.getInstance();
//setting exact date and time of alarm
// in my case it is 17/11/2020 at 05.00 PM
calendar.set(Calendar.MONTH,10); // month count starts from 0; here 10 denotes November
calendar.set(Calendar.YEAR,2020); // year
calendar.set(Calendar.DAY_OF_MONTH,17); // day
calendar.set(Calendar.HOUR_OF_DAY,17); // hour
calendar.set(Calendar.MINUTE,0); // min
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,12345678,intent,PendingIntent.FLAG_CANCEL_CURRENT);
android.app.AlarmManager alarmManager = (android.app.AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),5000,pendingIntent);
}
}
Then receive the alarm at the Broadcast Receiver
class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// this will trigger at the alarm time
// show notification or vibration here
}
}
Don't forget to add the receiver in your Manifest file:
<receiver
android:name=".AlarmReceiver" // need to specify the path of your receiver
android:enabled="true"
android:exported="true">
// this filter is optional, needed if you want to trigger the receiver even after a restart
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This should work for all android versions.
Upvotes: 1
Reputation: 19243
Android 10 won't start Activity
from background task (alarm) if it won't have Intent.FLAG_ACTIVITY_NEW_TASK
. try to replace new Intent(context, MainActivity.class)
with intent
object, which sould be declared as follows:
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
(optionally you may add also Intent.FLAG_ACTIVITY_CLEAR_TOP
)
Upvotes: 0