나는 배터리에 대한 최신 정보를 얻고 유지하는이 방송 수신기를 가지고 있습니다. 배터리에 대한 자세한 내용은 WiFi를 사용하지 않는 동안 활동을 다시 만들 필요없이 업데이트된다는 의미에서 잘 작동합니다. 예를 들어, WiFi를 사용하지 않으면 기본 설정 요약이 변경되지 않습니다. 왜? 어떻게 해결할 수 있습니까? 이것은 코드입니다.방송 수신기 : 왜 작동하지 않습니까?
IntentFilter filter= new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
this.registerReceiver(this.batteryInfoReceiver,filter);
당신은 또한 당신의 onReceive가 네트워크 경우 호출되는 것 때문에 필터링 연결 변경 작업을 추가 할 수 있습니다
@Override
protected void onStop()
{
unregisterReceiver(batteryInfoReceiver);
super.onStop();
}
@Override
protected void onRestart()
{
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
super.onRestart();
}
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
String technology= intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
int temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)/10;
int voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if (sdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR1) {
boolean wCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS;
if(wCharge) {
ricarica.setSummary("Charging (WiFi)");
}
}
if(usbCharge) {
ricarica.setSummary("Charging (USB)");
}
else if (acCharge){
ricarica.setSummary("Charging (AC)");
}
else {
ricarica.setSummary("Not charging");
}
livelloBatteria.setSummary("Battery at "+level+"%");
livelloVoltaggio.setSummary(voltage+" mV");
livelloTemperatura.setSummary(Integer.toString(temperature)+"° Celsius");
tecnologia.setSummary(technology);
int ip = wifiInfo.getIpAddress();
int speed = wifiInfo.getLinkSpeed();
String speedString = Integer.toString(speed);
String mac = wifiInfo.getMacAddress();
String ssid = wifiInfo.getSSID();
String ipAddress = Formatter.formatIpAddress(ip);
if (wifiMgr.isWifiEnabled()){
if(mac!=null) {
indirizzoMac.setSummary(mac);
}
else {
indirizzoMac.setSummary("Unkown");
}
if(ipAddress!=null) {
indirizzoIp.setSummary(ipAddress);
}
else {
indirizzoIp.setSummary("Unkown");
}
if(ssid!=null) {
indirizzoSsid.setSummary(ssid);
}
else {
indirizzoSsid.setSummary("Unkown");
}
if(speedString!=null) {
velocità.setSummary(speedString+" Mbps");
}
else {
velocità.setSummary("Unkown");
}
}
else {
indirizzoIp.setSummary("WiFi disabled");
indirizzoSsid.setSummary("WiFi disabled");
velocità.setSummary("WIFi disabled");
indirizzoMac.setSummary(mac);
}
}
};
매니페스트를 게시 할 수 있습니까? 연결 이벤트를 캡처하기 위해 의도 필터를 올바르게 설정 했습니까? – SuppressWarnings
Manifest에 정확한 Intent-Filter를 사용하여 BroadcastReceiver를 등록하셨습니까? –