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:net.networksaremadeofstring.pulsant.portal.CreateTicket.java

public void onCreate(Bundle savedInstanceState) {
    API.SessionID = getIntent().getStringExtra("sessionid");
    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading...");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.createticket);

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            dialog.dismiss();/*  www . ja  va  2s  .  c o  m*/
            if (TicketID == 0) {
                Toast.makeText(CreateTicket.this,
                        "Ticket Creation Failed. Please try again later or email request@Pulsant.com",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(CreateTicket.this, "Ticket Created! Returning to main screen...",
                        Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    };

    final Thread submitTicket = new Thread() {
        public void run() {
            try {
                TicketID = API.CreateTicket(Subject, Message);
            } catch (JSONException e) {
                TicketID = 0;
            }

            handler.sendEmptyMessage(0);
        }
    };

    Button CreateTicketButton = (Button) findViewById(R.id.CreateTicketButton);
    CreateTicketButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dialog.show();
            EditText SubjectET = (EditText) findViewById(R.id.Subject);
            EditText MessageET = (EditText) findViewById(R.id.Message);
            Subject = SubjectET.getText().toString();
            Message = MessageET.getText().toString();

            submitTicket.start();
            /*EditText Subject = (EditText) findViewById(R.id.Subject);
            EditText Message = (EditText) findViewById(R.id.Message);
                    
            try 
            {
            TicketID = API.CreateTicket(Subject.getText().toString() , Message.getText().toString());
            } 
            catch (JSONException e) 
            {
               Toast.makeText(CreateTicket.this, "Ticket Creation Failed. Please try again later or email request@Pulsant.com", Toast.LENGTH_LONG).show();
            }
                    
                    
            if(TicketID == 0)
            {
               Toast.makeText(CreateTicket.this, "Ticket Creation Failed. Please try again later or email request@Pulsant.com", Toast.LENGTH_LONG).show();
            }
            else
            {
               Toast.makeText(CreateTicket.this, "Ticket Created! Returning to main screen...", Toast.LENGTH_SHORT).show();
               finish();
            }*/
        }
    });
}

From source file:com.dedipower.portal.android.AddTicketReply.java

public void onCreate(Bundle savedInstanceState) {
    API.SessionID = getIntent().getStringExtra("sessionid");
    ticketid = getIntent().getStringExtra("ticketid");
    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading...");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.addticketreply);

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            dialog.dismiss();// w  w  w .j a va 2s. c  o  m
            if (TicketID == 0) {
                Toast.makeText(AddTicketReply.this,
                        "Ticket Reply Failed. Please try again later or email request@dedipower.com",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(AddTicketReply.this, "Reply added! Returning to main screen...",
                        Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    };

    final Thread submitReply = new Thread() {
        public void run() {
            try {
                TicketID = API.ReplyToTicket(ticketid, Message);
            } catch (JSONException e) {
                TicketID = 0;
            }

            handler.sendEmptyMessage(0);
        }
    };

    Button AddReplyButton = (Button) findViewById(R.id.AddReplyButton);
    AddReplyButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dialog.show();
            EditText MessageET = (EditText) findViewById(R.id.Message);
            Message = MessageET.getText().toString();

            submitReply.start();
        }
    });
}

From source file:com.emorym.android_pusher.DeprecatedPusherSampleActivity.java

/** Called when the activity is first created. */
@Override/*from w  w  w . j  a v  a 2s  .c o m*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    setContentView(R.layout.main);

    // This Handler is going to deal with incoming messages
    mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundleData = msg.getData();
            if (bundleData.getString("type").contentEquals("pusher")) {
                try {
                    JSONObject message = new JSONObject(bundleData.getString("message"));

                    Log.d("Pusher Message", message.toString());

                    Toast.makeText(mContext, message.toString(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };

    mPusher = new Pusher(mHandler);
    mPusher.connect(PUSHER_APP_KEY);

    // Setup some toggles to subscribe/unsubscribe from our 2 test channels
    final ToggleButton test1 = (ToggleButton) findViewById(R.id.toggleButton1);
    final ToggleButton test2 = (ToggleButton) findViewById(R.id.toggleButton2);

    final Button send = (Button) findViewById(R.id.send_button);

    test1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (test1.isChecked())
                mPusher.subscribe(PUSHER_CHANNEL_1);
            else
                mPusher.unsubscribe(PUSHER_CHANNEL_1);
        }
    });

    test2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (test2.isChecked())
                mPusher.subscribe(PUSHER_CHANNEL_2);
            else
                mPusher.unsubscribe(PUSHER_CHANNEL_2);
        }
    });

    send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText channelName = (EditText) findViewById(R.id.channel_name);
            EditText eventName = (EditText) findViewById(R.id.event_name);
            EditText eventData = (EditText) findViewById(R.id.event_data);

            try {
                JSONObject data = new JSONObject();
                data.put("data", eventData.getText().toString());
                mPusher.send(eventName.getText().toString(), data, channelName.getText().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:com.fihmi.tools.minet.MiHttpResponseHandler.java

/**
 * Creates a new AsyncHttpResponseHandler
 *//*from ww w  . j  a  v a2  s . c om*/
@SuppressLint("HandlerLeak")
public MiHttpResponseHandler() {
    // 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) {
                MiHttpResponseHandler.this.handleMessage(msg);
            }
        };
    }
}

