2013-07-22 14 views
7

मैं सोच रहा था कि Context.bindService() वापसी false कब है?किस मामले में bindService झूठी वापसी करता है?

मैं onBind() वापसी null बनाने के लिए कोशिश की है, लेकिन यह अभी भी true लौटाता है जब bindService कहा जाता है और onServiceConnected निष्पादित नहीं होती है। मैं भी कोई जवाब नहीं https://groups.google.com/forum/#!topic/android-developers/ZLl56Mz1jYg

मैं भी bindService के कार्यान्वयन नहीं मिल सकता है, क्योंकि यह Context.java में और या तो किसी भी उपयोगी परिणाम नहीं है "सार्वजनिक बूलियन bindService" के लिए खोज सार है के साथ Google समूहों पर देखा (निकटतम ApplicationContext था जो वर्तमान API स्तरों में मौजूद प्रतीत नहीं होता है)।

उत्तर

10

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() का परिणाम शून्य है, यह अवास्तविक लौटाते हैं।
SAfter 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     } 

मिले कि अगर सेवा की ResolveInfo नहीं मिल सकता है, यह गलत होगा।

तो मैं करने की कोशिश की AndroidManifest.xml में हटाने सेवा घोषणा और पाते हैं कि bindService() वापसी झूठी।

+0

ActiveServices के स्रोत यह शून्य बाइंडरों स्वीकार करने के लिए और बस ठीक लौटने लगता है के माध्यम से देख के लिए नीचे देखा की तरह कोड जोड़ना चाहिए उस मामले में। अजीब तरह का। –

+0

कृपया मेरा अपडेट देखें, मुझे एक मामला मिला है कि bindService() झूठी वापसी करेगा – Ivan

2

एक बहुत ही सामान्य मामला जिसमें बाइंड सेवा कमजोर पड़ती है वह यह है कि सेवा declared in the Manifest नहीं थी। आप अपने मैनिफ़ेस्ट फ़ाइल

<manifest ... > 
    ... 
    <application ... > 
     <service android:name=".ExampleService" /> 
     ... 
    </application> 
</manifest> 
संबंधित मुद्दे