編輯:關於Android編程
當手機電量發生改變時,系統會對外發送Intent的Action為android.intent.action.BATTERY_CHANGED常量的廣播;當手機電量過低時,系統會對外發送Intent的Action為android.intent.action.BATTERY_LOW常量的廣播。
當手機電池從電量不足狀態恢復時,系統會對外發送Intent的Action為android.intent.action.BATTERY_OKAY常量的廣播。
下面通過一個簡單實例來演示:
Receiver:
package com.home.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BATTERY_OKAY.equals(intent.getAction())) {
Toast.makeText(context, "電量已恢復,可以使用!", Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) {
Toast.makeText(context, "電量過低,請盡快充電!", Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
Bundle bundle = intent.getExtras();
// 獲取當前電量
int current = bundle.getInt("level");
// 獲取總電量
int total = bundle.getInt("scale");
StringBuffer sb = new StringBuffer();
sb.append("當前電量為:" + current * 100 / total + "%" + " ");
// 如果當前電量小於總電量的15%
if (current * 1.0 / total < 0.15) {
sb.append("電量過低,請盡快充電!");
} else {
sb.append("電量足夠,請放心使用!");
}
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
}
}
}
package com.home.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BATTERY_OKAY.equals(intent.getAction())) {
Toast.makeText(context, "電量已恢復,可以使用!", Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) {
Toast.makeText(context, "電量過低,請盡快充電!", Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
Bundle bundle = intent.getExtras();
// 獲取當前電量
int current = bundle.getInt("level");
// 獲取總電量
int total = bundle.getInt("scale");
StringBuffer sb = new StringBuffer();
sb.append("當前電量為:" + current * 100 / total + "%" + " ");
// 如果當前電量小於總電量的15%
if (current * 1.0 / total < 0.15) {
sb.append("電量過低,請盡快充電!");
} else {
sb.append("電量足夠,請放心使用!");
}
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
}
}
}
配置Receiver:
receiver android:name="com.home.receiver.BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.BATTERY_LOW"/>
</intent-filter>
</receiver>
<receiver android:name="com.home.receiver.BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.BATTERY_LOW"/>
</intent-filter>
</receiver>
淺談android布局優化的三大標簽
1、布局重用 標簽能夠重用布局文件,簡單的使用如下: ... 1)標簽可以使用單獨的layout屬性,這個也是必須使用的。 2)可
OpenCV實現SfM(三):多目三維重建
注意:本文中的代碼必須使用OpenCV3.0或以上版本進行編譯,因為很多函數是3.0以後才加入的。 問題簡化終於有時間來填坑了,這次一口氣將雙目重建擴展為多目重建吧。首先
Andorid4.x 流氓式屏蔽HOME鍵
應用項目需要要屏蔽HOME鍵。項目本身的要求是讓按下HOME鍵後程序不做任何響應,就像按下返回鍵一樣在onBackPressed 方法中直接return啥都
為你的android App實現自簽名的ssl證書(https)
不願意看啰嗦的可以直接去 實現步驟 最近公司項目用到https的接口形式,對於一般的網絡請求 我們用的是http://****** 使用