package com.example.a_wuyafeng;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
int level ;
int scale ;
Button button ;
BroadcastReceiver br = new BroadcastReceiver(){
public void onReceive(android.content.Context context, android.content.Intent intent) {
String s = intent.getAction() ;
if(Intent.ACTION_BATTERY_CHANGED.equals(s)){
level = intent.getIntExtra("level", 0) ;
scale = intent.getIntExtra("scale", 100) ;
hd.sendEmptyMessage(0) ;
Toast.makeText(MainActivity.this, level*100/scale+"",Toast.LENGTH_LONG).show() ;
}
};
};
Handler hd = new Handler(){
public void handleMessage(android.os.Message msg) {
};
} ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main) ;
button = (Button) this.findViewById(R.id.main_btn1) ;
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
registerReceiver(br, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)) ;
}
}) ;
}
}
android获取电量信息
最新推荐文章于 2024-07-07 11:09:08 发布