Translate

Sonntag, 20. März 2016

Android-Java-AsyncTask Beispiel

package com.sshapp.tim.sshservershutdownandrestart;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.text.DateFormat;
import java.util.Date;
import java.util.Properties;

public class Main extends AppCompatActivity{

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("MyApp");
        Button mybutton1 = (Button) 
           findViewById(R.id.mybutton1);
        Button mybutton2 = (Button)
           findViewById(R.id.mybutton2);

        //Set click events        mybutton1.setOnClickListener(new View.OnClickListener(){
            @Override            public void onClick(View view) {
                onRestartClick((Button) view);
            }
        });
        mybutton12.setOnClickListener(new View.OnClickListener(){
            @Override            public void onClick(View view) {
                onShutdownClick((Button) view);
            }
        });
    }

    private void onRestartClick(Button view) {
        new AsyncSSHTask().execute("param1");
    }

    private void onShutdownClick(Button view) {
        new AsyncSSHTask().execute("param1");
    }

    private void ScrollViewDown(){
        ScrollView scrollViewLogging = (ScrollView)
           findViewById(R.id.scrollViewScroll);
        scrollViewLogging.post(new Runnable() {
            public void run() {
                ScrollView scrollViewLogging = (ScrollView)
                   findViewById(R.id.scrollViewScroll);
                scrollViewLogging.fullScroll(View.FOCUS_DOWN);
            }
        });
    }

    private void Log(String text){
        TextView textViewLogging = (TextView)
           findViewById(R.id.textViewLogging);
        String currentDateTimeString = DateFormat. 
           getDateTimeInstance().format(new Date());
        textViewLogging.append(currentDateTimeString + ": "  
           + text + "\n");
        ScrollViewDown();
    }

    public void LogPublic(String text) {
        final String str = text;
        Main.this.runOnUiThread(new Runnable() {
            @Override            public void run() {
                Log(str);
            }
        });
    }

    public class MyAsyncTask extends AsyncTask<String, Integer, 
       Long> {
        @Override        protected Long doInBackground(String... params) {
            try            {
                //Your code here
                return new Long(1);
            }
            catch (Exception ex)
            {
                LogPublic(ex.getMessage());
                return new Long(0);
            }
        }
    }

}

Keine Kommentare:

Kommentar veröffentlichen