步骤:
- 创建CallStateService继承Service
- 取得电话服务
- 监听电话动作
- 的对象
- 没有电话时
- 电话响铃时
- 电话接通时
- 的行为
- 启动service
- 注册service并添加监听电话状态的权限
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.callstatelistener"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<!-- 监听电话状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.callstatelistener.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注册service -->
<service android:name="com.example.callstatelistener.CallStateService"></service>
</application>
</manifest>
CallStateService.java
package com.example.callstatelistener;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
* 监听电话状态service
*
* @author zhaoyazhi
*
* 2014-6-17
*/
public class CallStateService extends Service {
public static final String TAG = "aaa";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
// 取得电话服务
TelephonyManager telManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
// 的对象,的行为
telManager.listen(new MyPhoneStateListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}
class MyPhoneStateListener extends PhoneStateListener {
/**
* int state电话状态。
* String incomingNumber电话号码
*/
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: /* 无任何状态时 */
// 实现黑名单操作
Log.i(TAG, "------------------没有电话来");
break;
case TelephonyManager.CALL_STATE_RINGING: /* 电话进来时 */
Log.i(TAG, "------------------来电话了---并且响铃");
break;
case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */
Log.i(TAG, "------------------通话中");
break;
default:
break;
}
};
}
}
MainActivity.java
package com.example.callstatelistener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//intent 意图
Intent service = new Intent(MainActivity.this, CallStateService.class);
//启动service服务
this.startService(service);
}
}
控制台输出:
分享到:
相关推荐
VC_MFC_编程实例1VC_MFC_编程实例1.pdf.pdf
Visual_C++_6[1].0_编程实例与技巧Visual_C++_6[1].0_编程实例与技巧Visual_C++_6[1].0_编程实例与技巧Visual_C++_6[1].0_编程实例与技巧
STM32F105_OBDII_实例 STM32F105_OBDII_实例 STM32F105_OBDII_实例 STM32F105_OBDII_实例 STM32F105_OBDII_实例 STM32F105_OBDII_实例 STM32F105_OBDII_实例 STM32F105_OBDII_实例 STM32F105_OBDII_实例 STM32F105_...
MTK_MMI实例培训教程1-5.docMTK_MMI实例培训教程1-5.docMTK_MMI实例培训教程1-5.docMTK_MMI实例培训教程1-5.doc
基于_Tomcat5.0和_Axis2开发Web_Service应用实例
外部(纯)担保交易接口__实例外部(纯)担保交易接口__实例外部(纯)担保交易接口__实例外部(纯)担保交易接口__实例外部(纯)担保交易接口__实例外部(纯)担保交易接口__实例外部(纯)担保交易接口__实例外部...
apache_tomcat_整合实例apache_tomcat_整合实例. apache_tomcat_整合实例apache_tomcat_整合实例apache_tomcat_整合实例.
Windows__API_实例精解.Windows__API_实例精解.Windows__API_实例精解.Windows__API_实例精解.Windows__API_实例精解.Windows__API_实例精解.
CATIA V5运动和力学分析实例教程
wincc7.3实例,新手上手宝典,基本功能画面都有
java_Timer定时器实例_Spring实例
3ds Max全面攻克_全面实例精解_CD43ds Max全面攻克_全面实例精解_CD43ds Max全面攻克_全面实例精解_CD43ds Max全面攻克_全面实例精解_CD43ds Max全面攻克_全面实例精解_CD43ds Max全面攻克_全面实例精解_CD43ds Max...
oracl_连接实例.txt oracl_连接实例.txt oracl_连接实例.txt oracl_连接实例.txt
opengl的实例。包括说明,源程序,demo等。
struts2_入门实例struts2_入门实例struts2_入门实例struts2_入门实例struts2_入门实例
vb功能实例介绍。详细、很好的实例说明。
大量labview的经典实例:包含了很多labview的例子,可供参考
vhdl 实例 通过实例学习vhdl 编程
可以把dao层的id等内容实例化至数据库
verilog代码实例 基础到进阶都有