安卓:ACTION_BATTERY_LOW不是在仿真器触发。接收器在code注册,没有表现 [英] Android: ACTION_BATTERY_LOW not triggered in emulator. Receiver registered in code, not manifest

查看:202
本文介绍了安卓:ACTION_BATTERY_LOW不是在仿真器触发。接收器在code注册,没有表现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到其中提及的帖子 registerReceiver 已被称为(清单中没有定义),以获得 ACTION_BATTERY_LOW 意图。

I have seen posts where it was mentioned registerReceiver has to be called (not defined in manifest) to receive ACTION_BATTERY_LOW intent.

public class MainActivity extends Activity
{

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       //....  
      registerReceiver(new BatteryLevelReceiver(), new IntentFilter(
                                              Intent.ACTION_BATTERY_LOW));
   }
   // .......
}

的BroadcastReceiver

public class BatteryLevelReceiver extends BroadcastReceiver
{
    private static final String TAG = BatteryLevelReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d(TAG, "onReceive");
    }
}

我没有看到的logcat了的onReceive日志声明。我使用模拟器来模拟电池低的状态,使用的telnet 5554 并执行电源容量10 。我看到电池状态变化的仿真器,但无意触发的。

I do not see the "onReceive" log statement in logcat. I am using emulator to simulate battery low state, using telnet 5554 and executing power capacity 10. I do see the battery status changing in the emulator but no intent triggered.

另外,如果我必须调用 registerReceiver()的活动内,我不叫 unregisterReceiver 在<$ C C>的onStop 或的onDestroy ,是它好吗?如果不行,我怎么会注册一个接收器接收系统的意图,即使我的应用程序是不是在前台? (除了使用清单)。

Also if I have to call registerReceiver() inside an activity and I do not call unregisterReceiver on onStop or onDestroy, is it okay? If not okay, how will I register for a receiver to receive system intents even when my app is not in foreground? (Apart from using manifest).

推荐答案

您可以在您的清单中添加以下code。

You can add the following code in your manifest.

<receiver android:name=".yourpackage.BroadCastNotifier" >
  <intent-filter>
    <action android:name="android.intent.action.BATTERY_LOW" />
  </intent-filter>
</receiver>

在你的 BroadCastNotifier类

package yourpackage;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BroadCastNotifier extends BroadcastReceiver {
    private final static String TAG = "BroadCastNotifier"; 
    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();

        if(Intent.ACTION_BATTERY_LOW.equalsIgnoreCase(intentAction)){
            Log.e(TAG, "GOT LOW BATTERY WARNING");          
        }
    }

}

您将收到的日志信息 GOT低电量警告时,电池电量不足时,也尝试在你的设备。

You will recieve the log message GOT LOW BATTERY WARNING when your battery is low, also try this in your device.

虽然上面的code可能工作不使用广播对这种行动的最好方式,可以监控电池水平的此处描述。你可以决定你的电池与code以下是比较容易的方式。

Although the above code might work the best way is not to use BroadCast for such actions, you can monitor the battery level as described here. You can determine your battery with the code below which is way easier.

int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPct = level / (float)scale; 

这篇关于安卓:ACTION_BATTERY_LOW不是在仿真器触发。接收器在code注册,没有表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