Android应用开发之PhoneStateListener 的使用
来源:互联网 发布:seo外链自动群发工具 编辑:程序博客网 时间:2024/05/22 02:03
这两天在做翻转静音的功能,需要用到PhoneStateListener,以前只是知道有这么个东西,没有具体用过
包含此类的包是:android.telephony.PhoneStateListener
由于StatusBar中用到了PhoneStateListener中较多的内容,索性研究了一下StatusBarPolicy.java
services/java/com/android/server/status/StatusBarPolicy.java
1、首先需要通过TelephonyManager来注册要监听的状态,状态定义在类PhoneStateListener中,如下所示
1) LISTEN_CALL_FORWARDING_INDICATOR Listen for changes to the call-forwarding indicator.
Requires Permission: READ_PHONE_STATE
2) LISTEN_CALL_STATE Listen for changes to the device call state.
Requires Permission: READ_PHONE_STATE
3) LISTEN_CELL_LOCATION Listen for changes to the device's cell location. Note that this
will result in frequent callbacks to the listener.
Requires Permission: ACCESS_COARSE_LOCATION
If you need regular location updates but want more control over the update interval or location precision, you can set up a listener through the location manager instead.
4) LISTEN_DATA_ACTIVITY Listen for changes to the direction of data traffic on the data
connection (cellular).
Requires Permission: READ_PHONE_STATE Example: The status bar uses this to display the appropriate data-traffic icon.
5) LISTEN_DATA_CONNECTION_STATE Listen for changes to the data connection state (cellular).
6) LISTEN_MESSAGE_WAITING_INDICATOR Listen for changes to the message-waiting indicator.
Requires Permission: READ_PHONE_STATE
Example: The status bar uses this to determine when to display the voicemail icon.
7) LISTEN_NONE Stop listening for updates.
8) LISTEN_SERVICE_STATE Listen for changes to the network service state (cellular).
9) LISTEN_SIGNAL_STRENGTH This constant is deprecated. by LISTEN_SIGNAL_STRENGTHS
10) LISTEN_SIGNAL_STRENGTHS Listen for changes to the network signal strengths (cellular).
Example: The status bar uses this to control the signal-strength icon
StatusBarPolicy中的注册代码如下所示:
当不需要再继续监听上述状态时,通常需要注销掉
// unregister for phone state notifications.
1) public void onSignalStrengthsChanged(SignalStrength signalStrength)
// Callback invoked when network signal strengths changes.
通过函数updateSignalStrength更新StatusBar上的信号图标
2) public void onServiceStateChanged(ServiceState state)
通过ServiceState返回当前服务状态,有如下四种状态
STATE_EMERGENCY_ONLY The phone is registered and locked. Only emergency numbers are allowed.
STATE_IN_SERVICE Normal operation condition, the phone is registered with an operator either in
home network or in roaming.
STATE_OUT_OF_SERVICE Phone is not registered with any operator, the phone can be currently searching a
new operator to register to, or not searching to registration at all, or registration
is denied, or radio signal is not available.
STATE_POWER_OFF Radio of telephony is explicitly powered off.
同时还可以获取到以下信息
StatusBarPolicy根据上面四种状态封装了如下函数,用来判断当前是否有服务
public static final int CALL_STATE_IDLE
Device call state: No activity.
public static final int CALL_STATE_OFFHOOK
Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
public static final int CALL_STATE_RINGING
Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.
public static final int DATA_CONNECTED
Data connection state: Connected. IP traffic should be available.
public static final int DATA_CONNECTING
Data connection state: Currently setting up a data connection.
public static final int DATA_DISCONNECTED
Data connection state: Disconnected. IP traffic not available.
public static final int DATA_SUSPENDED
Data connection state: Suspended. The connection is up, but IP traffic is temporarily unavailable. For example, in a 2G network, data activity may be suspended when a voice call arrives.
- Android应用开发之PhoneStateListener 的使用
- android-PhoneStateListener
- Android PhoneStateListener
- Android开发应用之照相机的使用
- android PhoneStateListener 内存泄露的问题
- 举例说明如何在android中实现设置黑名单的功能--AudioManager、PhoneStateListener的使用
- android之通过phoneStateListener监听电话状态改变
- android之通过phoneStateListener监听电话状态改变
- android之通过phoneStateListener监听电话状态改变
- android之通过phoneStateListener监听电话状态改变
- android应用开发之ImageView,SeekBar,TableHost,ProgressBar的使用
- Android应用开发之(Gson的使用)
- Android研究院之应用开发线程池的经典使用
- Android研究院之应用开发线程池的经典使用
- Android应用开发技巧之更方便的使用Sqlite
- Android研究院之应用开发线程池的经典使用
- Android研究院之应用开发线程池的经典使用
- Android研究院之应用开发线程池的经典使用
- 第十五周-(项目一)折腾二维数组。
- 蓝桥杯 c++ abc+cba=n
- rt3070 AP驱动移植
- crack the code interview 2.2
- 中科院计算所的ICTCLAS分词包java
- Android应用开发之PhoneStateListener 的使用
- Hadoop 中利用 mapreduce 读写 mysql 数据
- Genesis-3D 入门教程—7.贴图的导入
- 获取当前task的topactivity
- 嵌入式组态王的WinCE工业平板电脑与单片机,三菱FX2N工程案例
- android 蓝牙通信编程
- Android应用开发之ClipboardManager
- What Makes a Good Programmer Good?
- Genesis-3D 入门教程—8.天空盒