Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.countdowntimerservice;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * Created by igord on 21/09/2017.
- */
- public class BootReceiver extends BroadcastReceiver {
- String date_time;
- Calendar calendar;
- SimpleDateFormat simpleDateFormat;
- SharedPreferences.Editor mEditor;
- SharedPreferences mpref;
- Date horaquedesligou;
- Date horaatual;
- Date temporestante;
- @Override
- public void onReceive(Context context, Intent intent) {
- mpref = PreferenceManager.getDefaultSharedPreferences(context);
- mEditor = mpref.edit();
- calendar = Calendar.getInstance();
- simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
- date_time = simpleDateFormat.format(calendar.getTime());
- String horaquedesligous = mpref.getString("horatelefone","");
- String temporestanntes = mpref.getString("tempofaltando","");
- /* try {
- horaquedesligou = simpleDateFormat.parse(horaquedesligous);
- horaatual = simpleDateFormat.parse(date_time);
- temporestante = simpleDateFormat.parse(temporestanntes);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- long diferenca = horaquedesligou.getTime() - horaatual.getTime();
- long horasrestantes = temporestante.getTime()-diferenca;*/
- mEditor.putString("data", date_time).commit();
- mEditor.putString("hours","" /*Aqui tenho que passar as horas restantes*/).commit();
- Intent intent_service = new Intent(context, Timer_Service.class);
- context.startService(intent_service);
- Log.e("igr", "iniciou servico do timer");
- }
- }
- package com.countdowntimerservice;
- import android.app.Service;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Handler;
- import android.os.IBinder;
- import android.preference.PreferenceManager;
- import android.support.annotation.Nullable;
- import android.util.Log;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.concurrent.TimeUnit;
- public class Timer_Service extends Service {
- public static String str_receiver = "com.countdowntimerservice.receiver";
- private Handler mHandler = new Handler();
- Calendar calendar;
- SimpleDateFormat simpleDateFormat;
- String strDate;
- Date date_current, date_diff;
- SharedPreferences mpref;
- SharedPreferences.Editor mEditor;
- private Timer mTimer = null;
- public static final long NOTIFY_INTERVAL = 1000;
- Intent intent;
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- mpref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
- mEditor = mpref.edit();
- calendar = Calendar.getInstance();
- simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
- mTimer = new Timer();
- mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
- intent = new Intent(str_receiver);
- }
- class TimeDisplayTimerTask extends TimerTask {
- @Override
- public void run() {
- mHandler.post(new Runnable() {
- @Override
- public void run() {
- calendar = Calendar.getInstance();
- simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
- strDate = simpleDateFormat.format(calendar.getTime());
- Log.e("strDate", strDate);
- mEditor.putString("horatelefone", strDate).commit();
- twoDatesBetweenTime();
- }
- });
- }
- }
- public String twoDatesBetweenTime() {
- try {
- date_current = simpleDateFormat.parse(strDate);
- } catch (Exception e) {
- }
- try {
- date_diff = simpleDateFormat.parse(mpref.getString("data", ""));
- } catch (Exception e) {
- }
- try {
- long diff = date_current.getTime() - date_diff.getTime();
- int int_hours = Integer.valueOf(mpref.getString("hours", ""));
- long int_timer = TimeUnit.HOURS.toMillis(int_hours);
- long long_hours = int_timer - diff;
- long diffSeconds2 = long_hours / 1000 % 60;
- long diffMinutes2 = long_hours / (60 * 1000) % 60;
- long diffHours2 = long_hours / (60 * 60 * 1000) % 24;
- if (long_hours > 0) {
- String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2;
- Log.e("TIME", str_testing);
- fn_update(str_testing);
- } else {
- mEditor.putBoolean("finish", true).commit();
- mTimer.cancel();
- }
- }catch (Exception e){
- mTimer.cancel();
- mTimer.purge();
- }
- return "";
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.e("Serivco finalizado","Finalizado");
- }
- private void fn_update(String str_time){
- mEditor.putString("tempofaltando", str_time).commit();
- intent.putExtra("time",str_time);
- sendBroadcast(intent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement