Android对指定的应用屏蔽其ANR的Dialog和crash的Dialog

时间:2025-01-25 07:25:27

修改源码 framework/base/services/core/java/com/android/server/am/

亲试可用:

void handleShowAppErrorUi(Message msg) {
         data = () ;
        boolean showBackground = ((),
                .ANR_SHOW_BACKGROUND, 0) != 0;
        synchronized (mService) {
            ProcessRecord proc = ;
            AppErrorResult res = ;
            if (proc != null &&  != null) {
                (TAG, "App already has crash dialog: " + proc);
                if (res != null) {
                    (AppErrorDialog.ALREADY_SHOWING);
                }
                return;
            }
            boolean isBackground = (()
                    >= Process.FIRST_APPLICATION_UID
                    &&  != MY_PID);
            for (int userId : ()) {
                isBackground &= ( != userId);
            }
            if (isBackground && !showBackground) {
                (TAG, "Skipping crash dialog of " + proc + ": background");
                if (res != null) {
                    (AppErrorDialog.BACKGROUND_USER);
                }
                return;
            }
            final boolean crashSilenced = mAppsNotReportingCrashes != null &&
                    ();
            if ((() || showBackground) && !crashSilenced) {
                 = new AppErrorDialog(mContext, mService, data);
            } else {
                // The device is asleep, so just pretend that the user
                // saw a crash dialog and hit "force quit".
                if (res != null) {
                    (AppErrorDialog.CANT_SHOW);
                }
            }

			//增加包名过滤
			if( != null){
				if(proc != null && ("")){
					 = null;
				}
			}
        }
        // If we've created a crash dialog, show it without the lock held
        if( != null) {
            ();
        }
    }

/********************************************************************/
void handleShowAnrUi(Message msg) {
        Dialog d = null;
        synchronized (mService) {
            HashMap<String, Object> data = (HashMap<String, Object>) ;
            ProcessRecord proc = (ProcessRecord)("app");
            if (proc != null &&  != null) {
                (TAG, "App already has anr dialog: " + proc);
                (mContext, .ACTION_APP_ANR,
                        AppNotRespondingDialog.ALREADY_SHOWING);
                return;
            }

            Intent intent = new Intent("");
            if (!) {
                (Intent.FLAG_RECEIVER_REGISTERED_ONLY
                        | Intent.FLAG_RECEIVER_FOREGROUND);
            }
            (null, null, intent,
                    null, null, 0, null, null, null, AppOpsManager.OP_NONE,
                    null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);

            boolean showBackground = ((),
                    .ANR_SHOW_BACKGROUND, 0) != 0;
            if (() || showBackground) {
                d = new AppNotRespondingDialog(mService,
                        mContext, proc, (ActivityRecord)("activity"),
                        msg.arg1 != 0);
                 = d;
            } else {
                (mContext, .ACTION_APP_ANR,
                        AppNotRespondingDialog.CANT_SHOW);
                // Just kill the app if there is no dialog to be shown.
                (proc, null);
            }
			
			//增加包名过滤
			if(d != null){
				if(proc != null && ("")){
					d = null;
				}
			}
        }
		
        // If we've created a crash dialog, show it without the lock held
        if (d != null) {
            ();
        }
    }