Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.your.example;
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.Service;
- import android.content.Intent;
- import android.content.IntentFilter; // Add this import
- import android.net.Uri;
- import android.os.Build;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.PowerManager;
- import android.provider.Settings;
- import android.util.Log;
- import android.widget.Toast;
- import androidx.annotation.Nullable;
- import androidx.core.app.NotificationCompat;
- public class LocationPlugin extends Service {
- public static int number = 0;
- private final Handler handler = new Handler();
- private ServiceMessageReceiver receiver; // Declare the receiver
- private Runnable sendData = new Runnable() {
- public void run() {
- number++;
- send(number);
- Log.d("LocationPlugin", "number is: " + number);
- // Show number on screen
- Toast.makeText(LocationPlugin.this, "Current number: " + number, Toast.LENGTH_SHORT).show();
- handler.postDelayed(this, 5000); // Sends every 5 seconds
- }
- };
- public void send(int num) {
- Log.d("LocationPlugin", "Sending number: " + num);
- Intent sendIntent = new Intent();
- sendIntent.setAction("com.your.example.MESSAGE"); // Ensure this matches the action registered in Unity
- sendIntent.putExtra("message", num); // Ensure the key matches the one used in Unity
- sendBroadcast(sendIntent);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.d("LocationPlugin", "Service started.");
- createNotificationChannel();
- Notification notification = new NotificationCompat.Builder(this, "LocationPluginChannel")
- .setContentTitle("Exampple Service")
- .setContentText("Running...") // Minimal notification text
- .setSmallIcon(android.R.drawable.ic_dialog_info)
- .setOngoing(true) // Keeps the notification ongoing
- .setVisibility(NotificationCompat.VISIBILITY_SECRET) // Hidden from lock screen
- .setPriority(NotificationCompat.PRIORITY_LOW) // Low priority
- .build();
- startForeground(1, notification); // Start the service in the foreground
- handler.postDelayed(sendData, 1000); // Start sending data after 1 second
- // Register the broadcast receiver
- registerReceiver();
- // Prompt user to disable battery optimizations for your app
- checkBatteryOptimization();
- return START_REDELIVER_INTENT; // Ensure the last Intent is redelivered
- }
- private void registerReceiver() {
- receiver = new ServiceMessageReceiver(); // Initialize the receiver
- IntentFilter filter = new IntentFilter("com.your.example.MESSAGE"); // Ensure this matches
- registerReceiver(receiver, filter); // Register the receiver
- }
- private void unregisterReceiver() {
- if (receiver != null) {
- unregisterReceiver(receiver); // Unregister the receiver
- receiver = null; // Clear the reference
- }
- }
- private void checkBatteryOptimization() {
- Intent intent = new Intent();
- String packageName = getPackageName();
- PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
- if (pm != null && !pm.isIgnoringBatteryOptimizations(packageName)) {
- intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
- intent.setData(Uri.parse("package:" + packageName));
- // Add FLAG_ACTIVITY_NEW_TASK to the intent
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent);
- }
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
- handler.removeCallbacks(sendData); // Stop sending data
- unregisterReceiver(); // Unregister the receiver
- // Restart the service if it gets destroyed
- Intent restartServiceIntent = new Intent(getApplicationContext(), LocationPlugin.class);
- startService(restartServiceIntent);
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null; // Not using binding in this service
- }
- // This method creates a notification channel for the foreground service
- private void createNotificationChannel() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- NotificationChannel serviceChannel = new NotificationChannel(
- "LocationPluginChannel",
- "Location Plugin Service Channel",
- NotificationManager.IMPORTANCE_LOW // Set to low importance
- );
- // Ensure the notification will not show on the lock screen
- serviceChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
- NotificationManager manager = getSystemService(NotificationManager.class);
- if (manager != null) {
- manager.createNotificationChannel(serviceChannel);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement