gpt4 book ai didi

Android BroadcastReceiver android.intent.action.BATTERY_CHANGED

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:10:02 31 4
gpt4 key购买 nike

我能够在我的 Activity 中收听 Intent Intent.ACTION_BATTERY_CHANGED 并且效果很好。我尝试在 list 中定义接收器:

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

但它没有用,我在这里读了一篇文章说它必须以编程方式定义。我不想在我的 Activity 中注册接收器,而是想在服务中实现这一点,以便它持续监控。我已经成功地做到了这一点,而且它有效,但我想检查我的工作,看看它是否是正确的方法。

我从扩展 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() 中处理事件。如果级别低于阈值,我的 BroadcastReceiver 将启动服务。

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() 中注册接收者,然后在接收到事件时启动服务。首先,我尝试不使用服务,但后来我无法注册接收者,因为它无法在 list 中实现。其次,当我尝试在 onReceive() 中发送文本消息时抛出异常。我读到 onReceive() 不应该启动任何线程。

提前致谢

最佳答案

是的,这是正确的做法。这与我对我的应用程序所做的非常相似,该应用程序在生产环境中运行良好已有十多年(自 Android 早期以来),而且我看不到您在这里遗漏或出错的任何内容。你是对的,你不能从你的 list 中注册那个广播;您确实需要以编程方式这样做。根据您分享的其他内容,在服务而不是 Activity 中这样做显然是正确的。

关于Android BroadcastReceiver android.intent.action.BATTERY_CHANGED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6508914/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com