Example usage for android.os Handler Handler

List of usage examples for android.os Handler Handler

Introduction

In this page you can find the example usage for android.os Handler Handler.

Prototype

public Handler() 

Source Link

Document

Default constructor associates this handler with the Looper for the current thread.

Usage

From source file:cn.caimatou.canting.utils.http.asynchttp.AsyncHttpResponseHandler.java

/**
 * Creates a new AsyncHttpResponseHandler
 *//*from   w w  w .j  ava  2  s  . c  o m*/
public AsyncHttpResponseHandler() {
    // Set up a handler to post events back to the correct thread if possible
    if (Looper.myLooper() != null) {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                AsyncHttpResponseHandler.this.handleMessage(msg);
            }
        };
    }
}

From source file:com.elephant.http.AsyncHttpResponseHandler.java

/**
 * Creates a new AsyncHttpResponseHandler
 *///from  w  ww.j  av a 2 s . c  o  m
public AsyncHttpResponseHandler() {
    // Set up a handler to post events back to the correct thread if
    // possible
    if (Looper.myLooper() != null) {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                AsyncHttpResponseHandler.this.handleMessage(msg);
            }
        };
    }
}

From source file:com.damytech.HttpConn.AsyncHttpResponseHandler.java

/**
 * Creates a new AsyncHttpResponseHandler
 *//*from ww w  . j a va 2s. c om*/
@SuppressLint("HandlerLeak")
public AsyncHttpResponseHandler() {
    // Set up a handler to post events back to the correct thread if possible
    if (Looper.myLooper() != null) {
        handler = new Handler() {
            public void handleMessage(Message msg) {
                AsyncHttpResponseHandler.this.handleMessage(msg);
            }
        };
    }
}

From source file:org.sigimera.app.android.GCMIntentService.java

public GCMIntentService() {
    super(GCMIntentService.class.getName());
    this.mainThreadHandler = new Handler();
}

From source file:nl.meta.mobile.chat.MobileChatActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mobile_chat);

    // Declares/*from  w  w w  .j a v a2s.c  o m*/
    tvConnectStatus = (TextView) findViewById(R.id.tvConnected);
    etName = (EditText) findViewById(R.id.etName);
    llMessages = (LinearLayout) findViewById(R.id.llMessages);
    svMessages = (ScrollView) findViewById(R.id.svMessages);
    etMessage = (EditText) findViewById(R.id.etMessage);
    btnSend = (Button) findViewById(R.id.btnSend);

    mHandler = new Handler();

    // Update connection label
    setConnectionLabel();

    // Send message button handler
    btnSend.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // check connection
            if (connected) {
                // check name not null
                if (!etName.getText().toString().equals("")) {
                    if (!etMessage.getText().toString().equals("")) {
                        // Send message
                        mClient.send(etName.getText().toString() + ": " + etMessage.getText().toString());
                        etMessage.setText("");
                    }
                } else {
                    // Name is empty
                    addMessageToChat(CONSTANT_NO_NAME_ERROR_MSG);
                }
            }
        }
    });

    // Websockets setup
    List<BasicNameValuePair> extraHeaders = Arrays.asList(new BasicNameValuePair("Cookie", "session=abcd"));
    mClient = new WebSocketClient(URI.create(CONSTANT_WEBSOCKETS_URL), new WebSocketClient.Listener() {

        @Override
        public void onMessage(byte[] data) {
            // Byte array not used
            System.out.println(data);
        }

        @Override
        public void onMessage(String message) {
            // Process message - Handler required because it's on the UI
            // thread.
            final String messageForHandler = message;
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    addMessageToChat(messageForHandler);
                }
            });
        }

        @Override
        public void onError(Exception error) {
            // Should handle these errors...
            error.getStackTrace();
            Thread.currentThread().getStackTrace();
        }

        @Override
        public void onDisconnect(int code, String reason) {
            connected = false;
            // This is already on a handler
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    setConnectionLabel();
                }
            });

        }

        @Override
        public void onConnect() {
            connected = true;
            // This is already on a handler
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    setConnectionLabel();
                }
            });
        }
    }, extraHeaders);

    // Tell the client to connect
    mClient.connect();
}

From source file:com.cnblogs.app.http.async.AsyncHttpResponseHandler.java

/**
 * Creates a new AsyncHttpResponseHandler
 *//*from   www. j a  v a 2  s.  c om*/
@SuppressLint("HandlerLeak")
public AsyncHttpResponseHandler() {
    // Set up a handler to post events back to the correct thread if possible
    if (Looper.myLooper() != null) {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                AsyncHttpResponseHandler.this.handleMessage(msg);
            }
        };
    }
}

From source file:com.arquitetaweb.restaurantes.fragment.CardapioFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    contexto = this.getActivity();
    final View view = inflater.inflate(R.layout.list_itens, container, false);

    handler = new Handler();
    carregarDadosJson(view);//from   w  ww.  ja va  2s  . c  o  m

    return view;
}

From source file:com.app.jdy.ui.MyOrderActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_order);

    initView();/*ww  w  .j  av a 2  s  .  c om*/

    handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:
                Toast.makeText(MyOrderActivity.this, Constants.NO_INTENT_TIPS, Toast.LENGTH_SHORT).show();
                break;
            case 1:
                list = new ArrayList<MyOrder>();
                try {
                    JSONObject jsonObject = new JSONObject(dataJson);
                    if (jsonObject.getString("sum").equals("null")) {
                        my_order_sum.setText("0");
                        my_order_sum_yuan.setText("");
                    } else {
                        my_order_sum
                                .setText(ChineseMoneyUtils.numWithDigitArray(jsonObject.getDouble("sum"))[0]);
                        my_order_sum_yuan
                                .setText(ChineseMoneyUtils.numWithDigitArray(jsonObject.getDouble("sum"))[1]);
                    }
                    JSONArray jsonArray = jsonObject.getJSONArray("data");

                    for (int i = 0; i < jsonArray.length(); i++) {

                        MyOrder myOrder = new MyOrder();
                        myOrder.setTime(jsonArray.getJSONObject(i).getString("create_time"));
                        myOrder.setName(jsonArray.getJSONObject(i).getString("name"));
                        myOrder.setMoney(jsonArray.getJSONObject(i).getString("investTotal"));
                        list.add(myOrder);
                    }
                    myOrderAdapter = new MyOrderAdapter(MyOrderActivity.this, R.layout.my_order_item, list);
                    my_order_list.setAdapter(myOrderAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
            }
        }
    };

    getData();

}

From source file:com.novel.lightnovel.Utils.DrawableManager.java

public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
    if (drawableMap.containsKey(urlString)) {
        imageView.setImageDrawable(drawableMap.get(urlString));
    }//from w  w  w.  j  a v a 2  s .co  m
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageView.setImageDrawable((Drawable) message.obj);
        }
    };
    Thread thread = new Thread() {
        @Override
        public void run() {
            Drawable drawable = fetchDrawable(urlString);
            Message message = handler.obtainMessage(1, drawable);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:com.ytrain.mutrain.utils.asynchttp.AsyncHttpResponseHandler.java

/**
 * Creates a new AsyncHttpResponseHandler
 *//*from   ww w .  j a v a2s .co  m*/
public AsyncHttpResponseHandler() {
    // Set up a handler to post events back to the correct thread if
    // possible?handle??
    if (Looper.myLooper() != null) {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                AsyncHttpResponseHandler.this.handleMessage(msg);
            }
        };
    }
}