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.grappendorf.doitlater.TaskEditorActivity.java

private void createTask() {
    final int insertAt = insertPosition.getSelectedItemPosition();
    String previousTaskId = insertAt == INSERT_TOP ? null : getIntent().getStringExtra("lastTaskId");
    ((DoItLaterApplication) getApplication()).getTaskManager().createTask(taskListId, task, previousTaskId,
            this, new Handler() {
                @Override/*from  w  ww  . j a v  a 2 s .  co m*/
                public void handleMessage(Message msg) {
                    if (msg.obj != null) {
                        task = (Task) msg.obj;
                        Intent intent = new Intent();
                        intent.putExtra("taskId", task.getId());
                        intent.putExtra("insertedAt", insertAt);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        Toast.makeText(getApplicationContext(), R.string.save_error, Toast.LENGTH_LONG).show();
                    }
                }
            });
}

From source file:de.teunito.android.cyclelife.RennradNewsShare.java

/** Called when the activity is first created. */
@Override/*  w ww  .  j a va2s  . c  o m*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.share_rennradnews);

    Bundle data = new Bundle();
    data = getIntent().getExtras();
    trackId = data.getLong("trackId");

    mTrackDb = TrackDb.getInstance(getApplicationContext());

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    APIKey = prefs.getString(Preferences.RENNRADNEWS_API_KEY, "");
    bikeId = prefs.getString(Preferences.RENNRADNEWS_BIKE_ID, "");
    weight = prefs.getString(Preferences.WEIGHT, "");

    tv = (TextView) findViewById(R.id.rnsShareTitle);
    tv.setText("Share your track " + trackId + " to rennrad-news.de community!");
    bt = (Button) findViewById(R.id.rnsShareBtn);
    etTemp = (EditText) findViewById(R.id.rnsShareTemp);
    spSports = (Spinner) findViewById(R.id.rnsShareSports);
    spZone = (Spinner) findViewById(R.id.rnsShareZone);
    spWeather = (Spinner) findViewById(R.id.rnsShareWeather);
    spMood = (Spinner) findViewById(R.id.rnsShareMood);

    sportsAdapter = ArrayAdapter.createFromResource(this, R.array.sports, android.R.layout.simple_spinner_item);
    zoneAdapter = ArrayAdapter.createFromResource(this, R.array.zone, android.R.layout.simple_spinner_item);
    weatherAdapter = ArrayAdapter.createFromResource(this, R.array.weather,
            android.R.layout.simple_spinner_item);
    moodAdapter = ArrayAdapter.createFromResource(this, R.array.mood, android.R.layout.simple_spinner_item);

    sportsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    zoneAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    weatherAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    moodAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spSports.setAdapter(sportsAdapter);
    spZone.setAdapter(zoneAdapter);
    spWeather.setAdapter(weatherAdapter);
    spMood.setAdapter(moodAdapter);

    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(RennradNewsShare.this, "", "Uploading. Please wait...", true);

            sportsID = String.valueOf(spSports.getSelectedItemPosition() + 1);
            zoneId = String.valueOf(spZone.getSelectedItemPosition() + 1);
            weatherId = String.valueOf(spWeather.getSelectedItemPosition() + 1);
            moodId = String.valueOf(spMood.getSelectedItemPosition() + 1);
            temperature = etTemp.getText().toString();

            // execute is a blocking call, it's best to call this code in a thread separate from the ui's
            uploadThread.start();
        }
    });

    handler = new Handler() {
        public void handleMessage(Message msg) {
            String result = msg.getData().getString("result");
            if (result.contains("success")) {
                Toast.makeText(getApplicationContext(), "Uploaded: " + result, Toast.LENGTH_LONG).show();
                finish();
            } else
                Toast.makeText(getApplicationContext(), "Error: " + result, Toast.LENGTH_LONG).show();
        }
    };

    if (APIKey.length() < 20) {
        AlertDialog.Builder builder = new AlertDialog.Builder(RennradNewsShare.this);
        builder.setMessage("Please enter first the rennrad-news.de API-key in the Settings!")
                .setCancelable(false).setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        RennradNewsShare.this.finish();
                        startActivity(new Intent(RennradNewsShare.this, Preferences.class));
                    }
                });
        builder.create().show();
    }
}