I need to add a little resiliency to an existing Android app.
The goal is to listen for unhandled exceptions, log the exception, set an alarm to restart the app, and then kill the process to avoid displaying an error message to the user. I started a new project to prove the concept and it works in the emulator, works on my phone (Android 12) when launched via Android Studio debugger... but it doesn't work when I open the app on my phone normally. App opens, exception event raised, alarm gets scheduled, the app is not launched.
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
// ...
AndroidEnvironment.UnhandledExceptionRaiser += HandleUnhandled;
// ...
}
private void HandleUnhandled(object s, RaiseThrowableEventArgs e)
{
// write logs for e...
var intent = PackageManager.GetLaunchIntentForPackage(PackageName);
var pending = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.CancelCurrent);
var mgr = GetSystemService(AlarmService).JavaCast<AlarmManager>();
mgr.SetExactAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup,
SystemClock.ElapsedRealtime() + 10 * 1000, pending);
Process.KillProcess(Process.MyPid());
}
private void CrashOnClick(object s, EventArgs e)
{
throw new Exception("Yeet!");
}
}
Logcat
2022-08-09 09:40:24.797 1630-4569/? D/CompatibilityChangeReporter: Compat change id reported: 171306433; UID 10237; state: DISABLED
2022-08-09 09:40:24.850 1630-4569/? I/ActivityManager: Process com.companyname.autorestart (pid 12521) has died: fg TOP
2022-08-09 09:40:24.850 1630-3572/? I/WindowManager: WIN DEATH: Window{79a845c u0 com.companyname.autorestart/crc64a059b6e6f75d76b2.MainActivity}
2022-08-09 09:40:24.850 573-573/? W/powerhal-libperfmgr: sched_setattr failed for thread 12554, err=3
2022-08-09 09:40:24.850 573-573/? W/powerhal-libperfmgr: sched_setattr failed for thread 12555, err=3
2022-08-09 09:40:24.850 573-573/? W/powerhal-libperfmgr: sched_setattr failed for thread 12539, err=3
2022-08-09 09:40:24.851 573-573/? W/libprocessgroup: Failed to apply NoResetUclampGrp task profile: No such process
2022-08-09 09:40:24.851 573-573/? W/libprocessgroup: Failed to apply NoResetUclampGrp task profile: No such process
2022-08-09 09:40:24.851 573-573/? W/libprocessgroup: Failed to apply NoResetUclampGrp task profile: No such process
2022-08-09 09:40:24.851 1630-3572/? W/InputManager-JNI: Input channel object '79a845c com.companyname.autorestart/crc64a059b6e6f75d76b2.MainActivity (client)' was disposed without first being removed with the input manager!
2022-08-09 09:40:24.851 1630-1666/? I/libprocessgroup: Successfully killed process cgroup uid 10237 pid 12521 in 0ms
2022-08-09 09:40:24.851 712-712/? I/Zygote: Process 12521 exited due to signal 9 (Killed)
2022-08-09 09:40:24.857 1630-4569/? W/ActivityTaskManager: Force removing ActivityRecord{bae54a6 u0 com.companyname.autorestart/crc64a059b6e6f75d76b2.MainActivity t1429}: app died, no saved state
10 seconds later...
2022-08-09 09:40:34.797 1630-1774/? I/ActivityTaskManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000004 pkg=com.companyname.autorestart cmp=com.companyname.autorestart/crc64a059b6e6f75d76b2.MainActivity (has extras)} from uid 10237
2022-08-09 09:40:34.800 1630-1774/? W/ActivityTaskManager: Background activity start [callingPackage: com.companyname.autorestart; callingUid: 10237; appSwitchState: 2; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: NONEXISTENT; isCallingUidPersistentSystemProcess: false; realCallingUid: 1000; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: PERSISTENT; isRealCallingUidPersistentSystemProcess: true; originatingPendingIntent: PendingIntentRecord{f90f498 com.companyname.autorestart startActivity}; allowBackgroundActivityStart: false; intent: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000004 pkg=com.companyname.autorestart cmp=com.companyname.autorestart/crc64a059b6e6f75d76b2.MainActivity (has extras) }; callerApp: null; inVisibleTask: false]
2022-08-09 09:40:34.803 1630-1774/? E/ActivityTaskManager: Abort background activity starts from 10237
Any ideas why Android doesn't like this scheduled intent?