每分钟输出十分钟

时间:2015-04-14 17:07:48

标签: java android timer

我正在尝试每分钟为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.*;
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();
    }

1 个答案:

答案 0 :(得分:0)

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

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();