android - bindService 在什么情况下返回 false?

标签 android android-context

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

我试图让 onBind() 返回 null,但当 bindService 时它仍然返回 true > 被调用并且 onServiceConnected 没有被执行。我也在 Google Groups 上看到了这个,但没有回应 https://groups.google.com/forum/#!topic/android-developers/ZLl56Mz1jYg

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

最佳答案

binderService的实现在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。
此实现在 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    }

所以它最终调用了com.android.server.am.ActiveServices的bindeServiceLocked .

更新:

通过阅读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()的result为null,则返回false。
S查看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,会报错。

所以我尝试删除 AndroidManifest.xml 中的Service declaration 并发现 bindService() 返回 false。

关于android - bindService 在什么情况下返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17780049/

相关文章:

java - 如何使用模型实现简单的 REST 和 ContentProvider 架构?

java - 如何将 Activity 上下文放入非 Activity 类android中?

android - 如何删除错误: create method 'getApplicationContext()' and 'runOnUiThread(Runnable)' when extends Fragment

android - 为 ACTION_SEND Intent 过滤器 uri 获取持久性 URI 权限导致 SecurityException

java - Activity 关闭后显示 AlertDialog

Android:无法检测拨出号码

java - 请帮助我的应用程序在我的手机和模拟器上不断崩溃

Java:读取文件,第一行丢失

android - 写入 : Invalid argument

android - 服务类中的 SharedPreferences 不起作用(空指针)