Advertisement
Guest User

LocationPlugin.java

a guest
Nov 8th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. package com.your.example;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationChannel;
  5. import android.app.NotificationManager;
  6. import android.app.Service;
  7. import android.content.Intent;
  8. import android.content.IntentFilter; // Add this import
  9. import android.net.Uri;
  10. import android.os.Build;
  11. import android.os.Handler;
  12. import android.os.IBinder;
  13. import android.os.PowerManager;
  14. import android.provider.Settings;
  15. import android.util.Log;
  16. import android.widget.Toast;
  17. import androidx.annotation.Nullable;
  18. import androidx.core.app.NotificationCompat;
  19.  
  20. public class LocationPlugin extends Service {
  21.  
  22. public static int number = 0;
  23. private final Handler handler = new Handler();
  24. private ServiceMessageReceiver receiver; // Declare the receiver
  25.  
  26. private Runnable sendData = new Runnable() {
  27. public void run() {
  28. number++;
  29. send(number);
  30. Log.d("LocationPlugin", "number is: " + number);
  31. // Show number on screen
  32. Toast.makeText(LocationPlugin.this, "Current number: " + number, Toast.LENGTH_SHORT).show();
  33. handler.postDelayed(this, 5000); // Sends every 5 seconds
  34. }
  35. };
  36.  
  37. public void send(int num) {
  38. Log.d("LocationPlugin", "Sending number: " + num);
  39. Intent sendIntent = new Intent();
  40. sendIntent.setAction("com.your.example.MESSAGE"); // Ensure this matches the action registered in Unity
  41. sendIntent.putExtra("message", num); // Ensure the key matches the one used in Unity
  42. sendBroadcast(sendIntent);
  43. }
  44.  
  45. @Override
  46. public int onStartCommand(Intent intent, int flags, int startId) {
  47. Log.d("LocationPlugin", "Service started.");
  48.  
  49. createNotificationChannel();
  50. Notification notification = new NotificationCompat.Builder(this, "LocationPluginChannel")
  51. .setContentTitle("Exampple Service")
  52. .setContentText("Running...") // Minimal notification text
  53. .setSmallIcon(android.R.drawable.ic_dialog_info)
  54. .setOngoing(true) // Keeps the notification ongoing
  55. .setVisibility(NotificationCompat.VISIBILITY_SECRET) // Hidden from lock screen
  56. .setPriority(NotificationCompat.PRIORITY_LOW) // Low priority
  57. .build();
  58.  
  59. startForeground(1, notification); // Start the service in the foreground
  60.  
  61. handler.postDelayed(sendData, 1000); // Start sending data after 1 second
  62.  
  63. // Register the broadcast receiver
  64. registerReceiver();
  65.  
  66. // Prompt user to disable battery optimizations for your app
  67. checkBatteryOptimization();
  68.  
  69. return START_REDELIVER_INTENT; // Ensure the last Intent is redelivered
  70. }
  71.  
  72. private void registerReceiver() {
  73. receiver = new ServiceMessageReceiver(); // Initialize the receiver
  74. IntentFilter filter = new IntentFilter("com.your.example.MESSAGE"); // Ensure this matches
  75. registerReceiver(receiver, filter); // Register the receiver
  76. }
  77.  
  78. private void unregisterReceiver() {
  79. if (receiver != null) {
  80. unregisterReceiver(receiver); // Unregister the receiver
  81. receiver = null; // Clear the reference
  82. }
  83. }
  84.  
  85. private void checkBatteryOptimization() {
  86. Intent intent = new Intent();
  87. String packageName = getPackageName();
  88. PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
  89. if (pm != null && !pm.isIgnoringBatteryOptimizations(packageName)) {
  90. intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
  91. intent.setData(Uri.parse("package:" + packageName));
  92.  
  93. // Add FLAG_ACTIVITY_NEW_TASK to the intent
  94. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  95. startActivity(intent);
  96. }
  97. }
  98.  
  99. @Override
  100. public void onDestroy() {
  101. super.onDestroy();
  102. Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
  103. handler.removeCallbacks(sendData); // Stop sending data
  104. unregisterReceiver(); // Unregister the receiver
  105.  
  106. // Restart the service if it gets destroyed
  107. Intent restartServiceIntent = new Intent(getApplicationContext(), LocationPlugin.class);
  108. startService(restartServiceIntent);
  109. }
  110.  
  111. @Nullable
  112. @Override
  113. public IBinder onBind(Intent intent) {
  114. return null; // Not using binding in this service
  115. }
  116.  
  117. // This method creates a notification channel for the foreground service
  118. private void createNotificationChannel() {
  119. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  120. NotificationChannel serviceChannel = new NotificationChannel(
  121. "LocationPluginChannel",
  122. "Location Plugin Service Channel",
  123. NotificationManager.IMPORTANCE_LOW // Set to low importance
  124. );
  125. // Ensure the notification will not show on the lock screen
  126. serviceChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
  127.  
  128. NotificationManager manager = getSystemService(NotificationManager.class);
  129. if (manager != null) {
  130. manager.createNotificationChannel(serviceChannel);
  131. }
  132. }
  133. }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement