Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public class CloseOrderService extends Service {
  2. private Notification notification;
  3. private SharedPreferences mPrefs;
  4. private ExecutorService es;
  5.  
  6. @Override
  7. public void onCreate() {
  8. super.onCreate();
  9. es = Executors.newFixedThreadPool(3);
  10. }
  11. @Override
  12. public int onStartCommand(Intent intent, int flags, int startId) {
  13. Notification.Builder builder = new Notification.Builder(this)
  14. .setSmallIcon(R.drawable.ic_pizza_notification).setContentTitle("Title").setContentText("some content" + startId);
  15. if (Build.VERSION.SDK_INT < 16)
  16. notification = builder.getNotification();
  17. else
  18. notification = builder.build();
  19. startForeground(772, notification);
  20. MyRun mr = new MyRun(startId);
  21. es.execute(mr);
  22. return Service.START_NOT_STICKY;
  23. }
  24.  
  25.  
  26.  
  27. @Override
  28. public void onDestroy() {
  29. super.onDestroy();
  30. Log.e(TAG,"Destroy service");
  31. stopForeground(true);
  32. }
  33.  
  34. @Nullable
  35. @Override
  36. public IBinder onBind(Intent intent) {
  37. return null;
  38. }
  39.  
  40.  
  41. class MyRun implements Runnable {
  42.  
  43. final int startId;
  44. final long orderId;
  45.  
  46. public MyRun(int startId) {
  47. this.startId = startId;
  48. }
  49.  
  50. public void run() {
  51. try {
  52. TimeUnit.SECONDS.sleep(Settings.WAIT_BEFORE_CLOSE_ORDER_SEC);
  53. } catch (InterruptedException e) {
  54. e.printStackTrace();
  55. }
  56. //send some data to server
  57. stop();
  58. }
  59. void stop() {
  60. Log.d(TAG, "MyRun#" + startId + " end, stopSelfResult("
  61. + startId + ") = " + stopSelfResult(startId));
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement