本文介绍了在什么情况下,bindService返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Context.bindService()什么时候返回false?

I was wondering when does Context.bindService() return false?

我试图使onBind()返回null,但是当调用bindService并且未执行onServiceConnected时,它仍然返回true.我也在Google网上论坛上看到了此信息,但没有任何回应 https://groups.google.com/forum/#!topic/android -developers/ZLl56Mz1jYg

I've tried to make the onBind() return null, but it still returns true when bindService is called and the onServiceConnected does not get executed. I also saw this on Google Groups with no responsehttps://groups.google.com/forum/#!topic/android-developers/ZLl56Mz1jYg

我也找不到bindService的实现,因为它在Context.java中是抽象的,并且搜索"public boolean bindService"也不会产生任何有用的结果(最近的是ApplicationContext,它似乎不存在于当前的API级别.

I also can't find the implementation of bindService, since it is abstract in Context.java and searching for "public boolean bindService" does not yield any useful results either (closest was ApplicationContext which does not seem to be present in the current API levels).

推荐答案

binderService的实现在android.app.ConntextImpl :

The implementation of binderService is in android.app.ConntextImpl:

1412    public boolean bindService(Intent service, ServiceConnection conn, int flags, int userHandle) {
1413        IServiceConnection sd;
1414        if (conn == null) {
1415            throw new IllegalArgumentException("connection is null");
1416        }
1417        if (mPackageInfo != null) {
1418            sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1419                    mMainThread.getHandler(), flags);
1420        } else {
1421            throw new RuntimeException("Not supported in system context");
1422        }
1423        try {
1424            IBinder token = getActivityToken();
1425            if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1426                    && mPackageInfo.getApplicationInfo().targetSdkVersion
1427                    < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1428                flags |= BIND_WAIVE_PRIORITY;
1429            }
1430            service.setAllowFds(false);
1431            int res = ActivityManagerNative.getDefault().bindService(
1432                mMainThread.getApplicationThread(), getActivityToken(),
1433                service, service.resolveTypeIfNeeded(getContentResolver()),
1434                sd, flags, userHandle);
1435            if (res < 0) {
1436                throw new SecurityException(
1437                        "Not allowed to bind to service " + service);
1438            }
1439            return res != 0;
1440        } catch (RemoteException e) {
1441            return false;
1442        }
1443    }

从1431行开始,您可以看到它调用了ActivityManagerNative的bindService.
此实现位于:

From line 1431, you can see it calls ActivityManagerNative's bindService.
This implementation is in com.android.server.am.ActivityManagerService:

11070    public int bindService(IApplicationThread caller, IBinder token,
11071        Intent service, String resolvedType,
11072        IServiceConnection connection, int flags, int userId) {
11073        enforceNotIsolatedCaller("bindService");
11074        // Refuse possible leaked file descriptors
11075        if (service != null && service.hasFileDescriptors() == true) {
11076            throw new IllegalArgumentException("File descriptors passed in Intent");
11077        }
11078
11079        synchronized(this) {
11080            return mServices.bindServiceLocked(caller, token, service, resolvedType,
11081                connection, flags, userId);
11082        }
11083    }

,因此它最终调用.

更新:

通过阅读binderServiceLocked()的代码:

By reading the code of binderServiceLocked():

472         ServiceLookupResult res =
473             retrieveServiceLocked(service, resolvedType,
474                     Binder.getCallingPid(), Binder.getCallingUid(), userId, true);
475         if (res == null) {
476             return 0;
477         }
478         if (res.record == null) {
479             return -1;
480         }

发现,如果retrieveServiceLocked()的结果为null,它将返回false.
S检查完retrieveServiceLocked()的代码后

Found that if result of retrieveServiceLocked() is null, it will return false.
SAfter checking the code of retrieveServiceLocked()

721                 ResolveInfo rInfo =
722                     AppGlobals.getPackageManager().resolveService(
723                                 service, resolvedType,
724                                 ActivityManagerService.STOCK_PM_FLAGS, userId);
725                 ServiceInfo sInfo =
726                     rInfo != null ? rInfo.serviceInfo : null;
727                 if (sInfo == null) {
728                     Slog.w(TAG, "Unable to start service " + service + " U=" + userId +
729                           ": not found");
730                     return null;
731                 }

发现如果无法获取Service的ResolveInfo,它将为false.

Found that If can't get the ResolveInfo of Service, it will false.

因此,我尝试删除 AndroidManifest.xml 中的服务声明,并发现bindService()返回false.

So I tried to remove the Service declaration in AndroidManifest.xml and find that the bindService() return false.

这篇关于在什么情况下,bindService返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 10:35