本文主要是介绍Android Service、 BroadcastReceiver、ContentProvider ANR 原理详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Service ANR:
启动Service 的时候,会调用到ActiveServices 类的 realStartServiceLocked方法。
private final void realStartServiceLocked(ServiceRecord r,ProcessRecord app, boolean execInFg) throws RemoteException {//发送延迟消息bumpServiceExecutingLocked(r, execInFg, "create");}
private final void bumpServiceExecutingLocked(ServiceRecord r, boolean fg, String why) {boolean timeoutNeeded = true;long now = SystemClock.uptimeMillis();if (r.app != null) {r.app.executingServices.add(r);r.app.execServicesFg |= fg;if (timeoutNeeded && r.app.executingServices.size() == 1) {scheduleServiceTimeoutLocked(r.app);}}} else if (r.app != null && fg && !r.app.execServicesFg) {r.app.execServicesFg = true;if (timeoutNeeded) {scheduleServiceTimeoutLocked(r.app);}}r.executeFg |= fg;r.executeNesting++;r.executingStart = now;}
继续调用scheduleServiceTimeoutLocked
void scheduleServiceTimeoutLocked(ProcessRecord proc) {if (proc.executingServices.size() == 0 || proc.thread == null) {return;}Message msg = mAm.mHandler.obtainMessage(ActivityManagerService.SERVICE_TIMEOUT_MSG);msg.obj = proc;mAm.mHandler.sendMessageDelayed(msg,proc.execServicesFg ? SERVICE_TIMEOUT : SERVICE_BACKGROUND_TIMEOUT);}
可以看到,里面拿到AtivityManagerService 的 Mainhandler 发送一个延期消息。
当Service 启动之后,就会移除掉消息。(ActiveServices)见下面:
private void serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying,boolean finishing) { if (r.executeNesting <= 0) {if (r.app != null) { if (r.app.executingServices.size() == 0) { mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app);
BroadcastReceiver ANR:
当处理广播的时候,会走到下面的processNextBroadcastLocked 方法:
final void processNextBroadcastLocked(boolean fromMsg, boolean skipOomAdj) {BroadcastRecord r;// First, deliver any non-serialized broadcasts right away.while (mParallelBroadcasts.size() > 0) {if (mService.mProcessesReady && r.dispatchTime > 0) {long now = SystemClock.uptimeMillis();if ((numReceivers > 0) &&(now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) {broadcastTimeoutLocked(false); // forcibly finish this broadcastforceReceive = true;r.state = BroadcastRecord.IDLE;cancelBroadcastTimeoutLocked();}}
broadcastTimeoutLocked 里面会调用setBroadcastTimeoutLocked 方法,设置超时时间。
setBroadcastTimeoutLocked:
final void setBroadcastTimeoutLocked(long timeoutTime) {if (! mPendingBroadcastTimeoutMessage) {Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this);mHandler.sendMessageAtTime(msg, timeoutTime);mPendingBroadcastTimeoutMessage = true;}}
BroadcastHandler 是自己的Handler,但是Looper 是ActivityManagerService 的 MainHandler 的Looper.
private final class BroadcastHandler extends Handler {public BroadcastHandler(Looper looper) {super(looper, null, true);}@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case BROADCAST_INTENT_MSG: {if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Received BROADCAST_INTENT_MSG");processNextBroadcast(true);} break;case BROADCAST_TIMEOUT_MSG: {synchronized (mService) {broadcastTimeoutLocked(true);}} break;}}}
处理ANR:
final void broadcastTimeoutLocked(boolean fromMsg) {if (!debugging && anrMessage != null) {// Post the ANR to the handler since we do not want to process ANRs while// potentially holding our lock.mHandler.post(new AppNotResponding(app, anrMessage));}}
处理ANR:
private final class AppNotResponding implements Runnable {private final ProcessRecord mApp;private final String mAnnotation;public AppNotResponding(ProcessRecord app, String annotation) {mApp = app;mAnnotation = annotation;}@Overridepublic void run() {mService.mAppErrors.appNotResponding(mApp, null, null, false, mAnnotation);}}
ContentProvider ANR:
ContentProvider Timeout是位于”ActivityManager”线程中的AMS.MainHandler收到CONTENT_PROVIDER_PUBLISH_TIMEOUT_MSG消息时触发。
ContentProvider 超时为CONTENT_PROVIDER_PUBLISH_TIMEOUT = 10s. 这个跟前面的Service和BroadcastQueue完全不同, 由Provider进程启动过程相关.
ContentProvider 是在app 启动的时候,初始化的。初始话的时候,会发一条延迟消息,CONTENT_PROVIDER_PUBLISH_TIMEOUT_MSG
if (providers != null && checkAppInLaunchingProvidersLocked(app)) {Message msg = mHandler.obtainMessage(CONTENT_PROVIDER_PUBLISH_TIMEOUT_MSG);msg.obj = app;mHandler.sendMessageDelayed(msg, CONTENT_PROVIDER_PUBLISH_TIMEOUT);}
当发布成功之后,就会移除掉这条消息。
public final void publishContentProviders(IApplicationThread caller,List<ContentProviderHolder> providers) {if (wasInLaunchingProviders) {mHandler.removeMessages(CONTENT_PROVIDER_PUBLISH_TIMEOUT_MSG, r);}}}}
https://www.jianshu.com/p/1c827675f457
这篇关于Android Service、 BroadcastReceiver、ContentProvider ANR 原理详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!