Advertisement
slaer

Service_MyApp

Jan 29th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. package ru.greengobyte.ican;
  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.Context;
  8. import android.content.Intent;
  9. import android.content.pm.LauncherApps;
  10. import android.graphics.Bitmap;
  11. import android.graphics.BitmapFactory;
  12. import android.graphics.drawable.BitmapDrawable;
  13. import android.graphics.drawable.Drawable;
  14. import android.graphics.drawable.Icon;
  15. import android.media.RingtoneManager;
  16. import android.os.Handler;
  17. import android.os.IBinder;
  18. import android.support.v4.app.NotificationCompat;
  19. import android.support.v4.app.NotificationManagerCompat;
  20. import android.support.v4.content.ContextCompat;
  21. import android.util.Log;
  22. import android.widget.Toast;
  23.  
  24. import org.xmlpull.v1.XmlPullParser;
  25. import org.xmlpull.v1.XmlPullParserException;
  26. import org.xmlpull.v1.XmlPullParserFactory;
  27.  
  28. import java.io.FileInputStream;
  29. import java.io.IOException;
  30. import java.util.ArrayList;
  31. import java.util.Random;
  32. import java.util.Timer;
  33. import java.util.TimerTask;
  34. import java.util.concurrent.TimeUnit;
  35.  
  36. import me.leolin.shortcutbadger.ShortcutBadger;
  37.  
  38. public class DoIT extends Service {
  39.  
  40.     final String LOG_TAG = "myLogs";
  41.     NotificationManager nm;
  42.     public static int notify = 120000;
  43.     private Handler mHandler = new Handler();
  44.     private Timer mTimer = null;
  45.     private int day = 24;
  46.  
  47.     public void DoIT() {
  48.  
  49.         String[] doitArr = {
  50.                 "Ok",
  51.                 "Good",
  52.                 "Bad"
  53.         };
  54.  
  55.         int max = doitArr.length;
  56.         int min = 1;
  57.         Random r = new Random();
  58.         int itr = r.nextInt((max - min + 1));
  59.         String tr = doitArr[itr];
  60.  
  61.  
  62.         Intent intent = new Intent(this, NotifiWin.class);
  63.         intent.putExtra("id", 1);
  64.         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  65.  
  66.         NotificationCompat.Builder nt = new NotificationCompat.Builder(this);
  67.         nt.setContentTitle(getString(R.string.app_name))
  68.                 .setContentText(tr)
  69.                 .setSmallIcon(getApplicationInfo().icon)
  70.                 .setContentIntent(pendingIntent)
  71.                 .setWhen(System.currentTimeMillis())
  72.                 .setAutoCancel(true);
  73.  
  74.         nt.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
  75.  
  76.         nm.notify(1,nt.build());
  77.     }
  78.  
  79.  
  80.  
  81.     public void onCreate() {
  82.         super.onCreate();
  83.         nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  84.         if (mTimer != null)
  85.             mTimer.cancel();
  86.         else
  87.             mTimer = new Timer();
  88.         mTimer.scheduleAtFixedRate(new TimeDisplay(), notify, notify);
  89.     }
  90.  
  91.     @Override
  92.     public int onStartCommand(Intent intent, int flags, int startId) {
  93.         return super.onStartCommand(intent, flags, startId);
  94.         //super.onStartCommand(intent, flags, startId);
  95.         //return START_STICKY;
  96.  
  97.     }
  98.  
  99.     public void onDestroy() {
  100.         super.onDestroy();
  101.     }
  102.  
  103.     @Override
  104.     public IBinder onBind(Intent intent) {
  105.         //return null;
  106.         // TODO: Return the communication channel to the service.
  107.         throw new UnsupportedOperationException("Not yet implemented");
  108.     }
  109.  
  110.     class TimeDisplay extends TimerTask {
  111.         @Override
  112.         public void run() {
  113.             mHandler.post(new Runnable() {
  114.                 @Override
  115.                 public void run() {
  116.                     DoIT();
  117.                 }
  118.             });
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement