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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qin20082012/article/details/71078129


 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操作。




没有更多推荐了,返回首页