如何在后台获取电池电量并公布?

How to get the battery level in background and announce it?

在我的应用程序中,我想在后台获取电池电量,因为我想在电池电量低或电池充满或处于任何电量时在文本到语音中宣布它。我已经使用了广播接收器并且可以获取电池电量但不知道如何在后台获取它。 有人可以帮忙吗?

你想实现的东西可以通过intent Service来完成,你自己去文档里找找,这里是link Intent Service,这种intent可以在后台触发,可用于执行各种简单的操作,例如您的操作,因为它们没有任何界面,而只是在后台执行的操作。

这里还有一个视频指南Background Services,您可以自己使用它来获取电池百分比并在特定条件后公布

编辑 2:

(XML 中不需要任何内容​​,因为这是背景 process/operation) 这是一个获取电池百分比的代码,并在广播接收器中使用 Text to speech 宣布它,

MainActivity.java

package com.example.text_to_speech;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        Intent intent=new Intent(this,myBackgroundProcess.class);
        intent.setAction("BackgroundProcess");
        PendingIntent pendingIntent=PendingIntent.getBroadcast(this,0,intent,0);
        AlarmManager alarmManger= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManger.setRepeating(AlarmManager.RTC_WAKEUP,0,10,pendingIntent);//change this time based on your liking which will fire the intent

    }

}

客户类- myBackgroundProcess.java

package com.example.text_to_speech;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.speech.tts.TextToSpeech;
import android.util.Log;

import java.util.Locale;

import static android.content.Context.BATTERY_SERVICE;

public class myBackgroundProcess extends BroadcastReceiver {
    private TextToSpeech mTTS;

    @Override
    public void onReceive(Context context, Intent intent) {
        mTTS = new TextToSpeech(context.getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    int result = mTTS.setLanguage(Locale.US);

                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "Language not supported");
                        
                    } else {

                        BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
                        int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
                        if(batLevel==100 || batLevel<=10 || batLevel==50)
                        speak(context,batLevel);

                    }
                } else {
                    Log.e("TTS", "Initialization failed");
                    
                }
            }
        });

    }
    public void speak(Context context, int batlevel)
    {
        mTTS.setPitch(10);
        mTTS.setSpeechRate(1);
        String text=String.valueOf(batlevel);
        mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

}

在Android Manifest 中注册接收器并向其添加意图过滤器,如下所示(在应用程序标签下方)

<receiver android:name=".myBackgroundProcess"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>
                <action android:name="BackgroundProcess"/>
            </intent-filter>
        </receiver>