Android 我怎样才能得到;电池“U属性”“U电流”“U现在”;工作?

Android 我怎样才能得到;电池“U属性”“U电流”“U现在”;工作?,android,batterymanager,Android,Batterymanager,我正在尝试制作一个应用程序,它将显示我的一个项目的电池状态和电池信息。我想在应用程序中显示瞬时电流和电源,但我一直没有成功使其工作,我没有得到当前信息和电源信息,它只是保持在2和交流电源插头。如果你能帮我写代码,我将不胜感激。谢谢 import android.annotation.SuppressLint; //import com.batterystatus.namespace.R; import android.content.BroadcastReceiver; import and

我正在尝试制作一个应用程序,它将显示我的一个项目的电池状态和电池信息。我想在应用程序中显示瞬时电流和电源,但我一直没有成功使其工作,我没有得到当前信息和电源信息,它只是保持在2和交流电源插头。如果你能帮我写代码,我将不胜感激。谢谢

import android.annotation.SuppressLint;

//import com.batterystatus.namespace.R;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
private TextView level,voltage, status1,temp,health1,tech,sour,amp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        level=(TextView)findViewById(R.id.level);
        voltage=(TextView)findViewById(R.id.volt);
        status1=(TextView)findViewById(R.id.stat);
        temp=(TextView)findViewById(R.id.temp);
        health1=(TextView)findViewById(R.id.healt);
        tech=(TextView)findViewById(R.id.tech);
        sour=(TextView)findViewById(R.id.source);
        Button b = (Button) findViewById(R.id.ex);
        amp=(TextView)findViewById(R.id.current);
        b.setOnClickListener(new View.OnClickListener(){
             public void onClick(View v) {
                    // TODO Auto-generated method stub
                    finish();
                    System.exit(0);}
        });



        this.registerReceiver(this.myBatteryReceiver,
                 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));


    }

    private BroadcastReceiver myBatteryReceiver
       = new BroadcastReceiver(){

     @SuppressLint("InlinedApi")
    @Override
     public void onReceive(Context arg0, Intent arg1) {
      // TODO Auto-generated method stub

      if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){

          int lv = arg1.getIntExtra("level", 0);
       level.setText("Level: "
         + String.valueOf(lv) + "%");

       voltage.setText("Voltage: "
                 + String.valueOf((float)arg1.getIntExtra("voltage", 0)/1000) + "V");
               temp.setText("Temperature: "
                 + String.valueOf((float)arg1.getIntExtra("temperature", 0)/10) + "c");
               tech.setText("Technology: " + arg1.getStringExtra("technology"));

               int status = arg1.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
               String strStatus;
               if (status == BatteryManager.BATTERY_STATUS_CHARGING){
                strStatus = "Charging";
               } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING){
                strStatus = "Dis-charging";
               } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
                strStatus = "Not charging";
               } else if (status == BatteryManager.BATTERY_STATUS_FULL){
                strStatus = "Full";
               } else {
                strStatus = "Unknown";
               }
               status1.setText("Status: " + strStatus);

               int source=arg1.getIntExtra("source", BatteryManager.BATTERY_STATUS_UNKNOWN);
               **int current=arg1.getIntExtra("current", BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
               amp.setText("Current: "+String.valueOf(current));
               String strsource = null;

            if(source==BatteryManager.BATTERY_PLUGGED_AC){strsource="AC Power Plugged";}
              else if (source==BatteryManager.BATTERY_PLUGGED_USB){strsource="USB Plugged";}
              sour.setText("Power Source: "+ strsource);**
               int health = arg1.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";
               }
               health1.setText("Health: " + strHealth);

              }
             }

               };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

只需执行以下操作,而不是注册
操作\u电池\u更改的
广播:

电池管理器mBatteryManager=
(BatteryManager)Context.getSystemService(Context.BATTERY_服务);

然后使用
mBatteryManager
获取您想要的任何信息。如果您希望在某个时间间隔使用它,
AlarmManager
可能很有用

值得注意的是,并非所有手机都能提供瞬时功率,或其他一些您可能需要的指标。据我所知,Nexus6手机是唯一能提供瞬时供电的手机。这里有一个很好的参考资料,可以更好地帮助你