List of usage examples for android.os ResultReceiver send
public void send(int resultCode, Bundle resultData)
From source file:com.zapek.android.workerfragment.WorkerFragment.java
/** * To be called by the {@link Service} to send the result back to the * {@link Activity}./*from w ww. j a va 2s . co m*/ * * @param intent * {@link Intent} that the {@link Service} received. * @param resultCode * the result code, used to differentiate what command this * result belongs to * @param result * the {@link Bundle} which contains the data you're interested * in */ public static void sendResult(Intent intent, int resultCode, Bundle result) { ResultReceiver receiver = intent.getParcelableExtra(KEY_RECEIVER); receiver.send(resultCode, result); }
From source file:com.nadmm.airports.utils.NetworkUtils.java
public static boolean doHttpGet(Context context, URL url, File file, ResultReceiver receiver, Bundle result, Class<? extends FilterInputStream> filter) throws Exception { if (!NetworkUtils.isNetworkAvailable(context)) { return false; }//from www . ja va 2 s .c om if (receiver != null && result == null) { throw new Exception("Result cannot be null when receiver is passed"); } InputStream f = null; CountingInputStream in = null; OutputStream out = null; try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int status = conn.getResponseCode(); if (status != HttpURLConnection.HTTP_OK) { if (receiver != null) { // Signal the receiver that download is aborted result.putLong(CONTENT_LENGTH, 0); result.putLong(CONTENT_PROGRESS, 0); receiver.send(2, result); } throw new Exception(conn.getResponseMessage()); } long length = conn.getContentLength(); if (receiver != null) { result.putLong(CONTENT_LENGTH, length); } out = new FileOutputStream(file); in = new CountingInputStream(conn.getInputStream()); if (filter != null) { @SuppressWarnings("unchecked") Constructor<FilterInputStream> ctor = (Constructor<FilterInputStream>) filter .getConstructor(InputStream.class); f = ctor.newInstance(in); } else { f = in; } long chunk = Math.max(length / 100, 16 * 1024); long last = 0; int count; while ((count = f.read(sBuffer)) != -1) { out.write(sBuffer, 0, count); if (receiver != null) { long current = in.getCount(); long delta = current - last; if (delta >= chunk) { result.putLong(CONTENT_PROGRESS, current); receiver.send(0, result); last = current; } } } if (receiver != null) { // If compressed, the filter stream may not read the entire source stream result.putLong(CONTENT_PROGRESS, length); receiver.send(1, result); } } finally { try { if (f != null) { f.close(); } if (out != null) { out.close(); } } catch (IOException ignored) { } } return true; }
From source file:com.savvywits.wethepeople.RESTService.java
@Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras();//from w ww. j a va 2 s . c o m ResultReceiver receiver = extras.getParcelable("receiver"); String data = extras.getString("zipcode"); String url = ZIP_CODE_BASE + data; Bundle bundle = new Bundle(); receiver.send(STATUS_RUNNING, Bundle.EMPTY); try { String json = EntityUtils.toString(new DefaultHttpClient().execute(new HttpGet(url)).getEntity()); if (!validateJSON(json)) { receiver.send(STATUS_ERROR, Bundle.EMPTY); } else { bundle.putString("rest_result", json); receiver.send(STATUS_FINISHED, bundle); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { bundle.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, bundle); } }
From source file:org.androidtitlan.estoesgoogle.service.SyncService.java
@Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "onHandleIntent(intent=" + intent.toString() + ")"); final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER); if (receiver != null) receiver.send(STATUS_RUNNING, Bundle.EMPTY); final SharedPreferences prefs = getSharedPreferences(Prefs.IOSCHED_SYNC, Context.MODE_PRIVATE); try {//from ww w . j a v a2 s .co m // Bulk of sync work, performed by executing several fetches from // local sources. // Load static local data mLocalExecutor.execute(R.xml.blocks, new LocalBlocksHandler()); mLocalExecutor.execute(R.xml.speakers, new LocalSpeakersHandler()); mLocalExecutor.execute(R.xml.rooms, new LocalRoomsHandler()); mLocalExecutor.execute(R.xml.tracks, new LocalTracksHandler()); mLocalExecutor.execute(R.xml.search_suggest, new LocalSearchSuggestHandler()); mLocalExecutor.execute(R.xml.sessions, new LocalSessionsHandler()); // Save local parsed version prefs.edit().putInt(Prefs.LOCAL_VERSION, VERSION_CURRENT).commit(); } catch (Exception e) { Log.e(TAG, "Problem while syncing", e); if (receiver != null) { // Pass back error to surface listener final Bundle bundle = new Bundle(); bundle.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, bundle); } } // Announce success to any surface listener Log.d(TAG, "sync finished"); if (receiver != null) receiver.send(STATUS_FINISHED, Bundle.EMPTY); }
From source file:com.goliathonline.android.kegbot.service.SyncService.java
@Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "onHandleIntent(intent=" + intent.toString() + ")"); final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER); if (receiver != null) receiver.send(STATUS_RUNNING, Bundle.EMPTY); final Context context = this; final SharedPreferences prefs = getSharedPreferences(Prefs.IOSCHED_SYNC, Context.MODE_PRIVATE); final int localVersion = prefs.getInt(Prefs.LOCAL_VERSION, VERSION_NONE); try {/*from w w w. ja v a2 s . co m*/ // Bulk of sync work, performed by executing several fetches from // local and online sources. final long startLocal = System.currentTimeMillis(); final boolean localParse = localVersion < VERSION_CURRENT; Log.d(TAG, "found localVersion=" + localVersion + " and VERSION_CURRENT=" + VERSION_CURRENT); if (localParse) { // Parse values from local cache first, since spreadsheet copy // or network might be down. //mLocalExecutor.execute(context, "cache-drinks.json", new RemoteDrinksHandler()); //mLocalExecutor.execute(context, "cache-speakers.xml", new RemoteSpeakersHandler()); //mLocalExecutor.execute(context, "cache-vendors.xml", new RemoteVendorsHandler()); // Save local parsed version prefs.edit().putInt(Prefs.LOCAL_VERSION, VERSION_CURRENT).commit(); } Log.d(TAG, "local sync took " + (System.currentTimeMillis() - startLocal) + "ms"); // Always hit remote spreadsheet for any updates final long startRemote = System.currentTimeMillis(); mRemoteExecutor.executeGet(PrefsHelper.getAPIUrl(getBaseContext()) + "/events", new RemoteJsonHandler(mRemoteExecutor)); Log.d(TAG, "remote sync took " + (System.currentTimeMillis() - startRemote) + "ms"); } catch (Exception e) { Log.e(TAG, "Problem while syncing", e); if (receiver != null) { // Pass back error to surface listener final Bundle bundle = new Bundle(); bundle.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, bundle); } } // Announce success to any surface listener Log.d(TAG, "sync finished"); if (receiver != null) receiver.send(STATUS_FINISHED, Bundle.EMPTY); }
From source file:com.google.android.apps.iosched.service.SyncService.java
@Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "onHandleIntent(intent=" + intent.toString() + ")"); final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER); if (receiver != null) receiver.send(STATUS_RUNNING, Bundle.EMPTY); final Context context = this; final SharedPreferences prefs = getSharedPreferences(Prefs.IOSCHED_SYNC, Context.MODE_PRIVATE); final int localVersion = prefs.getInt(Prefs.LOCAL_VERSION, VERSION_NONE); try {/*from w w w.j av a 2 s .co m*/ // Bulk of sync work, performed by executing several fetches from // local and online sources. final long startLocal = System.currentTimeMillis(); final boolean localParse = localVersion < VERSION_CURRENT; Log.d(TAG, "found localVersion=" + localVersion + " and VERSION_CURRENT=" + VERSION_CURRENT); if (localParse) { // Load static local data mLocalExecutor.execute(R.xml.blocks, new LocalBlocksHandler()); mLocalExecutor.execute(R.xml.rooms, new LocalRoomsHandler()); mLocalExecutor.execute(R.xml.tracks, new LocalTracksHandler()); mLocalExecutor.execute(R.xml.search_suggest, new LocalSearchSuggestHandler()); mLocalExecutor.execute(R.xml.sessions, new LocalSessionsHandler()); // Parse values from local cache first, since spreadsheet copy // or network might be down. mLocalExecutor.execute(context, "cache-sessions.xml", new RemoteSessionsHandler()); mLocalExecutor.execute(context, "cache-speakers.xml", new RemoteSpeakersHandler()); mLocalExecutor.execute(context, "cache-vendors.xml", new RemoteVendorsHandler()); // Save local parsed version prefs.edit().putInt(Prefs.LOCAL_VERSION, VERSION_CURRENT).commit(); } Log.d(TAG, "local sync took " + (System.currentTimeMillis() - startLocal) + "ms"); // Always hit remote spreadsheet for any updates final long startRemote = System.currentTimeMillis(); mRemoteExecutor.executeGet(WORKSHEETS_URL, new RemoteWorksheetsHandler(mRemoteExecutor)); Log.d(TAG, "remote sync took " + (System.currentTimeMillis() - startRemote) + "ms"); } catch (Exception e) { Log.e(TAG, "Problem while syncing", e); if (receiver != null) { // Pass back error to surface listener final Bundle bundle = new Bundle(); bundle.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, bundle); } } // Announce success to any surface listener Log.d(TAG, "sync finished"); if (receiver != null) receiver.send(STATUS_FINISHED, Bundle.EMPTY); }
From source file:com.kuacm.expo2013.service.SyncService.java
@Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "onHandleIntent(intent=" + intent.toString() + ")"); final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER); if (receiver != null) receiver.send(STATUS_RUNNING, Bundle.EMPTY); final Context context = this; final SharedPreferences prefs = getSharedPreferences(Prefs.IOSCHED_SYNC, Context.MODE_PRIVATE); final int localVersion = prefs.getInt(Prefs.LOCAL_VERSION, VERSION_NONE); try {/*from w w w . java2s . com*/ // Bulk of sync work, performed by executing several fetches from // local and online sources. final long startLocal = System.currentTimeMillis(); final boolean localParse = localVersion < VERSION_CURRENT; Log.d(TAG, "found localVersion=" + localVersion + " and VERSION_CURRENT=" + VERSION_CURRENT); if (localParse) { // Load static local data mLocalExecutor.execute(R.xml.blocks, new LocalBlocksHandler()); mLocalExecutor.execute(R.xml.rooms, new LocalRoomsHandler()); mLocalExecutor.execute(R.xml.tracks, new LocalTracksHandler()); mLocalExecutor.execute(R.xml.search_suggest, new LocalSearchSuggestHandler()); mLocalExecutor.execute(R.xml.sessions, new LocalSessionsHandler()); // Parse values from local cache first, since spreadsheet copy // or network might be down. mLocalExecutor.execute(context, "cache-sessions.xml", new RemoteSessionsHandler()); mLocalExecutor.execute(context, "cache-speakers.xml", new RemoteSpeakersHandler()); mLocalExecutor.execute(context, "cache-vendors.xml", new RemoteVendorsHandler()); // Save local parsed version prefs.edit().putInt(Prefs.LOCAL_VERSION, VERSION_CURRENT).commit(); } Log.d(TAG, "local sync took " + (System.currentTimeMillis() - startLocal) + "ms"); // Always hit remote spreadsheet for any updates final long startRemote = System.currentTimeMillis(); mRemoteExecutor.executeGet(WORKSHEETS_URL, new RemoteWorksheetsHandler(mRemoteExecutor)); Log.d(TAG, "remote sync took " + (System.currentTimeMillis() - startRemote) + "ms"); } catch (Exception e) { Log.e(TAG, "Problem while syncing", e); if (receiver != null) { // Pass back error to surface listener final Bundle bundle = new Bundle(); bundle.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, bundle); } } // Announce success to any surface listener Log.d(TAG, "sync finished"); if (receiver != null) receiver.send(STATUS_FINISHED, Bundle.EMPTY); }
From source file:ca.mudar.parkcatcher.service.SyncService.java
@Override protected void onHandleIntent(Intent intent) { final boolean isLocal = intent.getBooleanExtra(Const.INTENT_EXTRA_SERVICE_LOCAL, false); final boolean isRemote = intent.getBooleanExtra(Const.INTENT_EXTRA_SERVICE_REMOTE, false); final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER); if (receiver != null) { receiver.send(STATUS_RUNNING, Bundle.EMPTY); }/* ww w.ja v a 2 s. c o m*/ try { // Bulk of sync work, performed by executing several fetches from // local and online sources. if (isLocal) { syncLocal(); ((ParkingApp) getApplicationContext()).setHasLoadedData(true); } if (isRemote) { syncRemote(); ((ParkingApp) getApplicationContext()).setHasLoadedData(true); } } catch (HandlerException e) { e.printStackTrace(); if (receiver != null) { /** * Pass back error to surface listener */ final Bundle bundle = new Bundle(); bundle.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, bundle); } } if (receiver != null) { receiver.send(STATUS_FINISHED, Bundle.EMPTY); } }
From source file:com.google.android.apps.iosched2.service.SyncService.java
@Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "onHandleIntent(intent=" + intent.toString() + ")"); final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER); if (receiver != null) receiver.send(STATUS_RUNNING, Bundle.EMPTY); final Context context = this; final SharedPreferences prefs = getSharedPreferences(Prefs.SCHED_SYNC, Context.MODE_PRIVATE); final int localVersion = prefs.getInt(Prefs.LOCAL_VERSION, VERSION_NONE); try {/*from w ww .ja v a2 s . c om*/ // Bulk of sync work, performed by executing several fetches from // local and online sources. final long startLocal = System.currentTimeMillis(); final boolean localParse = localVersion < VERSION_CURRENT; Log.d(TAG, "found localVersion=" + localVersion + " and VERSION_CURRENT=" + VERSION_CURRENT); if (localParse) { // Load static local data mLocalExecutor.execute(Setup.BLOCKS_XML, new LocalBlocksHandler()); mLocalExecutor.execute(Setup.ROOMS_XML, new LocalRoomsHandler()); // mLocalExecutor.execute(R.xml.blocks, new LocalBlocksHandler()); // mLocalExecutor.execute(R.xml.rooms, new LocalRoomsHandler()); mLocalExecutor.execute(R.xml.tracks, new LocalTracksHandler()); mLocalExecutor.execute(R.xml.search_suggest, new LocalSearchSuggestHandler()); mLocalExecutor.execute(R.xml.sessions, new LocalSessionsHandler()); // Parse values from local cache first, since spreadsheet copy // or network might be down. mLocalExecutor.execute(context, Setup.EVENT_PREFIX + "cache-sessions.xml", new RemoteSessionsHandler()); mLocalExecutor.execute(context, Setup.EVENT_PREFIX + "cache-speakers.xml", new RemoteSpeakersHandler()); if (Setup.FEATURE_VENDORS_ON) { mLocalExecutor.execute(context, Setup.EVENT_PREFIX + "cache-vendors.xml", new RemoteVendorsHandler()); } // Save local parsed version prefs.edit().putInt(Prefs.LOCAL_VERSION, VERSION_CURRENT).commit(); } Log.d(TAG, "local sync took " + (System.currentTimeMillis() - startLocal) + "ms"); // Always hit remote spreadsheet for any updates final long startRemote = System.currentTimeMillis(); mRemoteExecutor.executeGet(WORKSHEETS_URL, new RemoteWorksheetsHandler(mRemoteExecutor)); Log.d(TAG, "remote sync took " + (System.currentTimeMillis() - startRemote) + "ms"); } catch (Exception e) { Log.e(TAG, "Problem while syncing", e); if (receiver != null) { // Pass back error to surface listener final Bundle bundle = new Bundle(); bundle.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, bundle); } } // Announce success to any surface listener Log.d(TAG, "sync finished"); if (receiver != null) receiver.send(STATUS_FINISHED, Bundle.EMPTY); }
From source file:ca.mudar.mtlaucasou.services.SyncService.java
@Override protected void onHandleIntent(Intent intent) { final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_STATUS_RECEIVER); if (receiver != null) { receiver.send(STATUS_RUNNING, Bundle.EMPTY); }/*from w w w . j av a2s. co m*/ final Context context = this; try { // Bulk of sync work, performed by executing several fetches from // local and online sources. final long startLocal = System.currentTimeMillis(); /** * Five Assets files to load, so progress goes by 20%. */ Bundle bundle = new Bundle(); bundle.putInt(Const.KEY_BUNDLE_PROGRESS_INCREMENT, 20); // Parse values from local cache first, since SecurityServices copy // or network might be down. receiver.send(STATUS_RUNNING, bundle); mLocalExecutor.execute(context, KmlLocalAssets.FIRE_HALLS, new RemotePlacemarksHandler(FireHalls.CONTENT_URI, true)); receiver.send(STATUS_RUNNING, bundle); mLocalExecutor.execute(context, KmlLocalAssets.SPVM_STATIONS, new RemotePlacemarksHandler(SpvmStations.CONTENT_URI, true)); receiver.send(STATUS_RUNNING, bundle); mLocalExecutor.execute(context, KmlLocalAssets.WATER_SUPPLIES, new RemotePlacemarksHandler(WaterSupplies.CONTENT_URI)); receiver.send(STATUS_RUNNING, bundle); mLocalExecutor.execute(context, KmlLocalAssets.EMERGENCY_HOSTELS, new RemotePlacemarksHandler(EmergencyHostels.CONTENT_URI)); receiver.send(STATUS_RUNNING, bundle); mLocalExecutor.execute(context, KmlLocalAssets.CONDITIONED_PLACES, new RemotePlacemarksHandler(ConditionedPlaces.CONTENT_URI)); Log.v(TAG, "Sync duration: " + (System.currentTimeMillis() - startLocal) + " ms"); // TODO: update data from remote source // Always hit remote SecurityServices for any updates // final long startRemote = System.currentTimeMillis(); // mRemoteExecutor // .executeGet(WORKSHEETS_URL, new // RemoteSecurityServicesHandler(mRemoteExecutor)); // Log.d(TAG, "remote sync took " + (System.currentTimeMillis() - // startRemote) + "ms"); } catch (Exception e) { Log.e(TAG, e.getMessage()); if (receiver != null) { /** * Pass back error to surface listener */ final Bundle bundle = new Bundle(); bundle.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, bundle); } } if (receiver != null) { receiver.send(STATUS_FINISHED, Bundle.EMPTY); } }