当应用程序在后台时 Android 监听器停止运行

Sal*_*aai 10 android android-bluetooth

我正在开发一个应用程序,该应用程序将检测蓝牙信号(Sensoro 智能信标设备)并打开活动。但我希望应用程序仍然能够检测到信号,即使应用程序处于后台甚至被杀死。我使用了前台服务,当我打开应用程序并在活动之间移动时,它会检测到信号,但是当将应用程序发送到后台并打开其他应用程序时,侦听器会停止,尽管服务仍在工作。我正在打印日志。System.out.println("Sensoro 2" );即使我终止应用程序或打开另一个应用程序,也会继续打印。但是 BeaconManagerListener 中的打印日志不起作用。我尝试使用后台服务,但它也不起作用。您能否告知,当应用程序在后台或被杀死时,是否有一种方法可以使侦听器在服务中工作?这是服务代码:

public class MyService extends Service {
    public static final String CHANNEL_ID = "ForegroundServiceChannel";
    int service_timer=0;
    Timer timer = new Timer();
    SensoroManager sensoroManager;
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        sensoroManager = SensoroManager.getInstance(MyService.this);
        String input = intent.getStringExtra("inputExtra");
        System.out.println("Sensoro 2" );
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1,notification);
        //do heavy work on a background thread
        //stopSelf();
                if (sensoroManager.isBluetoothEnabled()) {
                    sensoroManager.setCloudServiceEnable(true);
                    /**
                     * Enable SDK service
                     **/
                    try {
                        sensoroManager.startService();

                    } catch (Exception e) {
                        e.printStackTrace(); // Fetch abnormal info
                    }
                }
                else
                {
                    Toast.makeText(MyService.this,"Bluetooth off",Toast.LENGTH_LONG).show();
                }
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
           public void run() {
                //your method
                System.out.println("Sensoro 2" );


                BeaconManagerListener beaconManagerListener = new BeaconManagerListener() {

                    @Override
                    public void onUpdateBeacon(ArrayList<Beacon> beacons) {
                        // Refresh sensor info
                        for (Beacon beacon : beacons
                        ) {
                            System.out.println("Sensoro 3" );
                           // System.out.println("Sensoro" +beacon.getAccuracy());
                        }
                    }

                    @Override
                    public void onNewBeacon(Beacon beacon) {

                        if (beacon.getSerialNumber().equals("0117C59B243C")){
                            System.out.println("Sensoro 3" );
                            System.out.println("Sensoro acc" +beacon.getAccuracy());
                }
            }

                    @Override
                    public void onGoneBeacon(Beacon beacon) {

                        if (beacon.getSerialNumber().equals("0117C59B243C")){
                            System.out.println("Sensoro acc gone");
                            System.out.println("Sensoro acc Timer" +service_timer);
                        }
                    }
                };
                sensoroManager.setBeaconManagerListener(beaconManagerListener);
            }
        }, 0, 2000);



        return START_NOT_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
       // timer.cancel();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }


}
Run Code Online (Sandbox Code Playgroud)

这是我称之为的地方:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService();
        }


    public void startService() {
        Intent serviceIntent = new Intent(this, MyService.class);
        serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");
        System.out.println("Sensoro 1 ");
        ContextCompat.startForegroundService(this, serviceIntent);
    }
    public void stopService() {
        Intent serviceIntent = new Intent(this, MyService2.class);
        stopService(serviceIntent);
    }


    public void movingg(View view) {
        Intent intent=new Intent(this,Usermain.class);
       startActivity(intent);    }
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*ani 2

这并不是 100% 与您正在搜索的内容匹配,但它接近您想要的内容。

您甚至可以使用在后台运行的应用程序的WorkManager 。WorkManager 有两种类型。1) 一次性工作请求 2) 定期工作请求。

因此,您可以使用PeriodicWorkRequest,它将每15分钟(最小间隔)调用一次,并且您可以在WorkManager中添加代码来检测蓝牙信号。因此您可以每隔 15 分钟检测一次蓝牙信号。

我希望这能帮助您解决您的问题。