List of usage examples for android.os Message obtain
public static Message obtain(Handler h, int what)
From source file:org.casquesrouges.missing.HttpAdapter.java
@Override public void run() { handler.sendMessage(Message.obtain(handler, HttpAdapter.START)); httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, 80), new UsernamePasswordCredentials(login, pass)); HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000); try {//from w ww .j ava2 s . c o m HttpResponse response = null; switch (method) { case GET: response = httpClient.execute(new HttpGet(url)); break; case POST: HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(data)); response = httpClient.execute(httpPost); break; case FILE: File input = new File(picFile); HttpPost post = new HttpPost(url); MultipartEntity multi = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); multi.addPart("person_id", new StringBody(Integer.toString(personID))); multi.addPart("creator_id", new StringBody(creator_id)); multi.addPart("file", new FileBody(input)); post.setEntity(multi); Log.d("MISSING", "http FILE: " + url + " pic: " + picFile); response = httpClient.execute(post); break; } processEntity(response); } catch (Exception e) { handler.sendMessage(Message.obtain(handler, HttpAdapter.ERROR, e)); } ConnectionManager.getInstance().didComplete(this); }
From source file:com.hotstar.player.adplayer.utils.http.AsyncHttpConnection.java
public void run() { _handler.sendMessage(Message.obtain(_handler, AsyncHttpConnection.DID_START)); HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. HttpConnectionParams.setConnectionTimeout(httpParameters, _timeout); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for _data. HttpConnectionParams.setSoTimeout(httpParameters, _timeout); _httpClient = new DefaultHttpClient(httpParameters); try {/*from w w w . ja v a 2 s .c o m*/ HttpResponse response = null; switch (_method) { case GET: HttpGet httpGet = new HttpGet(_url); for (Map.Entry<String, String> header : _headers.entrySet()) { httpGet.setHeader(header.getKey(), header.getValue()); } response = _httpClient.execute(httpGet); break; case POST: HttpPost httpPost = new HttpPost(_url); for (Map.Entry<String, String> header : _headers.entrySet()) { httpPost.setHeader(header.getKey(), header.getValue()); } httpPost.setEntity(new StringEntity(_data)); response = _httpClient.execute(httpPost); break; default: throw new IllegalArgumentException("Unsupported HTTP method: " + _method.name()); } processEntity(response.getEntity()); } catch (Exception e) { _handler.sendMessage(Message.obtain(_handler, AsyncHttpConnection.DID_ERROR, e)); } ConnectionManager.getInstance().didComplete(this); }
From source file:com.noah.lol.network.RequestNetwork.java
protected void asyncRequestGet(final String url, final NetworkListener<String> listener) { if (url == null) { return;/*from w w w .j a va 2s.c om*/ } try { environmentCheck(); } catch (EnvironmentException e) { if (listener != null) { e.setStatus(BAD_REQUEST_ENVIRONMENT_CONFIG); listener.onNetworkFail(BAD_REQUEST_ENVIRONMENT_CONFIG, e); } return; } final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Bundle bundle = msg.getData(); switch (msg.what) { case REQUEST_SUCCESS: String responseBody = bundle.getString(RESPONSE_KEY); if (listener != null && responseBody != null) { listener.onSuccess(responseBody); } break; case REQUEST_FAIL: NetworkException e = (NetworkException) bundle.getSerializable(EXCEPTION_KEY); if (listener != null) { listener.onNetworkFail(e.getStatus(), e); } break; } } }; new Thread(new Runnable() { @Override public void run() { String responseBody = null; Message message = new Message(); try { responseBody = syncRequestGet(url); Bundle bundle = new Bundle(); bundle.putString(RESPONSE_KEY, responseBody); message = Message.obtain(handler, REQUEST_SUCCESS); message.setData(bundle); handler.sendMessage(message); } catch (NetworkException e) { Bundle bundle = new Bundle(); bundle.putSerializable(EXCEPTION_KEY, e); message = Message.obtain(handler, REQUEST_FAIL); message.setData(bundle); handler.sendMessage(message); e.printStackTrace(); } } }).start(); }
From source file:com.finchuk.clock2.chronometer.ChronometerNotificationThread.java
@SuppressLint("HandlerLeak") @Override/*from ww w. j a va2 s. co m*/ protected void onLooperPrepared() { // This is called after the looper has completed initializing, but before // it starts looping through its message queue. Right now, there is no // message queue, so this is the place to create it. // By default, the constructor associates this handler with this thread's looper. mHandler = new Handler() { @Override public void handleMessage(Message m) { updateNotification(true); sendMessageDelayed(Message.obtain(this, MSG_WHAT), 1000); } }; // Once the handler is initialized, we may immediately begin our work. mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_WHAT), 1000); }
From source file:org.trace.tracker.Tracker.java
/** * Stops the location and activity tracking modules. *//* w w w. ja va 2 s .com*/ public String stopTracking() { Message msg = Message.obtain(null, TRACETrackerService.TRACETrackerOperations.UNTRACK_ACTION); sendRequest(msg); LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mLocationBroadcastReceiver); String id; synchronized (trackLock) { id = mCurrentTrack; mCurrentTrack = null; } return id; }
From source file:org.peercast.core.PeerCastServiceController.java
/** * PeerCastService???/*from ww w .j a va 2s . c om*/ * *<ul> * <li> * MSG_GET_APPLICATION_PROPERTIES = 0x00;<br> * ????<br> * : getInt("port") ??=0<br><br> * <li> * MSG_GET_CHANNELS = 0x01;<br> * ??????<br> * : nativeGetChannel()?<br> * : Channel.java <br><br> * <li> * MSG_GET_STATS = 0x02;<br> * ?????<br> * : nativeGetStats()?<br> * : Stats.java <br><br> * * @param what MSG_ ?? * @param listener ??Bundle???? */ public void sendCommand(int what, final OnServiceResultListener listener) { if (serverMessenger == null) new IllegalStateException("service not connected."); Message msg = Message.obtain(null, what); msg.replyTo = new Messenger(new Handler(new Handler.Callback() { //Handler??? @Override public boolean handleMessage(Message msg) { listener.onServiceResult(msg.getData()); return true; } })); try { serverMessenger.send(msg); } catch (RemoteException e) { Log.e(TAG, "what=" + what, e); } }
From source file:org.trace.tracker.Tracker.java
/** * Request the most current location.//from ww w . j av a 2s . c o m */ public TraceLocation getLastLocation() { Message msg = Message.obtain(null, TRACETrackerService.TRACETrackerOperations.LAST_LOCATION_ACTION); sendRequest(msg); synchronized (locationQueueLock) { return mCurrentLocation; } }
From source file:com.onebus.zxing.decoding.CaptureActivityHandler.java
public void quitSynchronously() { state = State.DONE;//from w ww . j a v a2 s .c o m CameraManager.get().stopPreview(); Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit); quit.sendToTarget(); try { decodeThread.join(); } catch (InterruptedException e) { // continue } // Be absolutely sure we don't send any queued up messages removeMessages(R.id.decode_succeeded); removeMessages(R.id.decode_failed); }
From source file:com.bangz.shotrecorder.RecordActivity.java
private void doRegisterClient() { Message msg = Message.obtain(null, RecordService.MSG_REGISTER_CLIENT); msg.replyTo = mMessenger;/*from www. java 2s .c o m*/ try { mService.send(msg); Message msggetlaps = Message.obtain(null, RecordService.MSG_LAPS, mSplitManager.getNumbers(), 0); mService.send(msggetlaps); } catch (RemoteException e) { } }
From source file:com.kevelbreh.steamchat.activity.AuthenticationActivity.java
/** * Authenticate the user. Perform actions depending on the required form state and result code * of the Steam API.//from w w w .j a va 2 s . co m */ @OnClick(R.id.authenticate) public void authenticate() { if (isValid()) try { Bundle data = new Bundle(); data.putString("username", getUsername()); data.putString("password", getPassword()); data.putString("guard", getGuard()); data.putString("machine", getMachine()); Message message = Message.obtain(null, SteamService.EVENT_STEAM_USER_LOGIN); message.setData(data); mService.send(message); } catch (RemoteException e) { SteamChat.debug(this, e.getMessage(), e); } }