如何在Android App中获取通话结束事件

How to get call end event in Android App(如何在Android App中获取通话结束事件)
本文介绍了如何在Android App中获取通话结束事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为 Android 手机制作了小应用程序.

I have made small app for Android mobile.

在一种情况下,我没有得到任何解决方案.实际上,我的应用程序具有呼叫客户的小功能.

In one situation I am not getting any solution. Actually my app has small functionality for calling to customer.

所以在通话结束后,我需要拨打最后一个号码或运行哪个应用程序的事件.

So after call ended I need that event of which last number will dialed or which app is runs.

推荐答案

AndroidManifest:

<receiver android:name=".PhoneStateBroadcastReceiver">  
       <intent-filter>  
               <action android:name="android.intent.action.PHONE_STATE">       
       </action></intent-filter>  
</receiver> 

添加以下权限:

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

PhoneStateBroadcastReceiver.java(稍微重构了代码)

package com.mobisys.android.salesbooster;

import com.mobisys.android.salesbooster.database.HelperDatabase;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "PhoneStateBroadcastReceiver";
    Context mContext;
    String incoming_number;
    private int prev_state;

    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
        CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
        telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager

        Bundle bundle = intent.getExtras();
        String phoneNr = bundle.getString("incoming_number");
        Log.v(TAG, "phoneNr: "+phoneNr);
        mContext = context;
    }

    /* Custom PhoneStateListener */
    public class CustomPhoneStateListener extends PhoneStateListener {

        private static final String TAG = "CustomPhoneStateListener";

        @Override
        public void onCallStateChanged(int state, String incomingNumber){

           if( incomingNumber != null && incomingNumber.length() > 0 ) 
            incoming_number = incomingNumber; 

            switch(state){
                case TelephonyManager.CALL_STATE_RINGING:
                        Log.d(TAG, "CALL_STATE_RINGING");
                        prev_state=state;
                        break;

                case TelephonyManager.CALL_STATE_OFFHOOK:
                                Log.d(TAG, "CALL_STATE_OFFHOOK");
                                prev_state=state;
                                break;

                case TelephonyManager.CALL_STATE_IDLE:

                    Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_number);

                    if((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)){
                        prev_state=state;
                        //Answered Call which is ended
                    }
                    if((prev_state == TelephonyManager.CALL_STATE_RINGING)){
                        prev_state=state;
                        //Rejected or Missed call
                    }
                    break;
            }
        }
    }
}

在此处阅读更多信息,来源:http://mobisys.在/blog/2011/09/is-your-call-ended-on-android-phone/

Read more here, Source : http://mobisys.in/blog/2011/09/is-your-call-ended-on-android-phone/

这篇关于如何在Android App中获取通话结束事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)