From source file:com.fly1tkg.streamfileupload.FileUploadFacade.java

public FileUploadFacade() {
    mHttpClient = new DefaultHttpClient();
    mHandler = new Handler();
}

From source file:net.networksaremadeofstring.pulsant.portal.AddTicketReply.java

public void onCreate(Bundle savedInstanceState) {
    API.SessionID = getIntent().getStringExtra("sessionid");
    ticketid = getIntent().getStringExtra("ticketid");
    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading...");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.addticketreply);

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            dialog.dismiss();/*from w  w  w  . j  a  va 2  s  .  c o  m*/
            if (TicketID == 0) {
                Toast.makeText(AddTicketReply.this,
                        "Ticket Reply Failed. Please try again later or email request@Pulsant.com",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(AddTicketReply.this, "Reply added! Returning to main screen...",
                        Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    };

    final Thread submitReply = new Thread() {
        public void run() {
            try {
                TicketID = API.ReplyToTicket(ticketid, Message);
            } catch (JSONException e) {
                TicketID = 0;
            }

            handler.sendEmptyMessage(0);
        }
    };

    Button AddReplyButton = (Button) findViewById(R.id.AddReplyButton);
    AddReplyButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dialog.show();
            EditText MessageET = (EditText) findViewById(R.id.Message);
            Message = MessageET.getText().toString();

            submitReply.start();
        }
    });
}

From source file:com.gb.cwsup.activity.CarAddActivity.java

private void initview() {
    loadingDialog = new LoadingDialog(this);
    mhandler = new Handler();
    no = (EditText) findViewById(R.id.addcar_no);
    color = (EditText) findViewById(R.id.addcar_color);
    type = (EditText) findViewById(R.id.addcar_type);
    typemobels = (EditText) findViewById(R.id.addcar_typemobels);
    isdefauls = (ToggleButton) findViewById(R.id.addcar_togglebutton);
}

From source file:com.echopf.ECHOQuery.java

/**
 * Does Find objects from the remote server.
 * @param sync : if set TRUE, then the main (UI) thread is waited for complete the finding in a background thread. 
 *              (a synchronous communication)
 * @param listKey the key associated with the object list
 * @param clazz the object class//from w w w .  j  a  v  a 2s .  co m
 * @param callback invoked after the finding is completed
 * @param instanceId the reference ID of the finding target instance
 * @param resourceType the type of this object
 * @param params to control the output objects
 * @throws ECHOException
 */
public static <T extends ECHODataObject<T>> ECHOList<T> doFind(final boolean sync, final String listKey,
        final String resourceType, final FindCallback<T> callback, final String instanceId,
        final JSONObject fParams, final ECHODataObjectFactory<T> factory) throws ECHOException {

    // Get ready a background thread
    final Handler handler = new Handler();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Callable<ECHOList<T>> communicator = new Callable<ECHOList<T>>() {
        @Override
        public ECHOList<T> call() throws ECHOException {
            ECHOException exception = null;
            ECHOList<T> objList = null;

            try {
                JSONObject response = getRequest(instanceId + "/" + resourceType, fParams);

                /* begin copying data */
                objList = new ECHOList<T>(response.optJSONObject("paginate"));

                JSONArray items = response.optJSONArray(listKey);
                if (items == null)
                    throw new ECHOException(0, "Invalid data type for response-field `" + listKey + "`.");

                for (int i = 0; i < items.length(); i++) {
                    JSONObject item = items.optJSONObject(i);
                    if (item == null)
                        throw new ECHOException(0, "Invalid data type for response-field `" + listKey + "`.");

                    String refid = item.optString("refid");
                    if (refid.isEmpty())
                        continue;

                    T obj = factory.create(instanceId, refid, item);
                    objList.add(obj);
                }
                /* end copying data */

            } catch (ECHOException e) {
                exception = e;
            } catch (Exception e) {
                exception = new ECHOException(e);
            }

            if (sync == false) {
                // Execute a callback method in the main (UI) thread.
                if (callback != null) {
                    final ECHOException fException = exception;
                    final ECHOList<T> fObjList = objList;

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.done(fObjList, fException);
                        }
                    });
                }

                return null;

            } else {

                if (exception == null)
                    return objList;
                throw exception;

            }
        }
    };

    Future<ECHOList<T>> future = executor.submit(communicator);

    if (sync) {
        try {
            return future.get();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // ignore/reset
        } catch (ExecutionException e) {
            Throwable e2 = e.getCause();

            if (e2 instanceof ECHOException) {
                throw (ECHOException) e2;
            }

            throw new RuntimeException(e2);
        }
    }

    return null;
}

From source file:com.mike.aframe.http.HttpCallBack.java

public HttpCallBack() {
    // handler???
    if (Looper.myLooper() != null) {
        handler = new Handler() {
            @Override//from  ww  w.ja v a  2s.  co m
            public void handleMessage(Message msg) {
                HttpCallBack.this.handleMessage(msg);
            }
        };
    }
}

From source file:mnc.beacon.mainservice.BeaconListForwardService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    mHandler = new Handler();
    checkEvent(true);//w  ww.  j  a  v  a  2  s.  c om
    return super.onStartCommand(intent, flags, startId);
}