Java 每分钟输出10分钟

Java 每分钟输出10分钟,java,android,timer,Java,Android,Timer,我正试图在十分钟内每分钟为android应用程序打印一些东西。我更愿意访问手机的时钟,让代码在特定时间自动启动。然而,我并不完全确定如何做这一部分。这是我到目前为止所拥有的 这是我的主要代码,我想添加下面的代码以及下面提供的答案: package com.example.james.texttospeech; import android.os.Bundle; import android.app.Activity; import android.view.V

我正试图在十分钟内每分钟为android应用程序打印一些东西。我更愿意访问手机的时钟,让代码在特定时间自动启动。然而,我并不完全确定如何做这一部分。这是我到目前为止所拥有的

这是我的主要代码,我想添加下面的代码以及下面提供的答案:

    package com.example.james.texttospeech;


    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.view.View;
    import android.widget.EditText;
    import android.speech.tts.TextToSpeech;
    import android.speech.tts.TextToSpeech.OnInitListener;
    import android.content.Intent;
    import java.util.Locale;
    import android.widget.Toast;

    public class MainActivity extends Activity implements OnClickListener, OnInitListener {

    //TTS object
    private TextToSpeech myTTS;
    //status check code
    private int MY_DATA_CHECK_CODE = 0;

    //create the Activity
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //get a reference to the button element listed in the XML layout
        Button speakButton = (Button)findViewById(R.id.speak);
        Button speakButton1 = (Button)findViewById(R.id.button);
        Button speakButton2 = (Button)findViewById(R.id.button2);
        //listen for clicks
        speakButton.setOnClickListener(this);
        speakButton1.setOnClickListener(this);
        speakButton2.setOnClickListener(this);
        //check for TTS data
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
    }

    //respond to button clicks
    public void onClick(View v) {

        //get the text entered
        EditText enteredText = (EditText)findViewById(R.id.enter);
        String words = enteredText.getText().toString();
        speakWords(words);
    }

    //speak the user text
    private void speakWords(String speech) {

        //speak straight away
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
    }

    //act on result of TTS data check
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                //the user has the necessary data - create the TTS
                myTTS = new TextToSpeech(this, this);
            }
            else {
                //no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }

    //setup TTS
    public void onInit(int initStatus) {

        //check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        }
        else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
        }
    }
    }

import java.util.*;
导入java.util.Date;
导入java.util.Timer;
导入java.util.TimerTask;
公共类类执行任务{
长延时=10*60000;//延时(毫秒)
LoopTask任务=新建LoopTask();
定时器=新定时器(“任务名称”);
公开作废开始(){
timer.cancel();
定时器=新定时器(“任务名称”);
日期executionDate=新日期();//没有参数=现在
timer.scheduleAtFixedRate(任务、执行日期、延迟);
}
私有类LoopTask扩展TimerTask{
公开募捐{
同时(延迟>0){
System.out.println(“队形将在“+延迟+”分钟内形成”);
延迟--;
}
}
}
公共静态void main(字符串[]args){
ClassExecutingTask executingTask=新建ClassExecutingTask();
executingTask.start();
}
}
公开募捐{
同时(延迟>0){
System.out.println(“队形将在“+延迟+”分钟内形成”);
延迟--;
}
}
公共静态void main(字符串[]args){
ClassExecutingTask executingTask=新建ClassExecutingTask();
executingTask.start();
}

如果您想在android中执行上述操作,那么下面的代码将帮助您在onCreate()方法中添加这些行


谢谢,现在如果我想把它连接到一个被按下的按钮上,我会把它添加到那个按钮中吗?是的。那样做。如果我的回答有帮助,请接受。上面说我可以在2分钟内完成,对不起,我对这整件事不熟悉。没有问题,但请在两分钟后完成。完成,如果我需要更多帮助,我将在这里发布。再次感谢。
import java.util.*;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

    public class ClassExecutingTask {

        long delay = 10 * 60000; // delay in milliseconds
        LoopTask task = new LoopTask();
        Timer timer = new Timer("TaskName");

        public void start() {
            timer.cancel();
            timer = new Timer("TaskName");
            Date executionDate = new Date(); // no params = now
            timer.scheduleAtFixedRate(task, executionDate, delay);
        }

        private class LoopTask extends TimerTask {

            public void run() {

                while (delay > 0) {
                    System.out.println("Formation will form up in " + delay + " minutes");
                    delay--;
                }

            }
        }

        public static void main(String[] args) {
            ClassExecutingTask executingTask = new ClassExecutingTask();
            executingTask.start();
        }

    }

<!---->

    public void run() {

        while (delay > 0) {
            System.out.println("Formation will form up in " + delay + " minutes");
            delay--;
        }

    }

<!---->

    public static void main(String[] args) {
        ClassExecutingTask executingTask = new ClassExecutingTask();
        executingTask.start();
    }
new CountDownTimer(600000, 60000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long

             public void onTick(long millisUntilFinished) {
                  System.out.println("Your message on every minute");
              //here you can have your logic to set text to edittext
             }

             public void onFinish() {
                  System.out.println("Your message after 10 minute");
             }
            }
            .start();