关闭应用程序后,带有广播接收器的Work Manager无法正常工作

时间:2019-06-10 02:12:29

标签: android broadcastreceiver android-bluetooth background-service android-workmanager

我只想保持蓝牙开启,然后听蓝牙状态,如果蓝牙状态为关闭,则广播接收器可以启用它。而且我希望它在应用程序也关闭时运行。因此,即使在应用关闭后(当它不工作时),我仍试图运行蓝牙广播接收 r。为此,我了解到我需要使用工作管理器来支持所有设备。我尝试组合广播接收器和工作管理器。但是当应用程序关闭时,我无法使其运行。

这是我的 MainActivity.java 。在这里,我将工作请求排队。

package com.example.workmanagersample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;

public class MainActivity extends AppCompatActivity {

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

        final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(MyWorker.class).build();
        WorkManager.getInstance().enqueue(workRequest);

    }
}

下面的类是我的 MyWorker.java 。在这里,我注册了接收器。

package com.example.workmanagersample;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import androidx.work.Worker;
import androidx.work.WorkerParameters;


public class MyWorker extends Worker {
    private BlueToothBroadcastReceiver myReceiver;

    public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    /*
     * This method is responsible for doing the work
     * so whatever work that is needed to be performed
     * we will put it here
     *
     * For example, here I am calling the method displayNotification()
     * It will display a notification
     * So that we will understand the work is executed
     * */

    @NonNull
    @Override
    public Result doWork() {
        displayNotification("My Worker", "Hey I finished my work");


        setReceiver();

        return Worker.Result.success();
    }

    /*
     * The method is doing nothing but only generating
     * a simple notification
     * If you are confused about it
     * you should check the Android Notification Tutorial
     * */
    private void displayNotification(String title, String task) {
        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("simplifiedcoding", "simplifiedcoding", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), "simplifiedcoding")
                .setContentTitle(title)
                .setContentText(task)
                .setSmallIcon(R.mipmap.ic_launcher);

        notificationManager.notify(1, notification.build());
    }


    private void setReceiver() {
        myReceiver = new BlueToothBroadcastReceiver();
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        getApplicationContext().registerReceiver(myReceiver, filter);

    }
}

以下类是我的 BlueToothBroadcastReceiver.java :在这里,我侦听蓝牙状态是否已更改,如果蓝牙状态已关闭,则尝试打开它。应用正在运行时,它正在工作。但是我希望它在关闭应用程序但仍然无法实现的情况下也能正常工作。

package com.example.workmanagersample;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BlueToothBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                    BluetoothAdapter.ERROR);
            switch (state) {
                case BluetoothAdapter.STATE_OFF:
                    setBluetooth(true);
                    // Bluetooth has been turned off;
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    setBluetooth(true);
                    // Bluetooth is turning off;
                    break;
                case BluetoothAdapter.STATE_ON:
                    // Bluetooth has been on
                    break;
                case BluetoothAdapter.STATE_DISCONNECTING:
                    setBluetooth(true);
                    // Bluetooth is turning on
                    break;

                case BluetoothAdapter.STATE_DISCONNECTED:
                    setBluetooth(true);
                    // Bluetooth is turning on
                    break;
            }
        }
    }

    public static boolean setBluetooth(boolean enable) {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        boolean isEnabled = bluetoothAdapter.isEnabled();
        if (enable && !isEnabled) {
            return bluetoothAdapter.enable();
        }
        else if(!enable && isEnabled) {
            return bluetoothAdapter.disable();
        }
        // No need to change bluetooth state
        return true;
    }
}

最近是我的清单文件;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.workmanagersample">

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".BlueToothBroadcastReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
                <action android:name="android.bluetooth.adapter.action.STATE_OFF"/>
                <action android:name="android.bluetooth.adapter.action.STATE_TURNING_OFF"/>
                <action android:name="android.bluetooth.adapter.action.STATE_ON"/>
                <action android:name="android.bluetooth.adapter.action.STATE_DISCONNECTING"/>
                <action android:name="android.bluetooth.adapter.action.STATE_DISCONNECTED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

我在周末研究后选择使用工作管理器,但是当我关闭该应用程序后,它不起作用。有什么我想念的东西或有什么限制吗?如果可以,我该如何解决?任何帮助将非常感激!谢谢!

3 个答案:

答案 0 :(得分:0)

您需要在应用程序关闭后让广播接收器保持运行,您将 通过Service类实现这一目标,您可能希望寻找this答案

答案 1 :(得分:0)

WorkManager用于执行任务,即使您的应用程序在后台。您可以为Worker类分配一些约束,以便仅在满足这些约束时才执行(即,如果需要将一些数据上传到服务器,则在WiFi连接上具有约束可用)。

是为什么WorkManager使用广播接收器(最高API级别22)或JobScheduler:知道这些约束何时更改的原因。

正如其他人回答的那样,您需要使用服务(如果需要长时间运行此服务,那么前景服务可能是个好主意)。 您应该评估的几件事:

  1. 首先检查“前景服务”的方案。 Consider this blog post
  2. 然后documentation on foreground services
  3. 最后是Bluetooth overview

答案 2 :(得分:0)

背景

现在谈到您的实施,似乎您有一些提示,因为我看到您有待处理的通知。由于这部分非常接近解决方案。所以实际上你必须使用 Foreground Service(不仅仅是这里提到的 Service,因为它可能被系统终止)。

最后,我相信您有运行此应用程序的用例。因为这对用户来说可能非常耗电,而且您知道自己在做什么。

解决方案

实现 Foreground Service 并从应用程序启动它。确保里面有通知。以及您对 Receiver 订阅的实施。

class LiveBroadcast : Service() {

    private var myReceiver: BlueToothBroadcastReceiver? = null

    override fun onCreate() {

        // Init Forgeround Notification
        val pendingIntent: PendingIntent =
            Intent(this, ExampleActivity::class.java).let { notificationIntent ->
                PendingIntent.getActivity(this, 0, notificationIntent, 0)
            }

        val notification: Notification = Notification.Builder(this, CHANNEL_DEFAULT_IMPORTANCE)
                .setContentTitle(getText(R.string.notification_title))
                .setContentText(getText(R.string.notification_message))
                .setSmallIcon(R.drawable.icon)
                .setContentIntent(pendingIntent)
                .setTicker(getText(R.string.ticker_text))
                .build()

        // Notification ID cannot be 0.
        startForeground(ONGOING_NOTIFICATION_ID, notification)

        // Start your BluetoothReceiver
        myReceiver = new BlueToothBroadcastReceiver()
        getApplicationContext().registerReceiver(
            myReceiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        return START_STICKY
    }

    override fun onBind(intent: Intent): IBinder? {
        // Pass
    }

    override fun onDestroy() {
        // Pass
    }
}