gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:33 26 4
gpt4 key购买 nike

我为 Android 手机制作了一个小应用。

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

因此,在通话结束后,我需要最后拨出的号码或正在运行的应用程序的事件。

最佳答案

安卓 list :

<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.in/blog/2011/09/is-your-call-ended-on-android-phone/

关于java - 如何在Android App中获取通话结束事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5869269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com