Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. package com.tomecki.counterpushups;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.Service;
  7. import android.app.TaskStackBuilder;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.database.Cursor;
  12. import android.database.sqlite.SQLiteDatabase;
  13. import android.os.CountDownTimer;
  14. import android.os.IBinder;
  15. import android.support.v7.app.NotificationCompat;
  16. import android.util.Log;
  17.  
  18. import java.text.SimpleDateFormat;
  19. import java.util.ArrayList;
  20. import java.util.Calendar;
  21. import java.util.HashMap;
  22.  
  23. public class MyService extends Service {
  24. public MyService() {
  25. }
  26.  
  27. int hours_with_setting;
  28.  
  29. String nameDay;
  30.  
  31. String title = "";
  32.  
  33. public static SharedPreferences sharedpreferences_date_now;
  34. public static final String MyPREFERENCES_date_now = "MyPrefs_date_now";
  35.  
  36. private CountDownTimer countDownTimer;
  37. private long totalTimeCountInMilliseconds;
  38.  
  39. @Override
  40. public void onCreate(){
  41. super.onCreate();
  42.  
  43. sharedpreferences_date_now = getSharedPreferences(MyPREFERENCES_date_now, Context.MODE_PRIVATE);
  44.  
  45. hours_with_setting = sharedpreferences_date_now.getInt("hours", 0);
  46.  
  47. Log.d("service- ", "oncreate");
  48.  
  49.  
  50. }
  51.  
  52. @Override
  53. public int onStartCommand(Intent intent, int flags, int startId) {
  54.  
  55. Log.d("service- ", "startcommnad");
  56.  
  57. setTimer();
  58. startTimer();
  59.  
  60. return Service.START_STICKY;
  61. }
  62.  
  63. @Override
  64. public void onDestroy(){
  65. super.onDestroy();
  66. }
  67.  
  68.  
  69. @Override
  70. public IBinder onBind(Intent intent) {
  71. return null;
  72. }
  73.  
  74. public void getDay() {
  75.  
  76. Log.d("service- ", "getday");
  77.  
  78. Calendar calendar = Calendar.getInstance();
  79.  
  80. SimpleDateFormat df_end = new SimpleDateFormat("EEEE");
  81. String formattedDate_end = df_end.format(calendar.getTime());
  82. if (formattedDate_end.equals("niedziela")) {
  83. nameDay = "Data." + "sun";
  84. } else if (formattedDate_end.equals("poniedziałek")) {
  85. nameDay = "Data." + "mon";
  86. } else if (formattedDate_end.equals("wtorek")) {
  87. nameDay = "Data." + "tue";
  88. } else if (formattedDate_end.equals("środa")) {
  89. nameDay = "Data." + "wed";
  90. } else if (formattedDate_end.equals("czwartek")) {
  91. nameDay = "Data." + "thu";
  92. } else if (formattedDate_end.equals("piątek")) {
  93. nameDay = "Data." + "fri";
  94. } else if (formattedDate_end.equals("sobota")) {
  95. nameDay = "Data." + "sat";
  96. }
  97. SQLiteDatabase db = MainActivity.dbhelper.getReadableDatabase();
  98.  
  99. Cursor c = db.query(Data.TABLE_NAME,
  100. new String[]{nameDay, Data.TITLE}, null, null, null, null, null);
  101.  
  102. while (c.moveToNext()) {
  103. if (c.getString(c.getColumnIndex(nameDay)).equals("true")) {
  104.  
  105. if (c.isLast()) {
  106.  
  107. title = title + c.getString(c.getColumnIndex(Data.TITLE));
  108.  
  109.  
  110. Log.i("Start", "notification");
  111.  
  112. Intent intent = new Intent(this, MyService.class);
  113. PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
  114.  
  115. Notification n = new Notification.Builder(this)
  116. .setContentTitle("Nie zapomnij o treningach:")
  117. .setSmallIcon(R.mipmap.ic_launcher)
  118. .setContentIntent(pIntent)
  119. .setContentText(""+title)
  120. .setStyle(new Notification.BigTextStyle().bigText(""+title))
  121. .setAutoCancel(true).build();
  122.  
  123.  
  124. NotificationManager notificationManager =
  125. (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  126.  
  127. notificationManager.notify(0, n);
  128.  
  129. title = "";
  130.  
  131. } else{
  132. title = title + c.getString(c.getColumnIndex(Data.TITLE))+", "+"\n";
  133. }
  134. } else {
  135.  
  136. }
  137. }
  138.  
  139.  
  140. }
  141.  
  142.  
  143. private void setTimer() {
  144.  
  145. totalTimeCountInMilliseconds = 60 * 1000;
  146.  
  147. }
  148.  
  149. private void startTimer() {
  150. countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 1000) {
  151.  
  152.  
  153. @Override
  154. public void onTick(long leftTimeInMilliseconds) {
  155.  
  156. }
  157.  
  158. @Override
  159. public void onFinish() {
  160.  
  161. Log.d("service- ", "onfinish");
  162.  
  163.  
  164. Calendar calendar = Calendar.getInstance();
  165.  
  166. SimpleDateFormat df_end = new SimpleDateFormat("HHmm");
  167. String formattedDate_end = df_end.format(calendar.getTime());
  168. int hours = Integer.parseInt(formattedDate_end);
  169.  
  170. if(hours >= hours_with_setting){
  171.  
  172. setTimer();
  173. startTimer();
  174.  
  175. getDay();
  176.  
  177. } else{
  178.  
  179. setTimer();
  180. startTimer();
  181.  
  182. }
  183.  
  184. }
  185.  
  186. }.start();
  187.  
  188. }
  189.  
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement