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;

				}

			}
		}

	}


posted @ 2013-06-20 19:39  jlins  阅读(363)  评论(0编辑  收藏  举报