【android】电话接听状态事件

androidmanifest权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


首先获取TelephonyManager实例。
其和获取其他系统服务一样采用getSystemService方法获得。
        TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);


然后使用telephonyManager监听事件。采用TelephonyManager.listen(PhoneStateListener listener, int events)方法。复写PhoneStateListener中的onCallStateChanged方法。使其得到状态变化的信息进行输出。

源代码如下:
package com.duduli.li;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CallMe2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

tm.listen(new MyPhoneState(), PhoneStateListener.LISTEN_CALL_STATE);



}

class MyPhoneState extends PhoneStateListener{

@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);

switch (state) {
case 0x00000000:
System.out.println("CALL_STATE_IDLE");
break;
case 0x00000002:
System.out.println("CALL_STATE_OFFHOOK");
break;
case 0x00000001:
System.out.println("CALL_STATE_RINGING");
break;

default:
break;
}
}

};

}

CALL_STATE_IDLE 为电话空闲状态。
CALL_STATE_OFFHOOK 电话接通后的状态。
CALL_STATE_RINGING 如果有电话进来(没有接听)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值