Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

start async task android

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.System;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class AsyncTaskActivity extends Activity implements OnClickListener {

    Button btn;
    AsyncTask<?, ?, ?> runningTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = findViewById(R.id.button1);

        // Because we implement OnClickListener, we only
        // have to pass "this" (much easier)
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        // Detect the view that was "clicked"
        switch (view.getId()) {
        case R.id.button1:
            if (runningTask != null)
                runningTask.cancel(true);
            runningTask = new LongOperation();
            runningTask.execute();
            break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Cancel running task(s) to avoid memory leaks
        if (runningTask != null)
            runningTask.cancel(true);
    }

    private final class LongOperation extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // We were cancelled; stop sleeping!
                }
            }
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            TextView txt = (TextView) findViewById(R.id.output);
            txt.setText("Executed"); // txt.setText(result);
            // You might want to change "executed" for the returned string
            // passed into onPostExecute(), but that is up to you
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java priority queue 
Java :: java 8 find in list 
Java :: dot operator java 
Java :: convert two bytes to int java 
Java :: simple function java 
Java :: Program type already present: android.support.v4.app.INotificationSideChannel 
Java :: java final meaning 
Java :: set look and feel system default java 
Java :: calling activity method from fragment: 
Java :: number format java 
Java :: how to round double to 2 decimal places java 
Java :: spring boot access images in resources folder 
Java :: java display message 
Java :: android maven dependency 
Java :: java find if element of list in present in another list 
Java :: set view layout params android 
Java :: array contain java 
Java :: pre increment and post increments java 
Java :: convert void * to int 
Java :: how to sum a 2d array in java 
Java :: convert array of char to string java 
Java :: remove last character from stringbuffer 
Java :: java explicit array declaration 
Java :: java string.format system.currenttimemillis 
Java :: button change text java 
Java :: reverse number java 
Java :: java swing get frame size 
Java :: java list last element 
Java :: float.compare java 
Java :: If you are using the git profile, you need to set a Git URI in your configuration. If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration. 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =