Android Open Source - CSCI567---Workspace Main Activity






From Project

Back to project page CSCI567---Workspace.

License

The source code is released under:

MIT License

If you think the Android project CSCI567---Workspace listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package csci567.asynctaskexample;
/*from www  .  j  av a  2s.com*/
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
 * @author Prabu
 * AsyncTask example
 *
 */
public class MainActivity extends Activity {
  private Button button;
  private EditText time;
  private TextView finalResult;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    time = (EditText) findViewById(R.id.et_time);
    button = (Button) findViewById(R.id.btn_do_it);
    finalResult = (TextView) findViewById(R.id.tv_result);
    //In-line OnClick Listener
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        AsyncTaskRunner runner = new AsyncTaskRunner();
        String sleepTime = time.getText().toString();
        runner.execute(sleepTime);
      }
    });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  /**
   * @author Prabu
   * Private class which runs the long operation. ( Sleeping for some time )
   * AsyncTask<Params, Progress, Result>
   */
  private class AsyncTaskRunner extends AsyncTask<String, String, String> {

    private String resp;

    @Override
    protected String doInBackground(String... params) {
      publishProgress("Sleeping..."); // Calls onProgressUpdate()
      try {
        // Do your long operations here and return the result
        int time = Integer.parseInt(params[0]);    
        // Sleeping for given time period
        Thread.sleep(time);
        resp = "Slept for " + time + " milliseconds";
      } catch (InterruptedException e) {
        e.printStackTrace();
        resp = e.getMessage();
      } catch (Exception e) {
        e.printStackTrace();
        resp = e.getMessage();
      }
      return resp;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(String result) {
      // execution of result of Long time consuming operation
      finalResult.setText(result);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {
      // Things to be done before execution of long running operation. For
      // example showing ProgessDialog
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onProgressUpdate(Progress[])
     */
    @Override
    protected void onProgressUpdate(String... text) {
      finalResult.setText(text[0]);
      // Things to be done while execution of long running operation is in
      // progress. For example updating ProgessDialog
    }
  }
}




Java Source Code List

com.example.linearlayoutexample.MainActivity.java
com.example.relativelayoutexample.MainActivity.java
csci567.FragmentExample.Fragmentmanageractivity.java
csci567.FragmentExample.MainActivity.java
csci567.FragmentExample.MyFragment.java
csci567.FragmentExample.MyStaticFragment.java
csci567.alarmexample.MainActivity.java
csci567.alarmexample.SampleAlarmReceiver.java
csci567.asynctaskexample.MainActivity.java
csci567.buttonexample.MainActivity.java
csci567.checkboxexample.MainActivity.java
csci567.doodleexample.MainActivity.java
csci567.doodleexample.SampleAlarmReceiver.java
csci567.doodleexample.SampleBootReceiver.java
csci567.doodleexample.SampleSchedulingService.java
csci567.eventreceiver.DataReceiver.java
csci567.eventreceiver.MainActivity.java
csci567.eventreceiver.RebootReceiver.java
csci567.helloworld.MainActivity.java
csci567.simpledbexample.DBHelper.java
csci567.simpledbexample.MainActivity.java
csci567.suggestionapp.MainActivity.java
csci567.writefile.MainActivity.java
org.ndeftools.boilerplate.AndroidNfcActivity.java
org.ndeftools.boilerplate.DefaultNfcBeamWriterActivity.java
org.ndeftools.boilerplate.DefaultNfcReaderActivity.java
org.ndeftools.boilerplate.DefaultNfcTagWriterActivity.java
org.ndeftools.boilerplate.NdefRecordAdapter.java
org.ndeftools.nfcdemo.TagViewer.java
org.ndeftools.nfcdemo.simulator.FakeTagsActivity.java
org.ndeftools.nfcdemo.simulator.MockNdefMessages.java