List of usage examples for android.os Message obtain
public static Message obtain()
From source file:com.polyvi.xface.http.XAsyncHttpResponseHandler.java
/** * ?/* w w w.j a v a 2s. c o m*/ * * @param responseMessage * @param response * @return */ protected Message obtainMessage(int responseMessage, Object response) { Message msg = null; if (mHandler != null) { msg = this.mHandler.obtainMessage(responseMessage, response); } else { msg = Message.obtain(); msg.what = responseMessage; msg.obj = response; } return msg; }
From source file:de.qspool.clementineremote.ui.Player.java
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.disconnect: requestDisconnect();//from w w w . j ava 2 s . co m break; case R.id.shuffle: Message msg = Message.obtain(); msg.obj = new RequestControl(Request.SHUFFLE); App.mClementineConnection.mHandler.sendMessage(msg); break; case R.id.settings: Intent settingsIntent = new Intent(this, ClementineRemoteSettings.class); startActivity(settingsIntent); break; default: break; } return true; }
From source file:de.qspool.clementineremote.ui.fragments.GlobalSearchFragment.java
@SuppressLint({ "InlinedApi", "NewApi" }) @Override/*from w w w . ja va 2 s . c o m*/ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_global_search, container, false); mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.global_search_refresh_layout); mEmptyView = (SwipeRefreshLayout) view.findViewById(R.id.global_search_empty); mSwipeRefreshLayout.setColorSchemeResources(R.color.orange); mEmptyView.setColorSchemeResources(R.color.orange); mSwipeRefreshLayout.setEnabled(true); mEmptyView.setEnabled(true); mEmptyText = (TextView) view.findViewById(R.id.global_search_empty_txt); mList = (ListView) view.findViewById(R.id.global_search); mList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { SongSelectItem item = mAdapters.getLast().getItem(position); if (item.getLevel() == mMaxLevels - 1) { Message msg = Message.obtain(); LinkedList<ClementineRemoteProtocolBuffer.SongMetadata> songs = new LinkedList<>(); songs.add(GlobalSearchManager.getInstance().getRequest().getSongFromUrl(item.getUrl())); msg.obj = ClementineMessageFactory .buildInsertSongs(App.Clementine.getPlaylistManager().getActivePlaylistId(), songs); App.ClementineConnection.mHandler.sendMessage(msg); String text = getActivity().getResources().getQuantityString(R.plurals.songs_added, 1, 1); Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show(); } else { GlobalSearchQuery globalSearchQuery = new GlobalSearchQuery(getActivity(), mQueryId); globalSearchQuery.openDatabase(); globalSearchQuery.setLevel(mAdapters.size()); globalSearchQuery.setSelection(item.getSelection()); mAdapters.add(new DynamicSongQueryAdapter(getActivity(), globalSearchQuery)); showList(); } } }); mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); mList.setMultiChoiceModeListener(new MultiChoiceModeListener() { @Override public boolean onActionItemClicked(ActionMode mode, android.view.MenuItem item) { SparseBooleanArray checkedPositions = mList.getCheckedItemPositions(); final LinkedList<SongSelectItem> selectedItems = new LinkedList<>(); for (int i = 0; i < checkedPositions.size(); ++i) { int position = checkedPositions.keyAt(i); if (checkedPositions.valueAt(i)) { selectedItems.add(mAdapters.getLast().getItem(position)); } } for (SongSelectItem songItem : selectedItems) { OnSongSelectFinishedListener listener; switch (item.getItemId()) { case R.id.global_search_context_add: listener = new OnSongSelectFinishedListener() { @Override public void OnSongSelectFinished(LinkedList<SongSelectItem> l) { addSongsToPlaylist(l); } }; break; default: return false; } queryItems(songItem, listener); } mode.finish(); return true; } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.global_search_context_menu, menu); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getActivity().getWindow() .setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.grey_cab_status)); } return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public void onDestroyActionMode(ActionMode mode) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getActivity().getWindow() .setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.actionbar_dark)); } } @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { } }); GlobalSearchManager.getInstance().addOnGlobalSearchResponseListerner(this); setHasOptionsMenu(true); mActionBar.setTitle(""); mActionBar.setSubtitle("/"); return view; }
From source file:com.yangtsaosoftware.pebblemessenger.services.NotificationService.java
@Override public void onNotificationPosted(StatusBarNotification sbn) { if (sbn == null) return;/*from w w w . java 2 s .co m*/ Constants.log(LOG_TAG, "New Access Event:" + sbn.getPackageName() + " tag:" + sbn.getTag()); /*if(event.getEventType()!= AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED){ return; } */ PowerManager powMan = (PowerManager) this.getSystemService(POWER_SERVICE); if (!notifScreenOn && powMan.isScreenOn()) { Constants.log(LOG_TAG, "because screen out!"); return; } //Parcelable parcelable=event.getParcelableData(); //if(!(parcelable instanceof Notification)) return; Notification notif = sbn.getNotification(); if (sbn.isOngoing()) { Constants.log(LOG_TAG, "because is Ongoing!"); return; } String eventPackageName; if (sbn.getPackageName() != null) { eventPackageName = sbn.getPackageName(); } else { Constants.log(LOG_TAG, "Can't get event package name. Returning."); return; } boolean found = false; for (String packageName : packages) { if (packageName.equalsIgnoreCase(eventPackageName)) { found = true; break; } } if (!found) { Constants.log(LOG_TAG, eventPackageName + " was not found in the include list. Returning."); return; } String title = eventPackageName.substring(eventPackageName.lastIndexOf('.') + 1); // get the notification text Bundle notiBundle = notif.extras; StringBuilder notifySb = new StringBuilder(); CharSequence notifyChars = notiBundle.getCharSequence(Notification.EXTRA_TITLE); if (notifyChars != null) { notifySb.append(notifyChars); } else { Constants.log(LOG_TAG, "empty message title,return!"); return; } CharSequence bodyCS = notiBundle.getCharSequence(Notification.EXTRA_TEXT); if (bodyCS != null) { notifySb.append(":"); notifySb.append(bodyCS); } else { Constants.log(LOG_TAG, "empty message body,return!" + notifySb.toString()); return; } bodyCS = notiBundle.getCharSequence(Notification.EXTRA_SUB_TEXT); if (bodyCS != null) { notifySb.append(bodyCS); } Message msg = Message.obtain(); msg.what = MessageProcessingService.MSG_NEW_MESSAGE; Bundle b = new Bundle(); b.putString(MessageDbHandler.COL_MESSAGE_APP, title); b.putString(MessageDbHandler.COL_MESSAGE_CONTENT, notifySb.toString()); Constants.log(LOG_TAG, "Send new message title:" + title + " body:" + notifySb.toString()); msg.setData(b); try { rMessageProcessHandler.send(msg); } catch (RemoteException e) { e.printStackTrace(); Constants.log(LOG_TAG, "Error when sending message to MessageProcessingService."); } }
From source file:ru.kudzmi.rajitaku.radioService.RadioService.java
private void startUpdateThread() { Thread thread = new Thread() { @Override//from www . ja va2 s.c o m public void run() { while (mPlayer != null && mPlayer.isPlaying()) { mHandler.sendMessage(Message.obtain()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; thread.start(); }
From source file:com.adjust.sdk.ActivityHandler.java
@Override public void finishedTrackingActivity(JSONObject jsonResponse) { if (jsonResponse == null) { return;/*from w ww. j a v a 2 s . c o m*/ } Message message = Message.obtain(); message.arg1 = SessionHandler.FINISH_TRACKING; message.obj = jsonResponse; sessionHandler.sendMessage(message); }
From source file:cn.xiaocool.android_etong.net.constant.request.MainRequest.java
public void forgetpassword(final String phone, final String code, final String password) { new Thread() { Message msg = Message.obtain(); public void run() { String data = "&phone=" + phone + "&code=" + code + "&password=" + password; Log.e("data is ", data); String result_data = NetUtil.getResponse(WebAddress.FORGETPASSWORD, data); Log.e("successful", result_data); try { JSONObject obj = new JSONObject(result_data); msg.what = CommunalInterfaces.FORGETPASSWORD; msg.obj = obj;// w ww . j a va2 s . co m } catch (JSONException e) { e.printStackTrace(); } finally { handler.sendMessage(msg); } } }.start(); }
From source file:og.android.tether.system.WebserviceTask.java
public static boolean downloadFile(String url, String destinationDirectory, String destinationFilename) { boolean filedownloaded = true; HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(String.format(url)); Message msg = Message.obtain(); try {/* w w w . ja v a 2s . co m*/ HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); Log.d(MSG_TAG, "Request returned status " + status); if (status.getStatusCode() == 200) { HttpEntity entity = response.getEntity(); InputStream instream = entity.getContent(); int fileSize = (int) entity.getContentLength(); FileOutputStream out = new FileOutputStream( new File(destinationDirectory + "/" + destinationFilename)); byte buf[] = new byte[8192]; int len; int totalRead = 0; while ((len = instream.read(buf)) > 0) { msg = Message.obtain(); msg.what = MainActivity.MESSAGE_DOWNLOAD_PROGRESS; totalRead += len; msg.arg1 = totalRead / 1024; msg.arg2 = fileSize / 1024; MainActivity.currentInstance.viewUpdateHandler.sendMessage(msg); out.write(buf, 0, len); } out.close(); } else { throw new IOException(); } } catch (IOException e) { Log.d(MSG_TAG, "Can't download file '" + url + "' to '" + destinationDirectory + "/" + destinationFilename + "'."); filedownloaded = false; } msg = Message.obtain(); msg.what = MainActivity.MESSAGE_DOWNLOAD_COMPLETE; MainActivity.currentInstance.viewUpdateHandler.sendMessage(msg); return filedownloaded; }
From source file:com.newtifry.android.remote.BackendRequest.java
/** * Fire off this request in a thread in the background. * @param context The context for this request. * @param statusMessage If provided, show a progress dialog with this message. If NULL, no progress dialog is shown. * @param accountName The account name to perform the request under. *//*from w w w . j a v a 2 s. c o m*/ public void startInThread(final Context context, final String statusMessage, final String accountName) { // Set up the dialog. if (statusMessage != null) { this.dialog = ProgressDialog.show(context, context.getString(R.string.app_name), statusMessage, true); } // Create a new thread. Thread thread = new Thread() { public void run() { // Create a client. BackendClient client = new BackendClient(context, accountName); BackendResponse result; try { Log.i(TAG, "Beginning request..."); // dumpRequest(); result = client.request(thisRequest); // Was it successful? if (result.isError()) { Log.d(TAG, "Error: " + result.getError()); } else { Log.d(TAG, "Success! Server returned: " + result.getJSON().toString()); } // Prepare the message to send back. Message message = Message.obtain(); message.obj = result; // Post it to ourselves - it will then post it back to the real thread. handler.sendMessage(message); } catch (Exception e) { Log.e(TAG, "Generic exception: " + e.getMessage() + " of type " + e.getClass().toString()); } } }; // And start the request. thread.start(); }
From source file:com.notifry.android.remote.BackendRequest.java
/** * Fire off this request in a thread in the background. * @param context The context for this request. * @param statusMessage If provided, show a progress dialog with this message. If NULL, no progress dialog is shown. * @param accountName The account name to perform the request under. *//*w ww .j a va 2s.c o m*/ public void startInThread(final Context context, final String statusMessage, final String accountName) { // Set up the dialog. if (statusMessage != null) { this.dialog = ProgressDialog.show(context, context.getString(R.string.app_name), statusMessage, true); } // Create a new thread. Thread thread = new Thread() { public void run() { // Create a client. BackendClient client = new BackendClient(context, accountName); BackendResponse result; try { //Log.i(TAG, "Beginning request..."); result = client.request(thisRequest); // Was it successful? /*if( result.isError() ) { Log.e(TAG, "Error: " + result.getError()); } else { Log.e(TAG, "Success! Server returned: " + result.getJSON().toString()); }*/ // Prepare the message to send back. Message message = Message.obtain(); message.obj = result; // Post it to ourselves - it will then post it back to the real thread. handler.sendMessage(message); } catch (Exception e) { Log.e(TAG, "Generic exception: " + e.getMessage() + " of type " + e.getClass().toString()); } } }; // And start the request. thread.start(); }