List of usage examples for android.os Handler Handler
public Handler()
From source file:net.carlh.toast.MainActivity.java
private void startClient() { if (client != null) { return;//w w w .ja v a 2s .co 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.carapp.login.splashActivity.java
private void getClientBranch() { MultipartEntity entity = new MultipartEntity(); try {/*w ww.j av a2 s . co m*/ entity.addPart("action", new StringBody("init_config")); new AsyncWebServiceProcessingTask(context, entity, messagecheck, new AsynckCallback() { @Override public void run(String result) { if (UIUtils.checkJson(result, context)) { try { JSONObject jsonObject = new JSONObject(result); if (jsonObject.optString("satus").equals("success")) { PdfInfo.client = jsonObject.optString("client"); PdfInfo.branch = jsonObject.optString("branch"); Util.showCustomDialogWithoutButton(context, "Message", messagechecksucces, new Callback2() { @Override public void ok(final Dialog dialog) { final Handler handler = new Handler(); final Runnable runnable = new Runnable() { @Override public void run() { dialog.dismiss(); new AsyncWebServiceProcessingTask(context, null, "Please wait while checking date", new AsynckCallback() { @Override public void run(String result) { Log.e("date", "" + result); String sysdate = android.text.format.DateFormat .format("dd/MM/yyyy", new java.util.Date()) .toString(); if (result.equals(sysdate)) { Toast.makeText(context, "date match", Toast.LENGTH_SHORT).show(); new AsyncWebServiceProcessingTask( context, null, messagecheckday, new AsynckCallback() { @Override public void run( String result) { int day = Integer .parseInt( result); Calendar calendar = Calendar .getInstance(); int Today = calendar .get(Calendar.DAY_OF_WEEK); day++; if (day == Today) { Toast.makeText( context, "day is correct", Toast.LENGTH_SHORT) .show(); String ma_a = null; try { WifiManager wiman = (WifiManager) getSystemService( Context.WIFI_SERVICE); ma_a = wiman .getConnectionInfo() .getMacAddress(); Log.i(t, "" + ma_a); } catch (Exception e1) { e1.printStackTrace(); Log.i(t, " " + e1); } MultipartEntity entity = new MultipartEntity(); try { entity.addPart( "action", new StringBody( "device_authentication")); entity.addPart( "mac_address", new StringBody( ma_a)); new AsyncWebServiceProcessingTask( context, entity, "Checking License", new AsynckCallback() { @Override public void run( String result) { if (UIUtils .checkJson( result, context)) { try { JSONObject jsonObject = new JSONObject( result); if (jsonObject .optString( "satus") .equals("success")) { Toast.makeText( context, "LicenseCheck sucesses", Toast.LENGTH_SHORT) .show(); startActivity( new Intent( context, LoginActivity.class)); finish(); } else if (jsonObject .optString( "satus") .equals("error")) { Util.showCustomDialog( context, "Error", jsonObject .optString( "msg")); } } catch (Exception e) { e.printStackTrace(); } } } }).execute( PdfInfo.newjobcard); } catch (Exception e) { e.printStackTrace(); } } else { Util.showCustomDialogWithoutButton( context, "Error", messagecheckdayerror + " server day is " + day + " but your device is" + Today, new Callback2() { @Override public void ok( final Dialog dialog) { final Handler handler = new Handler(); final Runnable runnable = new Runnable() { @Override public void run() { dialog.dismiss(); finish(); } }; handler.postDelayed( runnable, 5000); } }); } } }).execute(PdfInfo.dayaddress); } else { Util.showCustomDialogWithoutButton( context, "Error", messagecheckdateerror + " server date is " + result + " but device date is " + sysdate, new Callback2() { @Override public void ok( final Dialog dialog) { new Timer().schedule( new TimerTask() { @Override public void run() { dialog.dismiss(); finish(); } }, 5000); } }); } } }).execute(PdfInfo.dateaddress); } }; handler.postDelayed(runnable, 5000); } }); } else if (jsonObject.optString("satus").equals("error")) { Util.showCustomDialogWithoutButton(context, "Message", messagechecksucces, new Callback2() { @Override public void ok(final Dialog dialog) { new Timer().schedule(new TimerTask() { @Override public void run() { dialog.dismiss(); finish(); } }, 5000); } }); } } catch (Exception e) { e.printStackTrace(); } } } }).execute(PdfInfo.newjobcard); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.uraroji.garage.android.arrraycopybench.MainActivity.java
private void startBench() { mResultTextView.setText(""); final String arrayTypeStr = mArrayTypeSpinner.getSelectedItem().toString(); final int arrayLength = Integer.parseInt(mArrayLengthSpinner.getSelectedItem().toString()); final int benchTimes = Integer.parseInt(mBenchTimesSpinner.getSelectedItem().toString()); final ProgressDialog dialog = new ProgressDialog(this); dialog.setMessage(getString(R.string.benchmarking)); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); dialog.setCancelable(false);// w ww.j a v a 2 s . c o m dialog.show(); final Handler handler = new Handler(); if (arrayTypeStr.equals("byte")) { final byte[] src = new byte[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = (byte) i; } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); benchResult = copyNative(src, benchTimes); showResult("native", benchResult, handler); dialog.dismiss(); } }).start(); } else if (arrayTypeStr.equals("short")) { final short[] src = new short[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = (short) i; } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); benchResult = copyNative(src, benchTimes); showResult("native", benchResult, handler); dialog.dismiss(); } }).start(); } else if (arrayTypeStr.equals("int")) { final int[] src = new int[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = (int) i; } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); benchResult = copyNative(src, benchTimes); showResult("native", benchResult, handler); dialog.dismiss(); } }).start(); } else if (arrayTypeStr.equals("long")) { final long[] src = new long[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = (long) i; } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); benchResult = copyNative(src, benchTimes); showResult("native", benchResult, handler); dialog.dismiss(); } }).start(); } else if (arrayTypeStr.equals("double")) { final double[] src = new double[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = (double) i; } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); benchResult = copyNative(src, benchTimes); showResult("native", benchResult, handler); dialog.dismiss(); } }).start(); } else if (arrayTypeStr.equals("Object")) { final Object[] src = new Object[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = new Object(); } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); dialog.dismiss(); } }).start(); } else if (arrayTypeStr.equals("Byte")) { final Byte[] src = new Byte[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = new Byte((byte) i); } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); dialog.dismiss(); } }).start(); } else if (arrayTypeStr.equals("Integer")) { final Integer[] src = new Integer[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = new Integer((int) i); } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); dialog.dismiss(); } }).start(); } else if (arrayTypeStr.equals("Long")) { final Long[] src = new Long[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = new Long((long) i); } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); dialog.dismiss(); } }).start(); } else if (arrayTypeStr.equals("Double")) { final Double[] src = new Double[arrayLength]; for (int i = 0; i < src.length; ++i) { src[i] = new Double((double) i); } new Thread(new Runnable() { @Override public void run() { BenchResult benchResult = null; benchResult = copyClone(src, benchTimes); showResult("clone", benchResult, handler); benchResult = copyArraycopy(src, benchTimes); showResult("System.arraycopy", benchResult, handler); benchResult = copyArraysCopyOf(src, benchTimes); showResult("Arrays.copyOf", benchResult, handler); benchResult = copyForLoop(src, benchTimes); showResult("for loop", benchResult, handler); dialog.dismiss(); } }).start(); } else { dialog.dismiss(); } }
From source file:com.binroot.fatpita.BitmapManager2.java
public void fetchBitmapOnThread(final String urlString, final ImageView imageView, final ProgressBar indeterminateProgressBar, final Activity act, final int sample) { SoftReference<Bitmap> ref = mCache.get(urlString); if (ref != null && ref.get() != null) { imageView.setImageBitmap(ref.get()); return;//from www. j av a2 s. co m } final Runnable progressBarShow = new Runnable() { public void run() { if (indeterminateProgressBar != null) { imageView.setVisibility(View.GONE); indeterminateProgressBar.setVisibility(View.VISIBLE); } } }; final Runnable progressBarHide = new Runnable() { public void run() { if (indeterminateProgressBar != null) { indeterminateProgressBar.setVisibility(View.GONE); imageView.setVisibility(View.VISIBLE); } } }; final Handler handler = new Handler() { @Override public void handleMessage(Message message) { if (indeterminateProgressBar != null && act != null) act.runOnUiThread(progressBarHide); imageView.setImageBitmap((Bitmap) message.obj); } }; Thread thread = new Thread() { @Override public void run() { if (indeterminateProgressBar != null && act != null) act.runOnUiThread(progressBarShow); Bitmap bitmap = fetchBitmap(urlString, sample); Message message = handler.obtainMessage(1, bitmap); handler.sendMessage(message); } }; thread.start(); }
From source file:com.honeycomb.cocos2dx.Soccer.java
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(com.honeycomb.cocos2dx.R.layout.main); interstitial = new InterstitialAd(this); interstitial.setAdUnitId("ca-app-pub-2442035138707094/8795197567"); activity = Soccer.this; AndroidNDKHelper.SetNDKReciever(this); Cocos2dxGLSurfaceView mGLView = (Cocos2dxGLSurfaceView) findViewById( com.honeycomb.cocos2dx.R.id.game_gl_surfaceview); mGLView.setEGLContextClientVersion(2); mGLView.setCocos2dxRenderer(new Cocos2dxRenderer()); new Handler().postDelayed(new Runnable() { @Override//from ww w . j a v a 2s .co m public void run() { Soccer.this.runOnUiThread(new Runnable() { @Override public void run() { findViewById(com.honeycomb.cocos2dx.R.id.logolayout).setVisibility(View.GONE); } }); } }, 3000); iapSetup = new IAPSetup(); mIABHelper = iapSetup.mIABHelper; if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) { isLargeDevice = true; } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) { isLargeDevice = true; } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) { isLargeDevice = false; } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) { isLargeDevice = false; } gamePrefrence = getSharedPreferences("null", MODE_PRIVATE); }
From source file:jp.mau.twappremover.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); _apps = new ArrayList<LinkedApp>(); _handler = new Handler(); _imageLoader = new ImageLoader(Volley.newRequestQueue(this), new BitmapCache()); _id = (EditText) findViewById(R.id.activity_main_form_id); _pass = (EditText) findViewById(R.id.activity_main_form_pass); ListView list = (ListView) findViewById(R.id.activity_main_list); list.setOnItemClickListener(new OnItemClickListener() { @Override// www . ja va 2 s. co m public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { final PopupView dialog = new PopupView(MainActivity.this); dialog.setDialog() .setLabels(getString(R.string.activity_main_dlgtitle_revoke), getString(R.string.activity_main_dlgmsg_revoke, _apps.get(position).name)) .setCancelable(true).setNegativeBtn(getString(R.string.activity_main_dlgbtn_revoke_cancel), new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }) .setPositiveBtn(getString(R.string.activity_main_dlgbtn_revoke_conf), new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); new Thread(new Runnable() { @Override public void run() { revokeApp(_apps.get(position)); } }).start(); } }) .show(); } }); setButton(); _appadapter = new AppAdapter(this); list.setAdapter(_appadapter); _client = new DefaultHttpClient(); HttpParams params = _client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 60 * 1000); HttpConnectionParams.setSoTimeout(params, 60 * 1000); _client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH); }
From source file:com.tcs.geofenceplugin.GeofenceTransitionsIntentService.java
private void getGeofenceTransitionDetails(Context context, int geofenceTransition, List<Geofence> triggeringGeofences) { ArrayList triggeringGeofencesIdsList = new ArrayList(); Handler mHandler = new Handler(); for (Geofence geofence : triggeringGeofences) { String geofenceTransitionString = getTransitionString(geofenceTransition); geofenceTransitionString += geofence.getRequestId(); URL url;/* ww w . j a va2s . c om*/ HttpURLConnection urlConnection = null; JSONArray response = new JSONArray(); String notificationtext = ""; String place = ""; String startdate = ""; String enddate = ""; JSONObject obj = new JSONObject(); try { int restid = Integer.parseInt(geofence.getRequestId()); url = new URL("https://apphonics.tcs.com/geofence/SelectTrigger?triggerid=" + restid); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); int responseCode = urlConnection.getResponseCode(); Log.d("Komal3", responseCode + ""); if (responseCode == 200) { BufferedReader reader = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), "utf-8")); StringBuilder sb = new StringBuilder(); try { String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } obj = new JSONObject(sb.toString()); JSONArray arr = obj.getJSONArray("notificationdata"); for (int i = 0; i < arr.length(); i++) { JSONObject O = arr.getJSONObject(i); notificationtext = O.getString("notification_text"); place = O.getString("place"); startdate = O.getString("startdate"); enddate = O.getString("enddate"); } //response = new JSONArray(responseString); } else { Log.d(TAG, "Response code:" + responseCode); } } catch (Exception e) { e.printStackTrace(); if (urlConnection != null) urlConnection.disconnect(); } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date datestart, dateend; try { datestart = format.parse(startdate); dateend = format2.parse(enddate); if (datestart.compareTo(new Date()) <= 0 && dateend.compareTo(new Date()) >= 0) { sendNotification(geofenceTransitionString, notificationtext, place); } mHandler.postDelayed(new Runnable() { public void run() { } }, 5000); } catch (ParseException e) { e.printStackTrace(); } } }
From source file:com.mobicage.rogerthat.registration.AbstractRegistrationActivity.java
public void init(Activity activity) { T.UI();/* w w w. ja va 2 s. c o m*/ mActivity = activity; mUIHandler = new Handler(); T.setUIThread("RegistrationProcedureActivity.UI"); mWorkerThread = new HandlerThread("rogerthat_registration_worker"); mWorkerThread.start(); mWorkerLooper = mWorkerThread.getLooper(); mWorkerHandler = new Handler(mWorkerLooper); mWorkerHandler.post(new SafeRunnable() { @Override public void safeRun() { T.setRegistrationThread("RegistrationProcedureActivity.WORKER"); } }); startRegistrationService(); }
From source file:org.liberty.android.fantastischmemopro.downloader.DownloaderSS.java
@Override protected void initialRetrieve() { dlAdapter = new DownloadListAdapter(this, R.layout.filebrowser_item); dlStack = new Stack<List<DownloadItem>>(); categoryIdStack = new Stack<String>(); mHandler = new Handler(); listView = (ListView) findViewById(R.id.file_list); listView.setOnScrollListener(this); listView.setAdapter(dlAdapter);// w w w. j ava2 s . c o m mProgressDialog = ProgressDialog.show(DownloaderSS.this, getString(R.string.loading_please_wait), getString(R.string.loading_connect_net), true, true, new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { finish(); } }); new Thread() { public void run() { try { categoryList = retrieveCategories(); mHandler.post(new Runnable() { public void run() { showRootCategories(); mProgressDialog.dismiss(); } }); } catch (final Exception e) { mHandler.post(new Runnable() { public void run() { mProgressDialog.dismiss(); new AlertDialog.Builder(DownloaderSS.this) .setTitle(R.string.downloader_connection_error) .setMessage( getString(R.string.downloader_connection_error_message) + e.toString()) .setPositiveButton(getString(R.string.ok_text), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .create().show(); } }); } } }.start(); }
From source file:com.support.android.designlibdemo.activities.CampaignDetailActivity.java
@Override protected void onStart() { super.onStart(); new Handler().postDelayed(new Runnable() { @Override//from w w w . j av a 2 s . co m public void run() { ivCampaignImage.requestLayout(); } }, 600); }