Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 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. private CountDownTimer countDownTimer;
  34. private long totalTimeCountInMilliseconds;
  35.  
  36. @Override
  37. public void onCreate(){
  38. super.onCreate();
  39.  
  40.  
  41. hours_with_setting = MainActivity.sharedpreferences_date_now.getInt("hours", 0);
  42.  
  43. Log.d("service- ", "oncreate");
  44.  
  45.  
  46. }
  47.  
  48. @Override
  49. public int onStartCommand(Intent intent, int flags, int startId) {
  50.  
  51. Log.d("service- ", "startcommnad");
  52.  
  53. totalTimeCountInMilliseconds = 60 * 1000;
  54.  
  55. countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 1000) {
  56.  
  57.  
  58. @Override
  59. public void onTick(long leftTimeInMilliseconds) {
  60.  
  61. }
  62.  
  63. @Override
  64. public void onFinish() {
  65.  
  66. Log.d("service- ", "onfinish");
  67.  
  68.  
  69. Calendar calendar = Calendar.getInstance();
  70.  
  71. SimpleDateFormat df_end = new SimpleDateFormat("HHmm");
  72. String formattedDate_end = df_end.format(calendar.getTime());
  73. int hours = Integer.parseInt(formattedDate_end);
  74.  
  75. if(hours >= hours_with_setting){
  76.  
  77. Log.d("service- ", "getday");
  78.  
  79. Calendar calendar1 = Calendar.getInstance();
  80.  
  81. SimpleDateFormat df_end1 = new SimpleDateFormat("EEEE");
  82. String formattedDate_end1 = df_end1.format(calendar1.getTime());
  83. if (formattedDate_end1.equals("niedziela")) {
  84. nameDay = "Data." + "sun";
  85. } else if (formattedDate_end1.equals("poniedziałek")) {
  86. nameDay = "Data." + "mon";
  87. } else if (formattedDate_end1.equals("wtorek")) {
  88. nameDay = "Data." + "tue";
  89. } else if (formattedDate_end1.equals("środa")) {
  90. nameDay = "Data." + "wed";
  91. } else if (formattedDate_end1.equals("czwartek")) {
  92. nameDay = "Data." + "thu";
  93. } else if (formattedDate_end1.equals("piątek")) {
  94. nameDay = "Data." + "fri";
  95. } else if (formattedDate_end1.equals("sobota")) {
  96. nameDay = "Data." + "sat";
  97. }
  98. SQLiteDatabase db = MainActivity.dbhelper.getReadableDatabase();
  99.  
  100. Cursor c = db.query(Data.TABLE_NAME,
  101. new String[]{nameDay, Data.TITLE}, null, null, null, null, null);
  102.  
  103. while (c.moveToNext()) {
  104. if (c.getString(c.getColumnIndex(nameDay)).equals("true")) {
  105.  
  106. if (c.isLast()) {
  107.  
  108. title = title + c.getString(c.getColumnIndex(Data.TITLE));
  109.  
  110.  
  111. Log.i("Start", "notification");
  112.  
  113. Intent intent = new Intent(getApplication(), MyService.class);
  114. PendingIntent pIntent = PendingIntent.getActivity(getApplication(), (int) System.currentTimeMillis(), intent, 0);
  115.  
  116. Notification n = new Notification.Builder(getApplication())
  117. .setContentTitle("Nie zapomnij o treningach:")
  118. .setSmallIcon(R.mipmap.ic_launcher)
  119. .setContentIntent(pIntent)
  120. .setContentText(""+title)
  121. .setStyle(new Notification.BigTextStyle().bigText(""+title))
  122. .setAutoCancel(true).build();
  123.  
  124.  
  125. NotificationManager notificationManager =
  126. (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  127.  
  128. notificationManager.notify(0, n);
  129.  
  130. title = "";
  131.  
  132. } else{
  133. title = title + c.getString(c.getColumnIndex(Data.TITLE))+", "+"\n";
  134. }
  135.  
  136. startService(new Intent(getApplication(), MyService.class));
  137.  
  138.  
  139. } else {
  140.  
  141. startService(new Intent(getApplication(), MyService.class));
  142.  
  143. }
  144. }
  145.  
  146. } else{
  147.  
  148.  
  149. }
  150.  
  151. }
  152.  
  153. }.start();
  154.  
  155.  
  156.  
  157.  
  158. return Service.START_STICKY;
  159. }
  160.  
  161. @Override
  162. public void onDestroy(){
  163. super.onDestroy();
  164. }
  165.  
  166.  
  167. @Override
  168. public IBinder onBind(Intent intent) {
  169. return null;
  170. }
  171.  
  172.  
  173.  
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement