/** Initiate the service binding protocol. */ @Override public void bindService() { Log.d(TAG, "calling bindService()"); // Launch the Acronym Bound Services if they aren't already // running via a call to bindService(), which binds this // activity to the AcronymService* if they aren't already // bound. if (mServiceConnectionSync.getInterface() == null) mActivity .get() .getApplicationContext() .bindService( AcronymServiceSync.makeIntent(mActivity.get()), mServiceConnectionSync, Context.BIND_AUTO_CREATE); if (mServiceConnectionAsync.getInterface() == null) mActivity .get() .getApplicationContext() .bindService( AcronymServiceAsync.makeIntent(mActivity.get()), mServiceConnectionAsync, Context.BIND_AUTO_CREATE); }
/** Initiate the service unbinding protocol. */ @Override public void unbindService() { if (mActivity.get().isChangingConfigurations()) Log.d(TAG, "just a configuration change - unbindService() not called"); else { Log.d(TAG, "calling unbindService()"); // Unbind the Async Service if it is connected. if (mServiceConnectionAsync.getInterface() != null) mActivity.get().getApplicationContext().unbindService(mServiceConnectionAsync); // Unbind the Sync Service if it is connected. if (mServiceConnectionSync.getInterface() != null) mActivity.get().getApplicationContext().unbindService(mServiceConnectionSync); } }
/* * Initiate the asynchronous acronym lookup when the user presses * the "Look Up Async" button. */ public void expandAcronymAsync(String acronym) { final AcronymRequest acronymRequest = mServiceConnectionAsync.getInterface(); if (acronymRequest != null) { try { // Invoke a one-way AIDL call, which does not block // the client. The results are returned via the // sendResults() method of the mAcronymResults // callback object, which runs in a Thread from the // Thread pool managed by the Binder framework. acronymRequest.expandAcronym(acronym, mAcronymResults); } catch (RemoteException e) { Log.e(TAG, "RemoteException:" + e.getMessage()); } } else { Log.d(TAG, "acronymRequest was null."); } }
/* * Initiate the synchronous acronym lookup when the user presses * the "Look Up Sync" button. */ public void expandAcronymSync(String acronym) { final AcronymCall acronymCall = mServiceConnectionSync.getInterface(); if (acronymCall != null) { // Use an anonymous AsyncTask to download the Acronym data // in a separate thread and then display any results in // the UI thread. new AsyncTask<String, Void, List<AcronymData>>() { /** Acronym we're trying to expand. */ private String mAcronym; /** * Retrieve the expanded acronym results via a synchronous two-way method call, which runs * in a background thread to avoid blocking the UI thread. */ protected List<AcronymData> doInBackground(String... acronyms) { try { mAcronym = acronyms[0]; return acronymCall.expandAcronym(mAcronym); } catch (RemoteException e) { e.printStackTrace(); } return null; } /** Display the results in the UI Thread. */ protected void onPostExecute(List<AcronymData> acronymDataList) { mResults = acronymDataList; mActivity .get() .displayResults(acronymDataList, "no expansions for " + mAcronym + " found"); } // Execute the AsyncTask to expand the acronym without // blocking the caller. }.execute(acronym); } else { Log.d(TAG, "mAcronymCall was null."); } }