I'm updating my app for Android 13. The app uses an exact alarm.
When the alarm is enabled by the user, the phone will wake up and it will launch my app automatically at the programmed hour. As the time accuracy of this alarm is critical, I use the setExactAndAllowWhileIdle()
function to setup the alarm.
Here is my piece of code (C++Builder, SDK33):
_di_JIntent Intent=TJIntent::Create();
_di_JPendingIntent PendingIntent;
Intent->setClassName(TAndroidHelper::Context, StringToJString("com.embarcadero.firemonkey.FMXNativeActivity"));
if(VERSION_ANDROID<12)
PendingIntent = TJPendingIntent::JavaClass->getActivity(TAndroidHelper::Context, 1, Intent, 0);
else
PendingIntent = TJPendingIntent::JavaClass->getActivity(TAndroidHelper::Context, 1, Intent, TJPendingIntent::JavaClass->FLAG_IMMUTABLE);
tm64 = new_timestamp; //new_timestamp is properly calculated, no issue here
TAndroidHelper::AlarmManager->setExactAndAllowWhileIdle(TJAlarmManager::JavaClass->RTC_WAKEUP,tm64,PendingIntent);
And here are the manifest permissions:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
...
<activity
android:name="com.embarcadero.firemonkey.FMXNativeActivity"
android:label="MyApp"
android:exported="true"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:launchMode="singleTask">
<!-- Tell NativeActivity the name of our .so -->
<meta-data android:name="android.app.lib_name" android:value="MyApp" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
It worked perfectly well with the previous version of Android, however it doesn't wake up the phone & launch the app anymore on my new phone (Samsung Android 13). The "Alarm and reminders" setting is enabled for this app, and I have also set the App's battery usage setting to unrestricted, but no success.
When I check if the alarm has been properly programmed with the adb shell dumpsys alarm
command, the alarm seems properly setup (I have set one at 15:00):
RTC_WAKEUP #15: Alarm{8e3d5e9 type 0 origWhen 1685415625000 whenElapsed 1028338535 com.pyxis.MyApp}
tag=*walarm*:com.pyxis.MyApp/com.embarcadero.firemonkey.FMXNativeActivity
type=RTC_WAKEUP origWhen=2023-05-30 15:00:25.000 window=0 exactAllowReason=permission repeatInterval=0 count=0 flags=0x9
policyWhenElapsed: requester=+23m53s21ms app_standby=-12s957ms device_idle=-- battery_saver=-- tare=-12s957ms gms_manager=--
whenElapsed=+23m53s21ms maxWhenElapsed=+23m53s21ms
operation=PendingIntent{f65426e: PendingIntentRecord{d143b2f com.pyxis.MyApp startActivity}}
idle-options=Bundle[{android.pendingIntent.backgroundActivityAllowed=false, android:broadcast.temporaryAppAllowlistReasonCode=302, android:broadcast.temporaryAppAllowlistDuration=10000, android:broadcast.temporaryAppAllowlistReason=, android:broadcast.temporaryAppAllowlistType=0}]
In the delivery history section, I could find some indication that the alarm was fired
u0a481: [tag=*walarm*:com.pyxis.MyApp/com.embarcadero.firemonkey.FMXNativeActivity origWhen=1685415625000 H=PI:d143b2f elapsed=1028338540 rtc=2023-05-30 15:00:25.005]
But the App was never launched by Android 13.
The logcat file showed that the scheduled alarm as been executed, however I can not see any obvious error or reason why the app has not been properly launched, but I'm not an Android expert.
05-30 15:00:25.009 3008 3074 I ChimeraSystemEventListener: appLaunchIntent package name is: com.pyxis.MyApp
05-30 15:00:25.010 3008 3631 I ActivityTaskManager: START u0 {flg=0x10000000 cmp=com.pyxis.MyApp/com.embarcadero.firemonkey.FMXNativeActivity} from uid 10481
05-30 15:00:25.012 3008 3631 W ActivityTaskManager: Background activity start [callingPackage: com.pyxis.MyApp; callingUid: 10481; appSwitchState: 2; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: NONEXISTENT; isCallingUidPersistentSystemProcess: false; realCallingUid: 1000; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: PERSISTENT; isRealCallingUidPersistentSystemProcess: true; originatingPendingIntent: PendingIntentRecord{c7b08e5 com.pyxis.MyApp startActivity}; allowBackgroundActivityStart: false; intent: Intent { flg=0x10000000 cmp=com.pyxis.MyApp/com.embarcadero.firemonkey.FMXNativeActivity }; callerApp: null; inVisibleTask: false]
05-30 15:00:25.015 3008 3631 D ActivityTaskManager: TaskLaunchParamsModifier:task=null activity=ActivityRecord{d88638b u0 com.pyxis.MyApp/com.embarcadero.firemonkey.FMXNativeActivity} display-from-caller=0 display-id=0 display-windowing-mode=1 suggested-display-area=DefaultTaskDisplayArea@260047950
05-30 15:00:25.015 3008 3631 D [secipm]: mSecIpmManager Preload com.pyxis.MyApp dex files
05-30 15:00:25.016 3008 3631 D ActivityTaskManager: TaskLaunchParamsModifier:task=null activity=ActivityRecord{d88638b u0 com.pyxis.MyApp/com.embarcadero.firemonkey.FMXNativeActivity} t-1} display-from-caller=0 display-id=0 display-windowing-mode=1 suggested-display-area=DefaultTaskDisplayArea@260047950 non-freeform-display display-area=DefaultTaskDisplayArea@260047950 maximized-bounds
05-30 15:00:25.017 3008 3631 E ActivityTaskManager: Abort background activity starts from 10481
05-30 15:00:25.017 3008 18565 D PkgPredictorService-IpmAdcpController: create a new DexPreloadTask pkg:com.pyxis.MyApp path:/data/app/~~xalia7OAHFWvyR5VF9ZSEw==/com.pyxis.MyApp-HJ8-6O36vZZoJVoSXqGryA==
05-30 15:00:25.017 3008 3631 D ActivityTaskManager: startActivity: reason=PendingIntentRecord, result=102
05-30 15:00:25.017 3008 3631 D ActivityManager: Received ACTIVITY intent 0xc7b08e5 Key{startActivity pkg=com.pyxis.MyApp intent=flg=0x10000000 cmp=com.pyxis.MyApp/com.embarcadero.firemonkey.FMXNativeActivity flags=0x4000000 u=0} requestCode=1 res=0 from uid 1000
Is my intent Intent->setClassName(TAndroidHelper::Context, StringToJString("com.embarcadero.firemonkey.FMXNativeActivity"));
properly declared, or are there some flags missing?
Any idea how to solve this issue?
canScheduleExactAlarms()
and theACTION_REQUEST_SCHEDULE_EXACT_ALARM
intent as recommended but unfortunately they have not been added to the last embarcadero androidapi.JNI header files yet. I understand the restriction for distributed app however as my app is a personal app, is there a way to add this permission manually?