2015-04-14 97 views
0

我试图在十分钟内每分钟输出一个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(); 
    } 

回答

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

谢谢,现在如果我希望它在调到一个正在按下的按钮,我会在该按钮中添加它吗? –

+0

是的。去做。 如果我的回答很有帮助,那么请接受它。 –

+0

它说我可以在2分钟内完成,对不起,我对这件事情是新的。 –