码迷,mamicode.com
首页 > 其他好文 > 详细

电话状态的监听。响铃,静止,接起

时间:2017-06-15 21:47:47      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:telephony   rri   div   super   stat   dem   text   pat   idle   

技术分享
package com.sharpcj.telephonestatelistenerdemo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    private TelephonyManager mTelephonyManager;
    private static final String TAG = "sharpcj:";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTelephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
        //不要忘记了权限的添加
        //<uses-permission Android:name="android.permission.READ_PHONE_STATE" />
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTelephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_NONE);
    }

    class MyPhoneStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            /**
             * 返回电话状态
             *
             * CALL_STATE_IDLE 无任何状态时
             * CALL_STATE_OFFHOOK 接起电话时
             * CALL_STATE_RINGING 电话进来时
             */
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE://无任何状态时
                    logi("CALL_STATE_IDLE");
                    break;
                case TelephonyManager.CALL_STATE_RINGING://电话进来时
                    logi("CALL_STATE_RINGING");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK://接起电话时
                    logi("CALL_STATE_OFFHOOK");
                    break;
                default:
                    super.onCallStateChanged(state, incomingNumber);
                    break;
            }
        }
    }


    public void logi(String s) {
        Log.i(TAG, "joy:---" + s);
    }
}
技术分享

电话状态的监听。响铃,静止,接起

标签:telephony   rri   div   super   stat   dem   text   pat   idle   

原文地址:http://www.cnblogs.com/wuyuxin/p/7019905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!