Java 如何检测设备是否在后台充电

Java 如何检测设备是否在后台充电,java,android,broadcastreceiver,Java,Android,Broadcastreceiver,我在android的开发者笔记中进行了详细的搜索,无法找到在设备插入时如何执行特定操作,以及在设备拔出时如何执行另一个操作 我已尝试设置如下广播接收器,但它不会运行: <receiver android:name=".PowerConnectionReceiver"> <intent-filter> <action android:name="android.intent.action.A

我在android的开发者笔记中进行了详细的搜索,无法找到在设备插入时如何执行特定操作,以及在设备拔出时如何执行另一个操作

我已尝试设置如下广播接收器,但它不会运行:

    <receiver android:name=".PowerConnectionReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
        </intent-filter>
    </receiver>

据我所知,从API 26及更高版本,您不能再接收清单中注册的某些广播,必须动态注册它们

我需要在后台运行,但不知道如何运行?这篇在2019年更新的文章(在api 26之后)暗示我应该能够做到这一点

充电状态可以像插入设备一样轻松地改变,因此监控充电状态的变化并相应地改变刷新率非常重要

每当设备连接或断开电源时,BatteryManager就会广播一个操作。即使你的应用程序没有运行,接收这些事件也很重要,特别是因为这些事件会影响你启动应用程序的频率,以便启动后台更新,因此你应该在清单中注册一个BroadcastReceiver,通过定义ACTION\u POWER\u CONNECTED和ACTION\u POWER\u DISCONNECTED来监听这两个事件在意向过滤器内。

我的最终目标是在设备插入或拔出时呼叫广播接收器


如何实现此功能?

您需要从代码注册此接收器

您可以运行WorkManager,使其每15分钟运行一次(至少每15分钟运行一次) 注册接收器,检查它是否正在充电
取消注册接收器

实现这一点的最佳方法是在后台运行服务,这样您就可以在用户不使用应用程序的情况下从android接收广播

首先在清单文件中注册服务

<Service android:name=".serviceName"/>

你需要从Code注册这个接收器,但这不允许我从后台获得目的。我的问题是,应用程序需要在充电时打开请勿打扰,tasker似乎能够有效地做到这一点,我不知道他们如何管理这篇文章。这篇文章还暗示你可以在后台做,这不是真的吗?tasker被定义为一个系统应用程序?
    public class BackgroundService extends Service {
        private static final int NOTIF_ID = 1;
        private static final String NOTIF_CHANNEL_ID = "AppNameBackgroundService";

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId){

            //Add broadcast receiver for plugging in and out here
            ChargeDetection chargeDetector = new ChargeDetection(); //This is the name of the class at the bottom of this code.

            IntentFilter filter = new IntentFilter();
            filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
            filter.addAction(Intent.ACTION_POWER_CONNECTED);
            filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
            this.registerReceiver(chargeDetector, filter);

            startForeground();

            return super.onStartCommand(intent, flags, startId);
        }

        private void startForeground() {
            createNotificationChannel();
            Intent notificationIntent = new Intent(this, MainActivity.class);

            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);

            startForeground(NOTIF_ID, new NotificationCompat.Builder(this,
                    NOTIF_CHANNEL_ID) // don't forget create a notification channel first
                    .setOngoing(true)
                    .setSmallIcon(R.mipmap.final_icon)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText("Background service is running")
                    .setContentIntent(pendingIntent)
                    .build());
        }

        private void createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel serviceChannel = new NotificationChannel(
                        NOTIF_CHANNEL_ID,
                        "Foreground Service Channel",
                        NotificationManager.IMPORTANCE_DEFAULT
                );
                NotificationManager manager = getSystemService(NotificationManager.class);
                manager.createNotificationChannel(serviceChannel);
            }
        }
    }

    class ChargeDetection extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Now check if user is charging here. This will only run when device is either plugged in or unplugged.
        }
    }   
    }