Android的广播接收器android.intent.action.BATTERY_CHANGED [英] Android BroadcastReceiver android.intent.action.BATTERY_CHANGED

查看:688
本文介绍了Android的广播接收器android.intent.action.BATTERY_CHANGED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够监听意图 Intent.ACTION_BATTERY_CHANGED 我的活动范围内,它的伟大工程。我试图在清单中定义接收器:

I am able to listen for the Intent Intent.ACTION_BATTERY_CHANGED within my activity and it works great. I tried defining the receiver in the manifest:

<receiver android:name="com.beargreaves.battery.BatteryReceiver"> 
    <intent-filter> 
        <action android:name="android.intent.action.BATTERY_CHANGED" />
    </intent-filter> 
</receiver>

但它没有工作,我读了在这里说,它必须以编程定义一个职位。而不是在我的活动接收器注册,我想在一个服务实现这一目标,使其持续监控。我已经成功地实现了这一点,它的工作原理,但我想检查我的工作,看它是否是正确的做法。

but it didn't work, I read a post on here saying that it must be defined programmatically. Rather than registering for the receiver in my Activity, I wanted to achieve this in a Service so that it continually monitors. I have successfully achieved this, and it works, but I wanted to check my working to see if it is the correct approach.

我开始通过延长广播接收器:

I started by extending BroadcastReceiver:

public class BatteryReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if(null == bundle)
            return;

        boolean isPresent = intent.getBooleanExtra("present", false);
        String technology = intent.getStringExtra("technology");
        int plugged = intent.getIntExtra("plugged", -1);
        int scale = intent.getIntExtra("scale", -1);
        int health = intent.getIntExtra("health", 0);
        int status = intent.getIntExtra("status", 0);
        int rawlevel = intent.getIntExtra("level", -1);
        int level = 0;

        Log.d("Debug","Battery Receiver OnReceive");

        if(isPresent) {
            if (rawlevel >= 0 && scale > 0) {
                level = (rawlevel * 100) / scale;

                Log.d("Debug","BatterReceiver: " + level);

                Toast.makeText(context,"Battery Receiver: " + level + "\t" + status + "Raw: " + rawlevel,Toast.LENGTH_LONG).show();

                if(level <60) {
                    /*
                     * Only invoke the service when level below threshold
                     */
                    Intent i = new Intent(context, BatteryService.class);
                    i.putExtra("level", level);
                    context.startService(i);
                }
            }
        }
    }
}

然后我用一个服务首先注册在的onCreate()接收器,然后处理在onStart事件()。我的广播接收器开始服务,如果电平低于一个阈值

And then I use a Service to first register the receiver in onCreate() and then handle the events in onStart(). My BroadcastReceiver starts the Service if the level is below a threshold.

public class BatteryService extends Service {
    /*
     * First Call to onStart we don't want to do anything
     */
    boolean avoidFirst = false;
    private BroadcastReceiver receiver;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        receiver = new BatteryReceiver();
        registerReceiver(receiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.d("Debug","Battery Service On Start");
        int level = intent.getIntExtra("level", -1);

        if(avoidFirst) {
            if(level != -1) {
                Log.d("Debug","Battery Alert Notifying..... " + level);
                SMSManager.sendSMS(BatteryService.this, "<number redacted>", "Battery Level Aleart: " + level);
                stopSelf();
            }
        } else {
            avoidFirst = true;
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        PreferenceUtil.updatePreference(BatteryService.this, "battery_monitor_on", false);
        unregisterReceiver(receiver);
    }
}

这是正确的做法?是寄存器的onCreate()接收器,然后当接收到事件启动该服务。首先,我试图不使用服务,但后来我都没有注册接收器,因为它可以在清单中不是可以实现的方式。其次,当我试图在发送短信时抛出异常的onReceive()。我读的onReceive()不应该开始任何线程。

Is this the correct approach? Be that register the receiver in onCreate() and then start the Service when an event is received. First I tried not using a Service but then I have no way of registering the receiver since it can't be achieved in the manifest. Secondly an exception was thrown when I tried to send a text message in the onReceive(). I read that onReceive() shouldn't be starting any threads.

在此先感谢

推荐答案

是的,这看起来像一个非常不错的办法。这是相当类似的(实际上有所好转,我会说)比我有我的应用程序,(已多年运作良好)做的。

Yes, that looks like a very good approach. It's quite similar (actually somewhat better, I'd say) than what I do with my app, (which has been working well for years).

这篇关于Android的广播接收器android.intent.action.BATTERY_CHANGED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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