Android电话响铃、接听、挂断状态

来源:互联网 发布:2016双色球预测软件 编辑:程序博客网 时间:2024/05/16 05:20
public class PhoneActivity extends BroadcastReceiver {    private TelephonyManager tm;    @Override    public void onReceive(Context context, Intent intent) {        //判断是否为手机行为状态        if("android.intent.action.PHONE_STATE".equals(intent.getAction())){            //获取电脑管理者            tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);            int state=tm.getCallState();           //获取电话号码           String phone=intent.getStringExtra("incoming_number");            switch (state){                case TelephonyManager.CALL_STATE_RINGING:                    Log.i("test",phone+"来电");                    break;                case TelephonyManager.CALL_STATE_OFFHOOK:                    Log.i("test",phone+"正在通话中");                    break;                case TelephonyManager.CALL_STATE_IDLE:                    Log.i("test",phone+"通话结束了");                    break;            }        }    }}

配置

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.zking.y2_android21_phone">    <!--读取电话状态的权限--><uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true" android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver android:name=".PhoneActivity">            <intent-filter>                <action android:name="android.intent.action.PHONE_STATE"></action>            </intent-filter>        </receiver>    </application></manifest>