天天看點

RK3399 Android7.1系統APP有問題時彈出 沒有響應 關系應用 等待

一、問題描述

android系統運作不正常的時候會彈出以下框框,需要手動選擇。如果想取消彈框,直接關機運作不正常的應用,背景我們自己做守護程式。方法如下

本質是ANR對話框 是類class AppNotRespondingDialog在處理

二、問題定位

點位彈框類在以下路徑

base/services/core/java/com/android/server/am/AppNotRespondingDialog.java +110

關閉應用方法接口如下:

mService.killAppAtUsersRequest(proc, null);

怎麼找到的如下紅色字型:

130 @Override

131 public void onClick(View v) {

132 switch (v.getId()) {

133 case com.android.internal.R.id.aerr_report:

134 mHandler.obtainMessage(WAIT_AND_REPORT).sendToTarget();

135 break;

136 case com.android.internal.R.id.aerr_close:

137 mHandler.obtainMessage(FORCE_CLOSE).sendToTarget();

138 break;

139 case com.android.internal.R.id.aerr_wait:

140 mHandler.obtainMessage(WAIT).sendToTarget();

141 break;

142 default:

143 break;

144 }

145 }

private final Handler mHandler = new Handler() {

148 public void handleMessage(Message msg) {

149 Intent appErrorIntent = null;

150

151 MetricsLogger.action(getContext(), MetricsProto.MetricsEvent.ACTION_APP_ANR,

152 msg.what);

153

154 switch (msg.what) {

155 case FORCE_CLOSE:

156 // Kill the application.

157 mService.killAppAtUsersRequest(mProc, AppNotRespondingDialog.this);

158 break;

159 case WAIT_AND_REPORT:

160 case WAIT:

161 // Continue waiting for the application.

162 synchronized (mService) {

163 ProcessRecord app = mProc;

164

165 if (msg.what == WAIT_AND_REPORT) {

166 appErrorIntent = mService.mAppErrors.createAppErrorIntentLocked(app,

167 System.currentTimeMillis(), null);

168 }

169

170 app.notResponding = false;

171 app.notRespondingReport = null;

172 if (app.anrDialog == AppNotRespondingDialog.this) {

173 app.anrDialog = null;

174 }

175 mService.mServices.scheduleServiceTimeoutLocked(app);

176 }

177 break;

178 }

三、問題解決

搜尋AppNotRespondingDialog ,發現在以下路徑調用

base/services/core/java/com/android/server/am/AppErrors.java +955

分析代碼:

1.不讓框框彈出,直接kill應用。屏蔽紅色代碼,添加藍色代碼即可

950 void handleShowAnrUi(Message msg) {

951 Dialog d = null;

952 synchronized (mService) {

953 HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;

954 ProcessRecord proc = (ProcessRecord)data.get("app");

955 if (proc != null && proc.anrDialog != null) {

956 Slog.e(TAG, "App already has anr dialog: " + proc);

957 MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR,

958 AppNotRespondingDialog.ALREADY_SHOWING);

959 return;

960 }

961

962 Intent intent = new Intent("android.intent.action.ANR");

963 if (!mService.mProcessesReady) {

964 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY

965 | Intent.FLAG_RECEIVER_FOREGROUND);

966 }

967 mService.broadcastIntentLocked(null, null, intent,

968 null, null, 0, null, null, null, AppOpsManager.OP_NONE,

969 null, false, false, MY_PID, Process.SYSTEM_UID, 0 / TODO: Verify /);

970

971 boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),

972 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;

973 if (mService.canShowErrorDialogs() || showBackground) {

//我們修改的内容就在這裡

974 mService.killAppAtUsersRequest(proc, null);

975 // d = new AppNotRespondingDialog(mService,

976 // mContext, proc, (ActivityRecord)data.get("activity"),

977 // msg.arg1 != 0);

978 //proc.anrDialog = d;

979 } else {

980 MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR,

981 AppNotRespondingDialog.CANT_SHOW);

982 // Just kill the app if there is no dialog to be shown.

983 mService.killAppAtUsersRequest(proc, null);

984 }

985 }

986 // If we've created a crash dialog, show it without the lock held

987 if (d != null) { //

988 //d.closeAnrApp();

989 d.show();

990 }

991 }

繼續閱讀