List of usage examples for android.os Handler Handler
public Handler()
From source file:com.toppatch.mv.ui.activities.MainActivity.java
private void checkConditions() { //sendToWelcomePage(); if (isConnectingToInternet()) { if (AdminHelper.isUserLoggedIn(this)) { if (AdminHelper.isAdminActive(this)) { if (AdminHelper.isSamsungESDKEnabled(this)) if (AdminHelper.isGCMRegistered(this)) { Log.i("see", "gcmif"); sendToWelcomePage(); } else { Log.i("see", "gcmelse"); sendToGCMPage(); }//www .j ava 2 s . c om else { Log.i("see", "register"); Boolean canRegiser = Registration.register(this); if (!canRegiser) sendToLoginPage(); } } else { sendToLoginPage(); } } else { sendToLoginPage(); } } else { AlertDialog alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle("Ohh No!!"); alertDialog.setMessage("No Internet Connection...Exiting.."); alertDialog.show(); new Handler().postDelayed(new Runnable() { @Override public void run() { finish(); } }, 2000); } }
From source file:com.mytutor.search.ImageLoader.java
public void fetchDrawableOnThread(final String urlString, Drawable drawable) { if (drawableMap.containsKey(urlString)) { drawable = drawableMap.get(urlString); }/*from w w w . j a v a 2 s . c o m*/ final Handler handler = new Handler() { @Override public void handleMessage(Message message) { final Drawable drawable = (Drawable) message.obj; } }; Thread thread = new Thread() { @Override public void run() { //TODO : set imageView to a "pending" image Drawable draw = fetchDrawable(urlString); Message message = handler.obtainMessage(1, draw); handler.sendMessage(message); } }; thread.start(); }
From source file:net.networksaremadeofstring.rhybudd.ViewZenossDevice.java
/** Called when the activity is first created. */ @Override/* ww w.j a v a 2 s . c o m*/ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); settings = PreferenceManager.getDefaultSharedPreferences(this); setContentView(R.layout.view_zenoss_device3); actionbar = getActionBar(); actionbar.setDisplayHomeAsUpEnabled(true); actionbar.setHomeButtonEnabled(true); list = (ListView) findViewById(R.id.ZenossEventsList); errorHandler = new Handler() { public void handleMessage(Message msg) { try { Toast.makeText(ViewZenossDevice.this, msg.getData().getString("exception"), Toast.LENGTH_LONG) .show(); } catch (Exception e) { ////BugSenseHandler.sendException("ViewZenossDevice-ErrorHandler", e); } } }; loadAverageHandler = new Handler() { public void handleMessage(Message msg) { try { ((ImageView) findViewById(R.id.loadAverageGraph)).setImageDrawable(loadAverageGraph); } catch (Exception e) { e.printStackTrace(); } } }; CPUGraphHandler = new Handler() { public void handleMessage(Message msg) { try { ((ImageView) findViewById(R.id.CPUGraph)).setImageDrawable(CPUGraph); } catch (Exception e) { e.printStackTrace(); } } }; MemoryGraphHandler = new Handler() { public void handleMessage(Message msg) { try { ((ImageView) findViewById(R.id.MemoryGraph)).setImageDrawable(MemoryGraph); } catch (Exception e) { e.printStackTrace(); } } }; eventsHandler = new Handler() { public void handleMessage(Message msg) { try { //((ProgressBar) findViewById(R.id.eventsProgressBar)).setVisibility(4); } catch (Exception e) { ////BugSenseHandler.sendException("ViewZenossDevice", e); } if (EventCount > 0 && msg.what == 1) { try { adapter = new ZenossEventsAdaptor(ViewZenossDevice.this, listOfZenossEvents, false); list.setAdapter(adapter); } catch (Exception e) { ////BugSenseHandler.sendException("ViewZenossDevice", e); } } else { list = null; } } }; firstLoadHandler = new Handler() { public void handleMessage(Message msg) { if (dialog != null) dialog.dismiss(); try { if (DeviceObject != null && msg.what == 1 && DeviceObject.getJSONObject("result").getBoolean("success") == true) { //Log.i("DeviceDetails",DeviceObject.toString()); DeviceDetails = DeviceObject.getJSONObject("result").getJSONObject("data"); try { String Name = DeviceDetails.getString("snmpSysName").toUpperCase(); if (Name.equals("")) { Name = DeviceDetails.getString("name").toUpperCase(); } ((TextView) findViewById(R.id.deviceID)).setText(Name); //actionbar.setTitle(DeviceDetails.getString("snmpSysName")); actionbar.setSubtitle(Name); } catch (Exception e) { ((TextView) findViewById(R.id.deviceID)).setText("--"); } try { ((TextView) findViewById(R.id.modelTime)) .setText(DeviceDetails.getString("lastCollected")); } catch (Exception e) { ((TextView) findViewById(R.id.modelTime)).setText("Unknown"); } try { ((TextView) findViewById(R.id.firstSeen)).setText(DeviceDetails.getString("firstSeen")); } catch (Exception e) { ((TextView) findViewById(R.id.firstSeen)).setText(""); } try { ((TextView) findViewById(R.id.location)) .setText(DeviceDetails.getString("snmpLocation")); } catch (Exception e) { ((TextView) findViewById(R.id.location)).setText("Unknown Location"); } try { ((TextView) findViewById(R.id.uptime)).setText(DeviceDetails.getString("uptime")); } catch (Exception e) { //Already got a placeholder } try { ((TextView) findViewById(R.id.productionState)) .setText(DeviceDetails.getString("productionState")); } catch (Exception e) { //Already got a placeholder } try { ((TextView) findViewById(R.id.memorySwap)) .setText(DeviceDetails.getJSONObject("memory").getString("ram") + " / " + DeviceDetails.getJSONObject("memory").getString("swap")); } catch (Exception e) { //Already got a placeholder } String Groups = ""; try { for (int i = 0; i < DeviceDetails.getJSONArray("groups").length(); i++) { if (i > 0) Groups += ", "; try { Groups += DeviceDetails.getJSONArray("groups").getJSONObject(i) .getString("name"); } catch (Exception e) { ////BugSenseHandler.sendException("ViewZenossDevice", e); } } ((TextView) findViewById(R.id.groups)).setText(Groups); } catch (Exception e) { ////BugSenseHandler.sendException("ViewZenossDevice", e); } String Systems = ""; try { for (int i = 0; i < DeviceDetails.getJSONArray("systems").length(); i++) { if (i > 0) Systems += ", "; Systems += DeviceDetails.getJSONArray("systems").getJSONObject(i).getString("name"); } ((TextView) findViewById(R.id.systems)).setText(Systems); } catch (Exception e) { //Already got a placeholder ////BugSenseHandler.sendException("ViewZenossDevice", e); } //etc } else { Toast.makeText(ViewZenossDevice.this, "There was an error loading the Device details", Toast.LENGTH_LONG).show(); //finish(); } } catch (Exception e) { //e.printStackTrace(); Toast.makeText(ViewZenossDevice.this, "An error was encountered parsing the JSON.", Toast.LENGTH_LONG).show(); ////BugSenseHandler.sendException("ViewZenossDevice", e); } } }; dialog = new ProgressDialog(this); dialog.setTitle("Contacting Zenoss"); dialog.setMessage("Please wait:\nLoading Device details...."); dialog.show(); dataPreload = new Thread() { public void run() { try { Message msg = new Message(); Bundle bundle = new Bundle(); if (API == null) { try { /*if(settings.getBoolean("httpBasicAuth", false)) { API = new ZenossAPIv2(settings.getString("userName", ""), settings.getString("passWord", ""), settings.getString("URL", ""),settings.getString("BAUser", ""), settings.getString("BAPassword", "")); } else { API = new ZenossAPIv2(settings.getString("userName", ""), settings.getString("passWord", ""), settings.getString("URL", "")); }*/ } /*catch(ConnectTimeoutException cte) { if(cte.getMessage() != null) { //Toast.makeText(ViewZenossDevice.this, "An error was encountered;\r\n" + cte.getMessage().toString(), Toast.LENGTH_LONG).show(); bundle.putString("exception","The connection timed out;\r\n" + cte.getMessage().toString()); msg.setData(bundle); msg.what = 0; errorHandler.sendMessage(msg); } else { bundle.putString("exception","A time out error was encountered but the exception thrown contains no further information."); msg.setData(bundle); msg.what = 0; errorHandler.sendMessage(msg); //Toast.makeText(ViewZenossDevice.this, "An error was encountered but the exception thrown contains no further information.", Toast.LENGTH_LONG).show(); } }*/ catch (Exception e) { if (e.getMessage() != null) { bundle.putString("exception", "An error was encountered;\r\n" + e.getMessage().toString()); msg.setData(bundle); msg.what = 0; errorHandler.sendMessage(msg); //Toast.makeText(ViewZenossDevice.this, "An error was encountered;\r\n" + e.getMessage().toString(), Toast.LENGTH_LONG).show(); } else { bundle.putString("exception", "An error was encountered but the exception thrown contains no further information."); msg.setData(bundle); msg.what = 0; errorHandler.sendMessage(msg); //Toast.makeText(ViewZenossDevice.this, "An error was encountered but the exception thrown contains no further information.", Toast.LENGTH_LONG).show(); } } DeviceObject = API.GetDevice(getIntent().getStringExtra("UID")); } } catch (Exception e) { //e.printStackTrace(); //BugSenseHandler.sendException("updateDevices-dataPreload",e); firstLoadHandler.sendEmptyMessage(0); } firstLoadHandler.sendEmptyMessage(1); } }; dataPreload.start(); loadAvgGraphLoad = new Thread() { public void run() { try { if (API == null) { Message msg = new Message(); Bundle bundle = new Bundle(); try { /*if(settings.getBoolean("httpBasicAuth", false)) { API = new ZenossAPIv2(settings.getString("userName", ""), settings.getString("passWord", ""), settings.getString("URL", ""),settings.getString("BAUser", ""), settings.getString("BAPassword", "")); } else { API = new ZenossAPIv2(settings.getString("userName", ""), settings.getString("passWord", ""), settings.getString("URL", "")); }*/ if (PreferenceManager.getDefaultSharedPreferences(ViewZenossDevice.this) .getBoolean(ZenossAPI.PREFERENCE_IS_ZAAS, false)) { API = new ZenossAPIZaas(); } else { API = new ZenossAPICore(); } ZenossCredentials credentials = new ZenossCredentials(ViewZenossDevice.this); API.Login(credentials); } catch (Exception e) { e.printStackTrace(); } } JSONObject graphURLs = API.GetDeviceGraphs(getIntent().getStringExtra("UID")); //Log.e("graphURLs",graphURLs.toString(3)); int urlCount = graphURLs.getJSONObject("result").getJSONArray("data").length(); for (int i = 0; i < urlCount; i++) { JSONObject currentGraph = null; try { currentGraph = graphURLs.getJSONObject("result").getJSONArray("data").getJSONObject(i); if (currentGraph.getString("title").equals("Load Average")) { loadAverageGraph = API.GetGraph(currentGraph.getString("url")); loadAverageHandler.sendEmptyMessage(1); } else if (currentGraph.getString("title").equals("CPU Utilization")) { CPUGraph = API.GetGraph(currentGraph.getString("url")); CPUGraphHandler.sendEmptyMessage(1); } else if (currentGraph.getString("title").equals("Memory Utilization")) { MemoryGraph = API.GetGraph(currentGraph.getString("url")); MemoryGraphHandler.sendEmptyMessage(1); } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } }; loadAvgGraphLoad.start(); eventsLoad = new Thread() { public void run() { try { if (API == null) { Message msg = new Message(); Bundle bundle = new Bundle(); try { /*if(settings.getBoolean("httpBasicAuth", false)) { API = new ZenossAPIv2(settings.getString("userName", ""), settings.getString("passWord", ""), settings.getString("URL", ""),settings.getString("BAUser", ""), settings.getString("BAPassword", "")); } else { API = new ZenossAPIv2(settings.getString("userName", ""), settings.getString("passWord", ""), settings.getString("URL", "")); }*/ if (PreferenceManager.getDefaultSharedPreferences(ViewZenossDevice.this) .getBoolean(ZenossAPI.PREFERENCE_IS_ZAAS, false)) { API = new ZenossAPIZaas(); } else { API = new ZenossAPICore(); } ZenossCredentials credentials = new ZenossCredentials(ViewZenossDevice.this); API.Login(credentials); } catch (ConnectTimeoutException cte) { if (cte.getMessage() != null) { //Toast.makeText(ViewZenossDevice.this, , Toast.LENGTH_LONG).show(); bundle.putString("exception", "An error was encountered;\r\n" + cte.getMessage().toString()); } else { //Toast.makeText(ViewZenossDevice.this, "An error was encountered but the exception thrown contains no further information.", Toast.LENGTH_LONG).show(); bundle.putString("exception", "An error was encountered but the exception thrown contains no further information."); } msg.setData(bundle); msg.what = 0; errorHandler.sendMessage(msg); } catch (Exception e) { if (e.getMessage() != null) { //Toast.makeText(ViewZenossDevice.this, "An error was encountered;\r\n" + e.getMessage().toString(), Toast.LENGTH_LONG).show(); bundle.putString("exception", "An error was encountered;\r\n" + e.getMessage().toString()); } else { //Toast.makeText(ViewZenossDevice.this, "An error was encountered but the exception thrown contains no further information.", Toast.LENGTH_LONG).show(); bundle.putString("exception", "An error was encountered but the exception thrown contains no further information."); } msg.setData(bundle); msg.what = 0; errorHandler.sendMessage(msg); } } /*try { EventsObject = API.GetDeviceEvents(getIntent().getStringExtra("UID"),false); } catch(Exception e) { e.printStackTrace(); }*/ /*try { if((EventsObject.has("type") && EventsObject.get("type").equals("exception")) || !EventsObject.getJSONObject("result").has("totalCount") ) { EventsObject = API.GetDeviceEvents(getIntent().getStringExtra("UID"),true); } } catch(Exception e) { e.printStackTrace(); }*/ if (null != EventsObject && EventsObject.has("result") && EventsObject.getJSONObject("result").getInt("totalCount") > 0) { Events = EventsObject.getJSONObject("result").getJSONArray("events"); try { if (EventsObject != null) { EventCount = EventsObject.getJSONObject("result").getInt("totalCount"); for (int i = 0; i < EventCount; i++) { //JSONObject CurrentEvent = null; try { //CurrentEvent = Events.getJSONObject(i); listOfZenossEvents.add(new ZenossEvent(Events.getJSONObject(i))); //Log.i("ForLoop",CurrentEvent.getString("summary")); } catch (JSONException e) { //Log.e("API - Stage 2 - Inner", e.getMessage()); } catch (Exception e) { //BugSenseHandler.sendException("ViewZenossDevice-EventsLoop", e); } } eventsHandler.sendEmptyMessage(1); } else { //Log.i("eventsLoad","Had a problem; EventsObject was null"); //eventsHandler.sendEmptyMessage(0); } } catch (JSONException e) { e.printStackTrace(); //BugSenseHandler.sendException("ViewZenossDevice-Events", e); eventsHandler.sendEmptyMessage(0); } } else { eventsHandler.sendEmptyMessage(0); } } catch (Exception e) { //Log.e("API - Stage 1", e.getMessage()); //BugSenseHandler.sendException("ViewZenossDevice-Events", e); eventsHandler.sendEmptyMessage(0); } } }; eventsLoad.start(); }
From source file:com.gtosoft.libvoyager.net.GTONet.java
/** * Creates a new "worker" thread, to which we can post new work to be performed asynchronously from the main (UI) thread. *//*from www. jav a2s . c om*/ public void startWorkerThread() { if (mWorkerThread != null) return; // create a new thread. mWorkerThread = new Thread() { public void run() { //mWorkerHandler.getLooper(); Looper.prepare(); // set the worker to a new handler owned by this thead. mWorkerHandler = new Handler(); // main loop. just loop, sleeping, waiting for work. while (1 == 1) { Looper.loop(); try { Thread.sleep(1000); } catch (InterruptedException e) { break; // break out of the while loop, kills the thread. } } // end while loop. Looper.myLooper().quit(); }// end of thread run() }; mWorkerThread.start(); // sleep for a second while thread starts... Prevents calling tasks from hitting it before it has a chance to start! try { Thread.sleep(1000); } catch (InterruptedException e) { return; } }
From source file:com.rightscale.app.dashboard.ShowServerMonitoring.java
public void showGraph(String template, String size, String period) { URI uri = URI.create(template); Map<String, String> query = parseQueryParams(uri); query.put("size", size); query.put("period", period); StringBuffer qstr = new StringBuffer(); for (String key : query.keySet()) { if (qstr.length() > 0) qstr.append('&'); qstr.append(key);//from ww w. j av a 2 s. com qstr.append('='); qstr.append(query.get(key)); } int qmark = template.indexOf('?'); if (qmark < 0) { throw new IllegalArgumentException("URI does not contain query-string marker: " + template); } String prefix = template.substring(0, qmark); //TODO thread safety _selectedGraphUri = URI.create(prefix + '?' + qstr); ContentTransfer.loadImage(this, this, new Handler()); }
From source file:ua.at.tsvetkov.data_processor.ProcessingCentre.java
/** * @param request/*ww w. j a va 2 s. co m*/ * @param clazz * @param callback */ public ProcessingCentre(Request request, Class<T> clazz, Callback<T> callback) { if (request == null || clazz == null) { throw new InvalidParameterException(INVALID_PARAMETER); } this.request = request; this.clazz = clazz; this.callback = callback; if (Looper.myLooper() != null) handler = new Handler(); else handler = null; thread = Thread.currentThread(); }
From source file:it.uniroma2.foundme.studente.ShowCourseActivity.java
public static void populateView(String[] result) { courses = new String[result.length]; System.arraycopy(result, 0, courses, 0, result.length); List<Map<String, String>> data = new ArrayList<Map<String, String>>(); for (int i = 0; i < result.length; i++) { Map<String, String> datum = new HashMap<String, String>(2); if (i == 0 && result[0].equalsIgnoreCase(Variables_it.NO_COURSE)) { datum.put(Variables_it.COURSE, result[0]); datum.put(Variables_it.PROF, ""); data.add(datum);//from www . j a v a 2 s . c o m break; } else { String[] items = result[i].split(","); datum.put(Variables_it.COURSE, items[0]); datum.put(Variables_it.PROF, items[1]); data.add(datum); } } //creo l'adapter SimpleAdapter adapter = new SimpleAdapter(context, data, android.R.layout.simple_list_item_2, new String[] { Variables_it.COURSE, Variables_it.PROF }, new int[] { android.R.id.text1, android.R.id.text2 }); lvCorsiSeguiti.setAdapter(adapter); swipeShow.setColorSchemeColors(0xff429874, 0xffffffff, 0xff429874, 0xffffffff); swipeShow.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { swipeShow.setRefreshing(true); (new Handler()).postDelayed(new Runnable() { @Override public void run() { swipeShow.setRefreshing(false); try { getCourse(false); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }, 3000); } }); lvCorsiSeguiti.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (!courses[position].equalsIgnoreCase(Variables_it.NO_COURSE)) { Intent i = new Intent(context, CourseActivity.class); i.putExtra(Variables_it.COURSE, courses[position]); ((Activity) context).startActivity(i); } } }); lvCorsiSeguiti.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) { } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem == 0) swipeShow.setEnabled(true); else swipeShow.setEnabled(false); } }); }
From source file:com.safecell.AccountFormActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setWindowAnimations(R.anim.null_animation); context = AccountFormActivity.this; this.initUI(); profilesRepository = new ProfilesRepository(context); createAccountButton.setOnClickListener(createAccountButtonOnclickListenr); progressDialog = new ProgressDialog(context); licenseProgressDialog = new ProgressDialog(context); licenseThread = new LicenseThread(); Intent intent = getIntent();/*from w w w . java 2 s .co m*/ Bundle bundle = intent.getExtras(); callingActivity = bundle.getString("from"); handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); licenseProgressDialog.dismiss(); if (licenseThread.isAlive()) { licenseThread.interrupt(); licenseThread = new LicenseThread(); } if (licensekeyString == null) { message = getLicenseKey.getFailureMessage(); UIUtils.OkDialog(context, message); return; } selectLicenseFromDialog(); } }; PackageManager pm = getPackageManager(); try { //---get the package info--- PackageInfo pi = pm.getPackageInfo("com.safecell", 0); //Log.v("Version Code", "Code = "+pi.versionCode); //Log.v("Version Name", "Name = "+pi.versionName); versionName = pi.versionName; } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } preferences = getSharedPreferences("AccountUniqueID", MODE_WORLD_WRITEABLE); preferences.getString("AccountUID", ""); createAccountUniqueID(SCProfile.newUniqueDeviceKey()); }
From source file:com.example.propertylist.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_project_sales_list); ImageView mImageView = new ImageView(this); DisplayMetrics metrics;/*from ww w .j av a 2s .co m*/ int mScreenWidth; int mScreenHeight; api_key = getResources().getString(R.string.key); metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); mScreenWidth = metrics.widthPixels; mScreenHeight = metrics.heightPixels; if (mScreenWidth >= minScreenwidth) { if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { if (mScreenWidth >= minLandScreenwidth) { largeScreen = true; } } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { if (mScreenHeight > minLandScreenwidth) { largeScreen = true; } } } else { largeScreen = false; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.id.daft_logo_image, options); mImageView.setImageBitmap( // ensure image size is 100 x 100. decodeSampledBitmapFromResource(getResources(), R.id.daft_logo_image, 100, 100)); handler = new Handler() { @Override public void handleMessage(Message msg) { hideProgressDialog(); startListActivity(); } }; }
From source file:com.lovejoy777sarootool.rootool.preview.IconPreview.java
private static void queueJob(final File uri, final ImageView imageView) { /* Create handler in UI thread. */ final Handler handler = new Handler() { @Override//from ww w . j a va2 s . c om public void handleMessage(Message msg) { String tag = imageViews.get(imageView); if (tag != null && tag.equals(uri.getAbsolutePath())) { if (msg.obj != null) { imageView.setImageBitmap((Bitmap) msg.obj); } else { imageView.setImageBitmap(null); } } } }; pool.submit(new Runnable() { public void run() { final Bitmap bmp = getPreview(uri); Message message = Message.obtain(); message.obj = bmp; handler.sendMessage(message); } }); }