Android应用开发获取手机电池电量的简单方法


 Intent batteryInfoIntent = getApplicationContext()
                        .registerReceiver( null ,
                                new IntentFilter( Intent.ACTION_BATTERY_CHANGED ) ) ;

                int level = batteryInfoIntent.getIntExtra( "level" , 0 );//电量(0-100)
                int status = batteryInfoIntent.getIntExtra( "status" , 0 );
                int health = batteryInfoIntent.getIntExtra( "health" , 1 );
                boolean present = batteryInfoIntent.getBooleanExtra( "present" , false );
                int scale = batteryInfoIntent.getIntExtra( "scale" , 0 );
                int plugged = batteryInfoIntent.getIntExtra( "plugged" , 0 );//
                int voltage = batteryInfoIntent.getIntExtra( "voltage" , 0 );//电压
                int temperature = batteryInfoIntent.getIntExtra( "temperature" , 0 ); // 温度的单位是10℃
                String technology = batteryInfoIntent.getStringExtra( "technology" );

相当于注册一个空的BroadcastReceiver,可以调用多次,在activity onDestroy时不需要进行unRegister操作。




你可能感兴趣的:(Android)