一般anr service超时是多长时间
时间: 2024-03-29 14:34:57 浏览: 13
ANR超时时间可以在Android系统的开发者选项中进行设置,一般默认为5秒钟。这意味着,如果一个服务在5秒钟内无法完成任务并释放主线程,Android系统将会认为该服务已经无响应并触发ANR警告。
值得注意的是,ANR超时时间是可以根据需要进行自定义的。在Android系统的开发者选项中,你可以将ANR超时时间设置为任意值,以适应不同的应用程序和任务需求。但是,在设置ANR超时时间时,需要注意不要将其设置得太短或太长,以免影响应用程序的性能和用户体验。
相关问题
android , 设置ANR 超时时间
在 Android 中,你可以通过设置 ANR(Application Not Responding)超时时间来定义应用程序在主线程上长时间无响应时触发的 ANR 错误。
ANR 超时时间可以在应用程序的 `AndroidManifest.xml` 文件中进行配置。打开该文件,找到 `<application>` 标签,然后添加 `android:timeout` 属性来设置 ANR 超时时间(以毫秒为单位)。
以下是一个示例:
```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:timeout="5000"> <!-- 设置 ANR 超时时间为 5000 毫秒 -->
...
</application>
</manifest>
```
在上面的示例中,我们将 ANR 超时时间设置为 5000 毫秒(即 5 秒)。你可以根据应用程序的需求将超时时间设置为适当的值。
请注意,较长的超时时间可能会导致应用程序在主线程上长时间无响应而触发 ANR 错误。因此,建议根据应用程序的性能和响应需求来设置适当的超时时间。
ActivityManagerService anr 源码
ActivityManagerService是负责监控应用程序的响应情况的服务,当应用程序出现ANR时,该服务会弹出ANR对话框。其ANR相关的源码如下:
1. 监控应用程序响应状态:ActivityManagerService会周期性地检测所有应用程序的响应状态,如果应用程序长时间未响应,就会触发ANR,代码如下:
```
private void checkAppTimeLimitLocked(ProcessRecord app) {
// Check if the app has exceeded its time limits.
if (app.notResponding) {
return; // Don't kill the app if it is already not responding.
}
final boolean isSilentANR = mService.mDidDexOpt;
final long now = SystemClock.uptimeMillis();
final long maxTime = isSilentANR ? mConstants.SANE_ANR_TIMEOUT : mConstants.ANR_TIMEOUT;
final long timeSinceUnfrozen = now - app.lastStateTime;
final boolean overTimeLimit = timeSinceUnfrozen > maxTime;
if (DEBUG_ANR) {
Slog.i(TAG, "checkAppTimeLimitLocked: " + app + " timeSinceUnfrozen=" + timeSinceUnfrozen
+ " maxTime=" + maxTime + " over=" + overTimeLimit);
}
if (overTimeLimit) {
makeAppNotRespondingLocked(app, null, null, 0, "time limit exceeded", isSilentANR);
}
}
```
2. 触发ANR:当应用程序长时间未响应时,ActivityManagerService会调用makeAppNotRespondingLocked方法触发ANR,代码如下:
```
void makeAppNotRespondingLocked(ProcessRecord app, String reason, String shortMsg, int flags,
String message, boolean isSilentANR) {
if (app.notResponding) {
return;
}
mService.addErrorToDropBox("anr", app, app.processName, null, null, reason, message, null, null);
...
// Show the ANR to the user.
Message msg = Message.obtain();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("app", app);
map.put("shortMsg", shortMsg);
map.put("flags", flags);
map.put("isSilentANR", isSilentANR);
msg.obj = map;
mHandler.sendMessage(msg);
...
}
```
3. 弹出ANR对话框:当应用程序出现ANR时,ActivityManagerService会通过Handler向主线程发送消息,弹出ANR对话框,代码如下:
```
private final class UiHandler extends Handler {
public UiHandler() {
super(mService.mHandler.getLooper());
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_NOT_RESPONDING_UI_MSG: {
HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
ProcessRecord app = (ProcessRecord) data.get("app");
String shortMsg = (String) data.get("shortMsg");
int flags = (Integer) data.get("flags");
boolean isSilentANR = (Boolean) data.get("isSilentANR");
...
// Display the ANR dialog.
mActivityManagerInternal.showAppErrorUi(app.pid, app.info.packageName, shortMsg, null,
null, flags, isSilentANR);
...
break;
}
...
}
}
}
```
以上就是ActivityManagerService中ANR相关的源码。