Service

来源:互联网 发布:淘宝特产网 编辑:程序博客网 时间:2024/04/29 13:59

private final HashMap<Context, HashMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>> mReceivers        = new HashMap<Context, HashMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>>();    private final HashMap<Context, HashMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>> mUnregisteredReceivers    = new HashMap<Context, HashMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>>();    private final HashMap<Context, HashMap<ServiceConnection, LoadedApk.ServiceDispatcher>> mServices        = new HashMap<Context, HashMap<ServiceConnection, LoadedApk.ServiceDispatcher>>();    private final HashMap<Context, HashMap<ServiceConnection, LoadedApk.ServiceDispatcher>> mUnboundServices        = new HashMap<Context, HashMap<ServiceConnection, LoadedApk.ServiceDispatcher>>();

 boolean doRebind = s.onUnbind(data.intent);                try {                    if (doRebind) {                        ActivityManagerNative.getDefault().unbindFinished(                                data.token, data.intent, doRebind);                    } else {                        ActivityManagerNative.getDefault().serviceDoneExecuting(                                data.token, 0, 0, 0);                    }                } 

public void serviceDoneExecuting(IBinder token, int type, int startId, int res) {        synchronized(this) {            if (!(token instanceof ServiceRecord)) {                throw new IllegalArgumentException("Invalid service token");            }            ServiceRecord r = (ServiceRecord)token;            boolean inStopping = mStoppingServices.contains(token);            if (r != null) {                if (r != token) {                    Slog.w(TAG, "Done executing service " + r.name                          + " with incorrect token: given " + token                          + ", expected " + r);                    return;                }                if (type == 1) {                    // This is a call from a service start...  take care of                    // book-keeping.                    r.callStart = true;                    switch (res) {                        case Service.START_STICKY_COMPATIBILITY:                        case Service.START_STICKY: {                            // We are done with the associated start arguments.                            r.findDeliveredStart(startId, true);                            // Don't stop if killed.                            r.stopIfKilled = false;                            break;                        }                        case Service.START_NOT_STICKY: {                            // We are done with the associated start arguments.                            r.findDeliveredStart(startId, true);                            if (r.lastStartId == startId) {                                // There is no more work, and this service                                // doesn't want to hang around if killed.                                r.stopIfKilled = true;                            }                            break;                        }                        case Service.START_REDELIVER_INTENT: {                            // We'll keep this item until they explicitly                            // call stop for it, but keep track of the fact                            // that it was delivered.                            ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);                            if (si != null) {                                si.deliveryCount = 0;                                si.doneExecutingCount++;                                // Don't stop if killed.                                r.stopIfKilled = true;                            }                            break;                        }                        default:                            throw new IllegalArgumentException(                                    "Unknown service start result: " + res);                    }                    if (res == Service.START_STICKY_COMPATIBILITY) {                        r.callStart = false;                    }                }                                final long origId = Binder.clearCallingIdentity();                serviceDoneExecutingLocked(r, inStopping);<span style="white-space:pre"></span>//通过bind启动的直接到这儿了                Binder.restoreCallingIdentity(origId);            } else {                Slog.w(TAG, "Done executing unknown service from pid "                        + Binder.getCallingPid());            }        }    }



在ActivityManagerService中:

ActivityRecord activity = null;            if (token != null) {                int aindex = mMainStack.indexOfTokenLocked(token);                if (aindex < 0) {                    Slog.w(TAG, "Binding with unknown activity: " + token);                    return 0;                }                activity = (ActivityRecord)mMainStack.mHistory.get(aindex);<span style="white-space:pre"></span>//找到与service绑定的activity            }

添加绑定信息

 AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp);            ConnectionRecord c = new ConnectionRecord(b, activity,                    connection, flags, clientLabel, clientIntent);            IBinder binder = connection.asBinder();            ArrayList<ConnectionRecord> clist = s.connections.get(binder);            if (clist == null) {                clist = new ArrayList<ConnectionRecord>();                s.connections.put(binder, clist);            }            clist.add(c);            b.connections.add(c);            if (activity != null) {                if (activity.connections == null) {                    activity.connections = new HashSet<ConnectionRecord>();                }                activity.connections.add(c);            }            b.client.connections.add(c);            clist = mServiceConnections.get(binder);            if (clist == null) {                clist = new ArrayList<ConnectionRecord>();                mServiceConnections.put(binder, clist);            }            clist.add(c);
























0 0