Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java android应用程序每10秒执行一次_Java_Android - Fatal编程技术网

java android应用程序每10秒执行一次

java android应用程序每10秒执行一次,java,android,Java,Android,对于我正在开发的android应用程序,我有以下代码: package com.exercise.AndroidInternetTxt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity

对于我正在开发的android应用程序,我有以下代码:

package com.exercise.AndroidInternetTxt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidInternetTxt extends Activity {

    TextView textMsg, textPrompt, textSite;
    final String textSource = "http://www.xxx/s.php";


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textPrompt = (TextView)findViewById(R.id.textprompt);
        textMsg = (TextView)findViewById(R.id.textmsg);
        textSite = (TextView)findViewById(R.id.textsite);

        //textPrompt.setText("Asteapta...");


        URL textUrl;
        try {
            textUrl = new URL(textSource);
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
            String StringBuffer;
            String stringText = "";
            while ((StringBuffer = bufferReader.readLine()) != null) {
                stringText += StringBuffer;
            }
            bufferReader.close();
            textMsg.setText(stringText);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        }


        //textPrompt.setText("Terminat!");

    }

}

它工作正常,它从.php文件输出一个文本。我希望它每10秒自动刷新一次,但我真的不知道怎么做。你能帮我解决这个问题吗?谢谢

在这里之前,这个问题应该被回答很多次。下面是我应该怎么做的

TimerTask fileProcessTask = new TimerTask(){

        @Override
        public void run() {
            //put your code to process the url here  
            processFile();

        }

    };

        Timer tm = new Timer();
        tm.schedule(fileProcessTask, 10000L);

应该工作

计时器任务将不工作,因为它将创建不同的线程,并且只有原始线程可以接触其视图

对于android来说,实现这一点的首选方法是使用处理程序。以太

textMsg.post( new Runnable(){

     public void run(){
            doProcessing();
            testMesg.setText("bla");
            testMsg.postDelayed(this,(1000*10));
     }
};
或者拥有单独的Handler类实例

Handler mHanlder = new Handler();
mHandler.post(runnable);

小心!您需要创建一个新线程并在那里完成繁重的工作。不要暂停
onCreate()
方法,否则程序将无法启动。