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

2022-01-15 00:00:00 android android-emulator java

我已经为 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/

相关文章