这是我的BattreyInfo.java文件。我正在尝试提取电池温度并将其显示在屏幕上。 Eclipse说BroadcastReciever
无法解析为某种类型。我不明白我已正确导入包。它也无法识别onRecieve
函数。我是否必须为BroadcastReciever
创建另一个课程?
package com.example.batterywidget;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.BatteryManager;
import android.widget.TextView;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.Color;
import android.view.View;
public class BatteryInfo extends Activity {
private TextView BatTemp;
BroadcastReceiver mBatInfoReciever = new BroadcastReceiver(){
@Override
public void onRecieve(Context arg0, Intent intent){
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
BatTemp.setText(String.valueOf(temp) + "%");
}
};
//called when the activity is first created
@Override
public void onCreate(Bundle binfo){
super.onCreate(binfo);
setContentView(R.layout.batteryinfo);
BatTemp = (TextView) this.findViewById(R.id.battery_temp);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBatInfoReceiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mBatInfoReceiver);
}
}
答案 0 :(得分:1)
BroadcastReceiver是一个抽象类。这意味着您无法创建它的实例。您必须创建自己的类并扩展BroadcastReceiver。
我认为您可以将所有现有代码复制到新课程中。
你似乎有3个广播公司。 第一个看起来像这样:
public class BatInfoReceiver extends BroadcastReceiver{
@Override
public void onRecieve(Context arg0, Intent intent){
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
BatTemp.setText(String.valueOf(temp) + "%");
}
}
现在可以创建这个类的实例:
BatInforeceiver mBatInfoReceiver = new BatInfoReceiver();
然后你对其他两个BroadcastReceivers做同样的事情。
答案 1 :(得分:0)
这是我用来获取温度的代码
batteryReceiver = new BroadcastReceiver() {
int level = -1;
int scale = -1;
int temperature = -1;
@Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
int health = intent.getIntExtra("health",BatteryManager.BATTERY_HEALTH_UNKNOWN);
String strHealth;
if (health == BatteryManager.BATTERY_HEALTH_GOOD){
strHealth = "Good";
} else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT){
strHealth = "Over Heat";
} else if (health == BatteryManager.BATTERY_HEALTH_DEAD){
strHealth = "Dead";
} else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){
strHealth = "Over Voltage";
} else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){
strHealth = "Unspecified Failure";
} else{
strHealth = "Unknown";
}
batteryStatusTextView.setText(" Temperature: "+temperature/10+"C" + " " + strHealth);
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryReceiver, filter);
答案 2 :(得分:0)
private void batteryLevel() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,
-1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
batteryLevel.setText("Battery Level Remaining: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(
Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
答案 3 :(得分:0)
BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
int scale = -1;
int level = -1;
int voltage = -1;
int temp = -1;
@Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
}
};