2016-12-14_15-52-07.png

activity_main.xml;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.okul.otomatikgorevzamanlama.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="saati göster"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/zamantext"
        android:textSize="30sp" />

    <Button
        android:text="5 Saniyede 1 kere çalıştır"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonsurekli"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="38dp" />

    <Button
        android:text="Dur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttondur"
        android:layout_marginTop="13dp"
        android:layout_below="@+id/buttonsurekli"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>


MainActivity.java;
package com.example.okul.otomatikgorevzamanlama;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    Button butonsurekli,butondur;
    TextView zamantext;
    //nesneleri koda tanıtma

    Timer ZamanAl;
    GorevZamanlamaSinifi GorevZamanlayici;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        butonsurekli=(Button) findViewById(R.id.buttonsurekli);
        butondur=(Button) findViewById(R.id.buttondur);
        zamantext=(TextView) findViewById(R.id.zamantext);
        //xml ile neneleri bağlama
        if(ZamanAl != null){
            ZamanAl.cancel();
        }
        ZamanAl=new Timer();
        GorevZamanlayici=new GorevZamanlamaSinifi();
        //sınıfı çağır


        butonsurekli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ZamanAl.schedule(GorevZamanlayici,1000,5000);
                //her 5 saniyede bir görevi tekrarla
            }
        });

        butondur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ZamanAl!=null){
                    ZamanAl.cancel();
                    ZamanAl = null;
                    //işlemi durdur
                }
            }
        });
    }
    //zamanlama için sınıf üretimi
class GorevZamanlamaSinifi extends TimerTask {

    @Override
    public void run() {
        Calendar takvim = Calendar.getInstance();
        SimpleDateFormat BasTarihAl =
                new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
        final String TarihStringi = BasTarihAl.format(takvim.getTime());

        runOnUiThread(new Runnable(){

            @Override
            public void run() {
                zamantext.setText(TarihStringi);
            }});
    }

}
}

not: Program her 5 saniyede bir zaman değerini text kutusuna alır.