List of usage examples for android.os Message getData
public Bundle getData()
From source file:net.carlh.toast.MainActivity.java
private void startClient() { if (client != null) { return;/*from w w w .j av a 2 s .c o m*/ } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); /* Client: this must update the State when it receives new data from the server. We also need to know when the client connects or disconnects. */ try { client = new Client(); client.addHandler(new Handler() { public void handleMessage(Message message) { Bundle data = message.getData(); if (data != null && data.getString("json") != null) { /* Some state changed */ try { state.setFromJSON(new JSONObject(data.getString("json"))); } catch (JSONException e) { } } else { /* Connected or disconnected */ if (getConnected()) { /* Newly connected: ask the server to tell us the basics and then the full temperature history. The full history can be moderately slow to parse as it can be a few hundred kilobytes. */ try { JSONObject json = new JSONObject(); json.put("type", "send_basic"); client.send(json); json.put("type", "send_all"); client.send(json); } catch (JSONException e) { } } } } }); client.start(prefs.getString("hostname", "192.168.1.1"), Integer.parseInt(prefs.getString("port", "80"))); } catch (IOException e) { Log.e("Toast", "IOException in startClient", e); } }
From source file:com.dena.app.usage.watcher.MainActivity.java
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler = new Handler(); startSplash();//from ww w . j a v a 2 s .c om mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager() .findFragmentById(R.id.navigation_drawer); mTitle = getTitle(); mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); WatchReceiver watchReceiver = new WatchReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WatchService.WATCH_ACTION); registerReceiver(watchReceiver, intentFilter); watchReceiver.registerHandler(new Handler() { public void handleMessage(Message msg) { if (null == mSwitchCompat) { return; } Bundle bundle = msg.getData(); String message = bundle.getString(WatchService.WATCH_MESSAGE); if (message.equals(WatchService.WATCH_ACTION)) { mSwitchCompat.setChecked(true); } } }); }
From source file:com.nextgis.firereporter.ScanexSubscriptionItem.java
protected void Prepare(GetFiresService c) { this.c = c;/*w w w .j a v a2s . co m*/ nID = -1; bHasNews = false; mFillDataHandler = new Handler() { public void handleMessage(Message msg) { Bundle resultData = msg.getData(); boolean bHaveErr = resultData.getBoolean(GetFiresService.ERROR); if (bHaveErr) { SendError(resultData.getString(GetFiresService.ERR_MSG)); } else { int nType = resultData.getInt(GetFiresService.SOURCE); String sData = resultData.getString(GetFiresService.JSON); switch (nType) { case 5: FillData(nType, sData); break; default: break; } } }; }; mmoItems = new HashMap<Long, ScanexNotificationItem>(); }
From source file:com.nextgis.uikobserver.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFillDataHandler = new Handler() { public void handleMessage(Message msg) { Bundle resultData = msg.getData(); boolean bHaveErr = resultData.getBoolean("error"); if (bHaveErr) { Toast.makeText(MainActivity.this, getResources().getText(R.string.sFailed), Toast.LENGTH_LONG) .show();//from w w w . j a v a 2s .c om } else { Toast.makeText(MainActivity.this, getResources().getText(R.string.sSuccess), Toast.LENGTH_LONG) .show(); } }; }; sendButton = (Button) findViewById(R.id.sendDataBtn); sendButton.setEnabled(false); sendButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SendData(); } }); tvLoc = (TextView) findViewById(R.id.locationString); tvUIK = (TextView) findViewById(R.id.uikNo); tvEMail = (TextView) findViewById(R.id.userMail); tvNote = (TextView) findViewById(R.id.notesString); locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); currentLocationListener = new CurrentLocationListener(); dfLon = 200; dfLat = 200; sN = (String) getResources().getText(R.string.compas_N); sS = (String) getResources().getText(R.string.compas_S); sW = (String) getResources().getText(R.string.compas_W); sE = (String) getResources().getText(R.string.compas_E); sCoordLat = (String) getResources().getText(R.string.coord_lat); sCoordLon = (String) getResources().getText(R.string.coord_lon); //sUIK, sMail, sNote requestLocationUpdates(); }
From source file:com.iloomo.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {/* ww w .j av a 2s .c om*/ for (int i = 0; i < HttpConstant.CONNECTION_COUNT; i++) { try { request = new HttpPost(url); request.addHeader("Accept-Encoding", "default"); if (parameter != null && parameter.size() > 0) { List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); Set<String> keys = parameter.keySet(); for (String key : keys) { list.add(new BasicNameValuePair(key, String.valueOf(parameter.get(key)))); } ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); } httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { // LogUtil.d("HttpTask", " use GZIPInputStream "); is = new GZIPInputStream(bis); } else { // LogUtil.d("HttpTask", // " not use GZIPInputStream"); is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("999", exception.getMessage()); } break; } catch (Exception e) { if (i == HttpConstant.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("999", exception.getMessage()); } else { Log.d("connection url", "" + i); continue; } } } } catch (IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, HttpConstant.ERROR_MESSAGE); ret = ErrorUtil.errorJson("999", exception.getMessage()); } finally { if (!HttpConstant.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } } super.run(); }
From source file:com.iloomo.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {/*from ww w . j a v a 2 s .c o m*/ if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); Set<String> keys = parameter.keySet(); for (String key : keys) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(EncodeUtils.encode(key)); bulider.append("="); bulider.append(EncodeUtils.encode(String.valueOf(parameter.get(key)))); } url += "?" + bulider.toString(); Log.i("request", url); } for (int i = 0; i < HttpConstant.CONNECTION_COUNT; i++) { try { request = new HttpGet(url); if (HttpConstant.isGzip) { request.addHeader("Accept-Encoding", "gzip"); } else { request.addHeader("Accept-Encoding", "default"); } // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); // ? httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); // ByteArrayOutputStream content = new // ByteArrayOutputStream(); // response.getEntity().writeTo(content); // ret = new String(content.toByteArray()).trim(); // content.close(); } else { ret = ErrorUtil.errorJson("" + statusCode, "??,??" + statusCode); continue; } break; } catch (Exception e) { if (i == HttpConstant.CONNECTION_COUNT - 1) { ret = ErrorUtil.errorJson("999", ""); } else { Log.d("connection url", "" + i); continue; } } } } catch (IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, HttpConstant.ERROR_MESSAGE); ret = ErrorUtil.errorJson("999", exception.getMessage()); } finally { if (!HttpConstant.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } } super.run(); }
From source file:ti.modules.titanium.geolocation.TiLocation.java
public void reverseGeocode(double latitude, double longitude, GeocodeResponseHandler responseHandler) { String geocoderUrl = buildGeocoderURL(TiC.PROPERTY_REVERSE, mobileId, appGuid, sessionId, latitude + "," + longitude, countryCode); if (geocoderUrl != null) { Message message = runtimeHandler.obtainMessage(MSG_LOOKUP); message.getData().putString(TiC.PROPERTY_DIRECTION, TiC.PROPERTY_REVERSE); message.getData().putString(TiC.PROPERTY_URL, geocoderUrl); message.obj = responseHandler;// w w w. j a v a2 s .co m message.sendToTarget(); } else { Log.e(TAG, "Unable to reverse geocode, geocoder url is null"); } }
From source file:org.anhonesteffort.flock.CalendarCopyService.java
@Override public int onStartCommand(Intent intent, int flags, int startId) { Message msg = serviceHandler.obtainMessage(); msg.getData().putParcelable(KEY_INTENT, intent); serviceHandler.sendMessage(msg);/*from www . j a v a 2s .c om*/ return START_STICKY; }
From source file:com.spoiledmilk.ibikecph.login.HTTPAccountHandler.java
public static Message performPutUser(UserData userData) { Message message = new Message(); JsonNode result = null;// w w w. j a v a2 s . c om JSONObject jsonPOST = new JSONObject(); JSONObject jsonUser = new JSONObject(); JSONObject jsonImagePath = new JSONObject(); try { jsonUser.put("name", userData.getName()); jsonUser.put("email", userData.getEmail()); jsonUser.put("password", userData.getPassword()); jsonUser.put("password_confirmation", userData.getPassword()); if (userData.getBase64Image() != null && !userData.getBase64Image().trim().equals("")) { jsonImagePath.put("filename", userData.getImageName()); jsonImagePath.put("original_filename", userData.getImageName()); jsonImagePath.put("file", userData.getBase64Image()); jsonUser.put("image_path", jsonImagePath); } jsonPOST.put("user", jsonUser); jsonPOST.put("id", userData.getId()); jsonPOST.put("auth_token", userData.getAuth_token()); result = HttpUtils.putToServer(Config.API_SERVER_REGISTER, jsonPOST); message = HttpUtils.JSONtoMessage(result); message.getData().putInt("type", PUT_USER); } catch (JSONException e) { LOG.e(e.getLocalizedMessage()); message.getData().putInt("type", ERROR); } return message; }
From source file:com.changxiang.game.sdk.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {/*from www. j a v a2 s . co m*/ if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); for (RequestParameter p : parameter) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(Util.encode(p.getName())); bulider.append("="); bulider.append(Util.encode(p.getValue())); } // url += "?" + bulider.toString(); url += "?" + bulider.toString() + "&sign=" + MD5Util.MD5(bulider.toString() + "1234" + CXGameConfig.SERVERKEY); System.out.println("===" + url); } LogUtil.d("AsyncHttpGet : ", url); for (int i = 0; i < CXGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpGet(url); if (CXGameConfig.isGzip) { request.addHeader("Accept-Encoding", "gzip"); } else { request.addHeader("Accept-Encoding", "default"); } // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); // ? httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); LogUtil.d("connection url", "" + i); continue; } break; } catch (Exception e) { if (i == CXGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, CXGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!CXGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }