• Android中吐司当前电池电量


    /**
     * 
     * @author chrp
     *
     *土司当当电池电量
     */
    public class MainActivity extends Activity {
    
    	class BtteryReceiver extends BroadcastReceiver {
    
    		@Override
    		public void onReceive(Context context, Intent intent) {
    			// TODO Auto-generated method stub
    			System.out.println("+++++++++++++++++++++++");
    			// 判断它是否是为电量变化的Broadcast Action
    			if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
    				// 获取当前电量
    				int level = intent.getIntExtra("level", 0);
    				// 电量的总刻度
    				int scale = intent.getIntExtra("scale", 0);
    
    				// 把它转换成百分比
    				String content = ("电池电量为" + ((level * 100) / scale) + "%");
    
    				int key = ((level * 100) / scale);
    				switch (key) {
    				case 100:
    					Toast.makeText(context, "电池已经充满", Toast.LENGTH_LONG).show();
    					break;
    				case 50:
    					Toast.makeText(context, "电池已经使用一半", Toast.LENGTH_LONG)
    							.show();
    					break;
    				case 30:
    					Toast.makeText(context, "电池已到30%了", Toast.LENGTH_LONG)
    							.show();
    					break;
    				case 40:
    					Toast.makeText(context, "电池已到31%了", Toast.LENGTH_LONG)
    							.show();
    					break;
    				case 10:
    					Toast.makeText(context, "电池不足10%", Toast.LENGTH_LONG)
    							.show();
    					break;
    				case 9:
    					Toast.makeText(context, "电池不足9%", Toast.LENGTH_LONG).show();
    					break;
    				case 5:
    					Toast.makeText(context, "电池不足5%", Toast.LENGTH_LONG).show();
    					break;
    
    				}
    
    			}
    		}
    
    	}
    


  • 相关阅读:
    华为机考--约瑟夫问题
    华为机考--字符串压缩
    2014华为校招机考模拟--求最大递增数
    2014华为校招机考模拟--记票统计
    插入排序与归并排序的C#实现
    步入C#--hello world
    shell 命令用法
    About compiling some source code on centos
    ubuntu Install packages
    ssh命令
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3146953.html
Copyright © 2020-2023  润新知