Guest User

Untitled

a guest
Nov 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. //Start foreground service
  2. Intent intent = new Intent(this, MyForeGroundService.class);
  3. intent.setAction(MyForeGroundService.ACTION_START_FOREGROUND_SERVICE);
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  5. startForegroundService(intent);
  6. }else{
  7. startService(intent);
  8. }
  9.  
  10. public class MyForeGroundService extends Service implements LocationListener {
  11.  
  12. public MyForeGroundService() {
  13. }
  14.  
  15. @Override
  16. public IBinder onBind(Intent intent) {
  17. // TODO: Return the communication channel to the service.
  18. throw new UnsupportedOperationException("Not yet implemented");
  19. }
  20.  
  21. @Override
  22. public void onCreate() {
  23. super.onCreate();
  24. wakeLock();
  25. }
  26.  
  27. @SuppressLint("InvalidWakeLockTag")
  28. public void wakeLock() {
  29. pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
  30. assert pm != null;
  31. wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "partialwl");
  32. wl.acquire();
  33. }
  34.  
  35. public void stopWakeLock() {
  36. wl.release();
  37. }
  38.  
  39. @Override
  40. public int onStartCommand(Intent intent, int flags, int startId) {
  41. if (intent != null) {
  42. String action = intent.getAction();
  43.  
  44. switch (action) {
  45. case ACTION_START_FOREGROUND_SERVICE:
  46. wakeLock();
  47. startForegroundService();
  48. break;
  49. case ACTION_STOP_FOREGROUND_SERVICE:
  50. stopWakeLock();
  51. stopForegroundService();
  52. break;
  53. case ACTION_PLAY:
  54. Intent openMap= new Intent(this,Map.class);
  55. startActivity(openMap);
  56. break;
  57. case ACTION_PAUSE:
  58. break;
  59. }
  60. }
  61. return START_STICKY;
  62. }
  63.  
  64. /* Used to build and start foreground service. */
  65. private void startForegroundService() {
  66. Log.d(TAG_FOREGROUND_SERVICE, "Start foreground service.");
  67.  
  68. // Create notification default intent.
  69. Intent intent = new Intent(this,Map.class);
  70. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  71.  
  72. // Create notification builder.
  73. NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  74.  
  75. //Go back to Map activity if user press at the notification
  76. builder.setContentIntent(pendingIntent)
  77. .setContentTitle("gEKOning...")
  78. .setContentText("Tap to open gEKOn app");
  79.  
  80.  
  81. builder.setWhen(System.currentTimeMillis());
  82. builder.setSmallIcon(R.mipmap.ic_launcher);
  83. Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
  84. builder.setLargeIcon(largeIconBitmap);
  85. // Make the notification max priority.
  86. builder.setPriority(Notification.PRIORITY_MAX);
  87. // Make head-up notification.
  88. builder.setFullScreenIntent(pendingIntent, true);
  89.  
  90.  
  91. // Build the notification.
  92. Notification notification = builder.build();
  93.  
  94. // Start foreground service.
  95. startForeground(1, notification);
  96. }
  97. private void stopForegroundService() {
  98. Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");
  99.  
  100. // Stop foreground service and remove the notification.
  101. stopForeground(true);
  102.  
  103. // Stop the foreground service.
  104. stopSelf();
  105. }
Add Comment
Please, Sign In to add comment