如何在onCreate中使用有价值的BroadcastReceiver?

时间:2017-11-05 17:04:39

标签: java android broadcastreceiver oncreate android-broadcastreceiver

我想在onCreate()上使用BroadcastReceiver的贵重物品(如VOLTAGE)。

有没有办法做到这一点?

我一直在努力解决这个问题2天。

我不擅长编码,所以我很欣赏任何提示。

我完全错了吗?

在onCreate()中使用BroadcastReceiver的贵重物品是不可能的吗?

然后请告诉我。

我真的需要你的帮助。提前谢谢。

public class MainActivity extends AppCompatActivity {
private TextView batteryInfo;
Long avgCurrent = null, currentNow = null;
int count = 0;
float voltage = 0;
float watt = 0;

MainOpenHelper mainOpenHelper;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainOpenHelper = new MainOpenHelper(this);
    SQLiteDatabase db;
    db = mainOpenHelper.getWritableDatabase();

    Intent i = new Intent(this,MainActivity.class);

    float voltage = getIntent().getIntExtra("voltage",1);
    //db.execSQL("insert into power(voltage, ampere, watt) values ( 1,"+voltage+",1);");

    BatteryManager mBatteryManager = (BatteryManager) getSystemService(Context.BATTERY_SERVICE);
    batteryInfo = (TextView) findViewById(R.id.textViewBatteryInfo);
    this.registerReceiver(this.batteryinfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    batteryinfoReceiver.onReceive().health;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        avgCurrent = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
        currentNow = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
    }

}




private BroadcastReceiver batteryinfoReceiver = new BroadcastReceiver(){
    public void onReceive(Context context, Intent intent){

        int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
        int icon_small = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL,0);
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
        boolean present = intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
        String technology = intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
        float temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
        float voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
        batteryInfo.setText("Health: " + health + "\n" + "Icon small : "+icon_small+"\n" +
                "Level : "+level+"\n"+"Present : "+present+"\n"+"Scale: "+
                scale+"\n"+"Status :"+status+"\n"+ "Technology:"+technology+"\n"+
                "Temperature :"+temp/10+"'C\n"+"Voltage:"+voltage/1000+"V\n"+"BATTERY_PROPERTY_CURRENT_AVERAGE = "
                + avgCurrent + "mAh"+"\n"+"BATTERY_PROPERTY_CURRENT_NOW =  " + currentNow + "mAh"+"\n"+"count ="+count + "\nwatt ="+watt);

        count++;

    }
};

}

0 个答案:

没有答案