Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.nihal.myapplication;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Handler;
- import android.os.IBinder;
- import android.widget.Toast;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- /**
- * Created by Boiijek on 21/10/2017.
- */
- public class TimeService extends Service {
- // constant
- public static final long NOTIFY_INTERVAL = 10 * 1000; // 10 seconds
- // run on another Thread to avoid crash
- private Handler mHandler = new Handler();
- // timer handling
- private Timer mTimer = null;
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- // cancel if already existed
- if(mTimer != null) {
- mTimer.cancel();
- } else {
- // recreate new
- mTimer = new Timer();
- }
- // schedule task
- mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
- }
- class TimeDisplayTimerTask extends TimerTask {
- @Override
- public void run() {
- // run on another thread
- mHandler.post(new Runnable() {
- @Override
- public void run() {
- // display toast
- Toast.makeText(getApplicationContext(), getDateTime(),
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- private String getDateTime() {
- // get date time in custom format
- SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
- return sdf.format(new Date());
- }
- }
- @Override
- public void onDestroy()
- {
- mTimer.cancel();
- super.onDestroy();
- Toast.makeText(this, "TimeService has stopped!",Toast.LENGTH_LONG).show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement