2

这是我的 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);
    }

}
4

4 回答 4

2

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();

然后你对你的另外两个广播接收器做同样的事情。

于 2013-11-13T13:32:58.410 回答
1
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);
    }
于 2013-11-13T13:24:09.167 回答
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);
于 2013-11-13T13:20:40.520 回答
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);
    }
};
于 2013-11-13T13:26:04.290 回答