首页 > 解决方案 > 如何在后台获取电池电量并公布?

问题描述

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

标签: androidservicebroadcastreceiverforeground-servicebatterylevel

解决方案


您想要实现的事情可以通过意图服务来完成,如果您在文档中挖掘您可以自己找到它,这里是链接Intent Service,这种类型的意图可以在后台触发并可以用于执行各种像您这样的简单操作,因为它们没有任何界面,而只是在后台执行的操作。

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

编辑2:

(在 XML 中不需要任何内容​​,因为这是一个后台进程/操作)这是一个获取电池百分比并在广播接收器中使用文本到语音来宣布它的代码,

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

    }

}

customerclass-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> 

推荐阅读