android epoch时钟使用Async Task

时间:2015-08-11 18:13:03

标签: android android-asynctask

我正在尝试创建一个应用程序来显示当前的纪元时间,该时间正在按钮启动时实时更新,另一个按钮会停止它。我试图为此目的使用异步任务,但我遇到错误,因为没有在异步任务中采取不能应用于java.lang.string。 请帮忙。

package com.example.sangeeta.epochtimer;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.os.AsyncTask;
import android.provider.Settings.System;
import android.os.SystemClock;

import java.util.Calendar;


public class MainActivity extends ActionBarActivity {

TextView textonee;
Button start, stop;
long epochtime;
boolean pressstate;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pressstate = false;
    start = (Button) findViewById(R.id.button1);
    stop = (Button) findViewById(R.id.button2);



    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pressstate = true;
            new Operation().execute("");


        }
    });

    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //shutdown();

            pressstate = false;


        }
    });
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


private class Operation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {


        while (pressstate) {
            Calendar rightNow = Calendar.getInstance();

            // offset to add since we're not UTC

            //long offset = rightNow.get(Calendar.ZONE_OFFSET) +
                    //rightNow.get(Calendar.DST_OFFSET);

            //long sinceMidnight = (rightNow.getTimeInMillis() + offset) %
                    //(24 * 60 * 60 * 1000);

            epochtime=rightNow.getTimeInMillis();
            this.publishProgress(""+epochtime);
        }
        return null;
    }

    protected void onPostExecute(String result) {
        textonee = (TextView) findViewById(R.id.textViewepo);
        //textonee.setText("" + epochtime); // txt.setText(result);
        // might want to change "executed" for the returned string passed
        // into onPostExecute() but that is upto you
        super.onProgressUpdate(result);
        textonee.setText(result[0]);
    }
}


};

2 个答案:

答案 0 :(得分:0)

您需要覆盖onProgressUpdate而不是onPostExecute才能正确发布进度。

private class Operation extends AsyncTask<String, String, Void> {

@Override
protected void doInBackground(String... params) {


    while (pressstate) {
        Calendar rightNow = Calendar.getInstance();

        // offset to add since we're not UTC

        //long offset = rightNow.get(Calendar.ZONE_OFFSET) +
                //rightNow.get(Calendar.DST_OFFSET);

        //long sinceMidnight = (rightNow.getTimeInMillis() + offset) %
                //(24 * 60 * 60 * 1000);

        epochtime=rightNow.getTimeInMillis();
        this.publishProgress(""+epochtime);
    }
}

protected void onProgressUpdate(String result) {
    textonee = (TextView) findViewById(R.id.textViewepo);
    //textonee.setText("" + epochtime); // txt.setText(result);
    // might want to change "executed" for the returned string passed
    // into onPostExecute() but that is upto you
    super.onProgressUpdate(result);
    textonee.setText(result[0]);
}

protected void onPostExecute(Void... voids) {
// NO-OP
}

答案 1 :(得分:0)

private class Operation extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {


    while (pressstate) {
        Calendar rightNow = Calendar.getInstance();

        // offset to add since we're not UTC

        //long offset = rightNow.get(Calendar.ZONE_OFFSET) +
                //rightNow.get(Calendar.DST_OFFSET);

        //long sinceMidnight = (rightNow.getTimeInMillis() + offset) %
                //(24 * 60 * 60 * 1000);

        epochtime=rightNow.getTimeInMillis();
        this.publishProgress(""+epochtime);
    }
    return null;
}

protected void onPostExecute(String result) {
    textonee = (TextView) findViewById(R.id.textViewepo);
    //textonee.setText("" + epochtime); // txt.setText(result);
    // might want to change "executed" for the returned string passed
    // into onPostExecute() but that is upto you
    super.onProgressUpdate(result);
    textonee.setText(result);
}

}

试试这个