聪明出于勤奋,天才在于积累

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::

http://developer.android.com/training/monitoring-device-state/index.html
http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

0. stickey 广播

系统会发一些 "sticky" 的广播,send出来后会一直保留着,当调用 registerReceiver 方法的 IntentFilter 符合其要求时,
就会返回这个广播,就像新广播的一样。

这时调用registerReceiver 传入的 receiver 可为null, 直接返回广播的Intent。如下面获取电池状态的方法。

若没有,返回null,有多个 "sticky"广播符合 IntentFilter 时,由系统决定返回哪一个。

 

只能动态代码注册接收的广播:

Intent.ACTION_BATTERY_CHANGED

 

可以静态注册注册接收的广播:

android.intent.action.ACTION_POWER_CONNECTED

android.intent.action.ACTION_POWER_DISCONNECTED

android.intent.action.ACTION_BATTERY_LOW

android.intent.action.ACTION_BATTERY_OKAY

 

1. 监听充电状态,电池状态

一般在AC充电器充电的时候,可以加快后台的更新速率,USB充电的时候要减小,用电池的时候要更低。

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = mContext.registerReceiver(null, ifilter);

// Are we charging / charged? 是否正在充电,是否充满
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;

// How are we charging? 用什么线充电,USB还是AC线
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

//可返回这些信息,有电量,电池类型等等: 
// [icon-small=17302814, scale=100, present=true, technology=Li-poly, level=97, 
// voltage=4206, status=2, invalid_charger=0, plugged=2, health=2, temperature=310]
//当前剩余百分之多少的电量: int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float)scale;

 PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
 SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
 SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
 FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
 ACQUIRE_CAUSES_WAKEUP:强制使屏幕亮起,这种锁主要针对一些必须通知用户的操作.
 ON_AFTER_RELEASE:当锁被释放时,保持屏幕亮起一段时间

 

 

 

 

用静态广播的方法:

Receiver接受到的Intent同上所述。

也可以用静态注册的广播来监听充电状态:
<receiver android:name=".PowerConnectionReceiver">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
  </intent-filter>
</receiver>

监听电量变低或者充满的广播:
<receiver android:name=".BatteryLevelReceiver">
<intent-filter>
  <action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
  <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
  </intent-filter>
</receiver>

 

2. Determining and Monitoring the Docking State and Type

//Intent.ACTION_DOCK_EVENT也是个 "sticky" 的广播。
IntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
Intent dockStatus = context.registerReceiver(null, ifilter);

int dockState = dockStatus.getIntExtra(Intent.EXTRA_DOCK_STATE, -1);
boolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;


boolean isCar = dockState == Intent.EXTRA_DOCK_STATE_CAR;
boolean isDesk = dockState == Intent.EXTRA_DOCK_STATE_DESK || 
                 dockState == Intent.EXTRA_DOCK_STATE_LE_DESK ||
                 dockState == Intent.EXTRA_DOCK_STATE_HE_DESK;

.Car
.Desk
.Low-End (Analog) Desk
.High-End (Digital) Desk
Note that the latter two options were only introduced to Android in API level 11

 

权限:

<action android:name="android.intent.action.ACTION_DOCK_EVENT"/>

 

3. 监听和获取网络连接的状态。

如果网络不通就没必要在后台去访问网络。

ConnectivityManager cm =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
 
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;

//监听网络变化,这个广播会触发得比较频繁,从net->wifi 或者相反都会触发。
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>

 

4. 动态的开启或者关闭 广播接收器

可以根据需要来设置receiver 是否要工作,这种方式设置后,即使重启手机了也是有效的。

ComponentName receiver = new ComponentName(context, myReceiver.class);
PackageManager pm = context.getPackageManager();

pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

 

/方法原型
public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags);

newState The
new enabled state for the component. The legal values for this state are: COMPONENT_ENABLED_STATE_ENABLED, COMPONENT_ENABLED_STATE_DISABLED, COMPONENT_ENABLED_STATE_DEFAULT The last one removes the setting, thereby restoring the component's state to whatever was set in it's manifest (or enabled, by default). flags Optional behavior flags: DONT_KILL_APP or 0. DONT_KILL_APP to indicate that you don't want to kill the app containing the component. Be careful when you set this since changing component states can make the containing application's behavior unpredictable.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2014-03-27 15:26    阅读(308)  评论(0编辑  收藏  举报