华为Y560-L01(Android5.1.1)创建通知时遇RemoteServiceException及Activity消失问题
Hey there, let's tackle this tricky issue you're hitting with the Huawei Y560-L01 running Android 5.1.1! Since it only happens on this specific device and you can't debug it directly, we'll focus on catching crash logs and checking for Android 5.1/Huawei ROM-specific quirks to narrow things down.
1. First, Capture Uncaught Exceptions to Get Crash Logs
Since you suspect an unhandled exception is killing your Activity, the first step is to force log every uncaught error to a local file. This way, even if you can't access the device, the user can send you the log file later.
Add a global exception handler to your Application class:
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); setupCrashLogging(); } private void setupCrashLogging() { Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> { // Write crash log to a local file String logFilePath = getExternalFilesDir(null) + "/app_crash_log.txt"; try (FileWriter writer = new FileWriter(logFilePath, true)) { writer.write("===== Crash Timestamp: " + new Date() + " =====\n"); throwable.printStackTrace(new PrintWriter(writer)); writer.write("\n-------------------------\n"); } catch (IOException e) { e.printStackTrace(); } // Restart the app or exit gracefully Intent restartIntent = new Intent(this, MainActivity.class); restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(restartIntent); android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); }); } }
Don't forget to declare this MyApp class in your AndroidManifest.xml, and add the WRITE_EXTERNAL_STORAGE permission (no dynamic request needed for Android 5.1). Ask the user to trigger the issue, then send you the log file from their device's storage.
2. Check for Android 5.1 & Huawei ROM Notification Pitfalls
Even without logs, we can rule out common issues with old Android versions and Huawei's custom EMUI:
Use
NotificationCompat.Builderfor Compatibility
Skip the nativeNotification.Builderand useNotificationCompat.Builder(from AndroidX or the Support Library) — it automatically handles API differences across versions. For example:NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "dummy_channel_id") .setSmallIcon(R.drawable.ic_notification_icon) .setContentTitle("Your Notification Title") .setContentText("Your Notification Content") .setPriority(NotificationCompat.PRIORITY_DEFAULT); // Show the notification NotificationManagerCompat.from(this).notify(1, notificationBuilder.build());The
dummy_channel_idis ignored on Android 5.1 but keeps your code compatible with newer Android versions.Avoid Unsupported PendingIntent Flags
Android 5.1 doesn't supportFLAG_IMMUTABLEorFLAG_MUTABLE(those were added in API 23). Stick to compatible flags likeFLAG_UPDATE_CURRENTorFLAG_CANCEL_CURRENT:PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, new Intent(this, TargetActivity.class), PendingIntent.FLAG_UPDATE_CURRENT );Check Huawei's Notification Permissions
Huawei's old EMUI builds sometimes restricted notification permissions by default. Ask the user to:- Open Settings > Apps > [Your App] > Notification Management
- Ensure "Allow Notifications" and related toggles are turned on
You can also add code to detect if notifications are enabled and prompt the user to open settings if needed (use reflection or intent-based navigation for Android 5.1).
Move Heavy Work Off the Main Thread
If your notification setup involves loading large bitmaps or network calls, doing this on the main thread can trigger ANRs or cause the system to kill your Activity. Shift these tasks to a background thread usingAsyncTask, coroutines, or a thread pool.
3. Alternative: Get System Logs Remotely
If the user can help, ask them to install a third-party log app like CatLog. Have them run the app in the background, trigger the issue, then export and send you the system logs. This will show you low-level system errors that your app's own log might miss.
内容的提问来源于stack exchange,提问作者Thomas

