Advertisement
Guest User

Untitled

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