简体   繁体   English

从Activity调用BroadcastReceiver

[英]calling BroadcastReceiver from Activity

I am newbie in android programming; 我是android编程的新手; sorry if my question is easy :) I'm trying to write code that monitors the battery level on the phone and if it is, lower some level for example (%15), create a message that asks user to plug the charger. 抱歉,如果我的问题很简单:)我正在尝试编写代码以监视手机上的电池电量,如果是,则降低电池电量(例如,%15),创建一条消息,要求用户插入充电器。 I know that I need to use BroadcastReceiver class and I want to use it in my MainActivity class. 我知道我需要使用BroadcastReceiver类,并且要在MainActivity类中使用它。 Here is the code I have: 这是我的代码:

public class MainActivity extends Activity{
BroadcastReceiver br = new BroadcastReceiver() {
    @Override
    public void onReceive(final Context context, Intent intent) {
        String intentAction = intent.getAction();
        Log.d("receiver", intentAction);
        int level = intent.getIntExtra("level", 0);
        if (level < 15){
            Log.d("receiver", "battery level low");
        }

        if (Intent.ACTION_BATTERY_OKAY.equalsIgnoreCase(intentAction)) {
            Log.d("receiver", "battery level okay");
        }
    }
};
......

but it seems that the onReceive method is never called since I never see the Log.d("receiver", intentAction) message on Android Studio debug window. 但是似乎从未调用过onReceive方法,因为我在Android Studio调试窗口上从未看到Log.d("receiver", intentAction)消息。 I also have registered br in onResume and unregistered it in onPause : 我也在onResume注册了br ,并在onPause取消了注册:

public void onResume() {
    super.onResume();
    filter.addAction("receiver");
    registerReceiver(br, filter);
}

public void onPause() {
    super.onPause();
    unregisterReceiver(br);
}

But still I am not getting any message. 但是我仍然没有收到任何消息。 Can anybody please help me? 有人可以帮我吗? Should I also add something to AndroidManifest.xml ? 我还应该在AndroidManifest.xml添加一些内容吗?

Your code in onResume() is wrong. 您在onResume()代码是错误的。 You will have to update it as follows. 您将必须按以下方式对其进行更新。

    filter.addAction(Intent.ACTION_BATTERY_LOW);
    filter.addAction(Intent.ACTION_BATTERY_OKAY);
    registerReceiver(br, filter);

to include the ACTION_BATTERY_LOW and ACTION_BATTERY_OKAY filters as mentioned in the docs . 包含docs中提到的ACTION_BATTERY_LOWACTION_BATTERY_OKAY过滤器。

If you dont want to use BroadcastReceiver simply dont use it. 如果您不想使用BroadcastReceiver,那就不要使用它。 Battery intent is sticky intent so you can check it without need of BroadcastReceiver and i also dont think its good idea to put receiver in activity. 电池意图是粘性意图,因此您可以在不需要BroadcastReceiver的情况下进行检查,而且我也不认为将接收器置于活动状态是个好主意。 You can check battery stuff in your activity like this and you dont need to edit your manifest 您可以像这样检查活动中的电池内容,而无需编辑清单

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, filter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
if(batteryPct < 15){
    //do your stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM