iT邦幫忙

0

Android app Task時間問題

程式碼多 請見諒 我只是想做一個鬧鐘
它能在設定時間用EditText最古老的方式
而取得EditText只能使用點擊按鈕來獲取
這邊的OnClick我叫他create
我的方法是 目前時間-當前時間 如果是負的或等於0 就叫一聲
但是我這邊設timer.schedule(task,20000); 等於20秒
我在模擬器一開啟20秒後就會自動關閉
android 提示錯誤是在 timeP = ut2 - ut1;//毫秒差 這裡 可以利用f3搜尋
https://ithelp.ithome.com.tw/upload/images/20180418/20108822PpraSvfIbq.png

package com.example.win10.chapter7;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.graphics.YuvImage;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.media.SoundPool;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener,

    DatePickerDialog.OnDateSetListener,

    TimePickerDialog.OnTimeSetListener, View.OnClickListener

{

Calendar c = Calendar.getInstance();
TextView txDate;
TextView txTime;
TextView tx3, tx4, t1, t2, t3;
EditText ed1;
Timer timer = new Timer();
int year= c.get(Calendar.YEAR);
int month= c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY) - 4;
int minute = c.get(Calendar.MINUTE);
String dts,receive;
Date dt,dt1,dt2;
SimpleDateFormat sdf;
Long ut1,ut2,timeP,sec;
TimerTask task;

{
    task = new TimerTask () {

        public void run() {
            timeP = ut2 - ut1;//毫秒差
            sec = timeP / 1000;
            if (sec <= 0) {
                Uri notification = RingtoneManager.getDefaultUri (RingtoneManager.TYPE_NOTIFICATION);
                Ringtone r = RingtoneManager.getRingtone (getApplicationContext (), notification);
                r.play ();

            } else
                tx3.setText (" nothing to do");


        }
    };
}


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

        try{
            txDate = findViewById(R.id.tx1);
            txTime = findViewById(R.id.tx2);
            t1 = findViewById(R.id.t1);
            t2 = findViewById(R.id.t2);
            t3 = findViewById(R.id.t3);
            tx3 = findViewById(R.id.tx3);
            tx4 = findViewById(R.id.tx4);
            tx3.setText("今日日期" + year + "/" + month + "/" + day);
            tx4.setText("今日時間" + hour + ":" + minute);
            ed1 = findViewById(R.id.ed1);
            txDate.setOnClickListener(this);
            txTime.setOnClickListener(this);
            timer.schedule(task,20000);

        }catch (Exception e)
        {
            System.out.print(e);
        }









    } @Override
public void onDateSet(DatePicker view, final int y, int m, int d)
{

     year = c.get(Calendar.YEAR);
     month = c.get(Calendar.MONTH)+1;
     day = c.get(Calendar.DAY_OF_MONTH);
    t1.setText(y+"/"+(m+1)+"/"+d);


}




@Override
public void onTimeSet(TimePicker view, int h, int m)
{
        t2.setText(+h+":"+m);
        hour = c.get(Calendar.HOUR_OF_DAY)-4;
        minute = c.get(Calendar.MINUTE);


}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

}

@Override
public void onClick(View v)
{

    if (v==txDate)
    {
        new DatePickerDialog(this,this,
                c.get(Calendar.YEAR),
                c.get(Calendar.MONTH),
                c.get(Calendar.DAY_OF_MONTH)).show();

    }
    else if (v==txTime)
    {
        new  TimePickerDialog(this,this,
                c.get(Calendar.HOUR_OF_DAY),
                c.get(Calendar.MINUTE),
                true).show();
    }



}
public void create(View v) throws ParseException {
        try {
            sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.TAIWAN);
            dt=new Date();
            dts=sdf.format(dt);
            dt1 =sdf.parse(dts);
            receive = ed1.getText().toString();
            dt2 =sdf.parse(receive);
            ut1=dt1.getTime();
            ut2=dt2.getTime();

        }catch (Exception e){
            System.out.print(e);

        }





}

}

尚未有邦友回答

立即登入回答