有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java每“X”分钟重复一次Android代码

我知道这个问题在这里已经提了10次了,但我一直有问题要解决,因为我对安卓非常熟悉

我很无聊,我想制作一个应用程序,用我的氏族服务器的最后一个TeamSpeak3连接打印一个列表。“服务器端”的工作已经完成了,因为我制作了一个Java程序,可以在TXT文件(ingenium.zapto.org/joins.txt)中打印最后的连接。现在,我只想制作一个Android应用程序,每“X”分钟打印一次TXT文件的内容,例如,每10分钟

我已经有了访问TXT文件并打印它的代码,但我不能让它每10分钟重新打印一次列表。 我知道有些工具,比如AlarmManager、处理程序、计时器。。。但我很困惑,我不知道在这种情况下使用什么是最好的,更重要的是,我不知道如何让它们在我的代码中工作

主要活动。java

package org.zapto.ingenium.fetchurl;

import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.TextView;


public class MainActivity extends ActionBarActivity {

private TextView txtJoins;

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

    txtJoins = (TextView)findViewById(R.id.TxtJoins);

    FetchURL fu = new FetchURL();

    fu.Run("http://192.168.0.10/joins.txt");

    String o = fu.getOutput();

    txtJoins.setText("Last connections: \n" + o);

}


@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);
}
}

获取URL。java

package org.zapto.ingenium.fetchurl;

/**
 * Created by Martin on 18/01/2015.
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class FetchURL {

private String output;
private String url;

public FetchURL()
{
    output = "";
}

public String getOutput()
{
    return output;
}


public void Run(String u)
{
    url = u;
    Thread t =  new Thread() {

        public void run() {

            URL textUrl;
            try {

                textUrl = new URL(url);

                BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));

                String StringBuffer;
                String stringText = "";
                while ((StringBuffer = bufferReader.readLine()) != null) {
                    stringText += "\n" + StringBuffer;
                }
                bufferReader.close();

                output = stringText;

            } catch (Exception e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();

                output= e.toString();
            }

        }
    };

    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

主要活动。xml

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/LytContenedorSaludo"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" >

<TextView 安卓:id="@+id/TxtJoins"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="" />

</LinearLayout>

总之,我想重复这3行代码(MainActivity.java):

    fu.Run("http://192.168.0.10/joins.txt");

    String o = fu.getOutput();

    txtJoins.setText("Last conections: \n" + o);

例如,每10分钟


共 (3) 个答案

  1. # 1 楼答案

    10分钟10分钟运行指令 http://developer.android.com/reference/android/os/CountDownTimer.html

    new CountDownTimer(10000, 1000) {
    
         public void onTick(long millisUntilFinished) {
    
         }
    
         public void onFinish() {
            fu.Run("http://192.168.0.10/joins.txt");
            String o = fu.getOutput();
            txtJoins.setText("Last conections: \n" + o);
            .start();
         }
      }.start();
    
  2. # 2 楼答案

    你可以添加一个Thread,让它每10分钟重复一次这个任务。像下面这样的方法会奏效。首先声明新的Thread

    public class MainActivity extends ActionBarActivity {
    
    private TextView txtJoins;
    private Thread repeatTaskThread;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        // Rest of code...
    

    然后创建一个包含新Thread的简单方法:

    private void RepeatTask()
    {
        repeatTaskThread = new Thread()
        {
            public void run()
            {
                while (true)
                {
    
                    FetchURL fu = new FetchURL();
                    fu.Run("http://192.168.0.10/joins.txt");
                    String o = fu.getOutput();
                    // Update TextView in runOnUiThread
                    runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            txtJoins.setText("Last connections: \n" + o);
                        }
                    });
                    try
                    {
                        // Sleep for 10 minutes
                        Thread.sleep(600000)
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            };
        };
        repeatTaskThread.start();
    }
    

    最后,在^{末尾调用您的方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Rest of code...
    
        RepeatTask();
    }
    
  3. # 3 楼答案

    在完成你的代码后,使用一个处理程序,它会在10分钟后再次执行这一切吗

    handlerTimer.postDelayed(new Runnable(){
        public void run() {
          // do something             
      }}, 600000);
    

    //1000=1秒//60000=60秒(1分钟)//600000=600秒(10分钟)