如何在 Android 设备中检测来电?
Posted
技术标签:
【中文标题】如何在 Android 设备中检测来电?【英文标题】:How to detect incoming calls, in an Android device? 【发布时间】:2013-03-11 22:20:05 【问题描述】:我正在尝试制作一个应用,例如,当电话打到我想检测号码时。以下是我尝试过的,但它没有检测到来电。
我想在后台运行我的MainActivity
,我该怎么做?
我已在manifest
文件中授予权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
我还应该在清单中提供什么吗?
public class MainActivity extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
public class myPhoneStateChangeListener extends PhoneStateListener
@Override
public void onCallStateChanged(int state, String incomingNumber)
super.onCallStateChanged(state, incomingNumber);
if (state == TelephonyManager.CALL_STATE_RINGING)
String phoneNumber = incomingNumber;
【问题讨论】:
我们应该为android P做什么 【参考方案1】:这是我用来执行此操作的:
清单:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<!--This part is inside the application-->
<receiver android:name=".CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
我的基础可重用呼叫检测器
package com.gabesechan.android.reusable.receivers;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
public abstract class PhonecallReceiver extends BroadcastReceiver
//The receiver will be recreated whenever android feels like it. We need a static variable to remember data between instantiations
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber; //because the passed incoming is only valid in ringing
@Override
public void onReceive(Context context, Intent intent)
//We listen to two intents. The new outgoing call only tells us of an outgoing call. We use it to get the number.
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
else
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE))
state = TelephonyManager.CALL_STATE_IDLE;
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
state = TelephonyManager.CALL_STATE_OFFHOOK;
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING))
state = TelephonyManager.CALL_STATE_RINGING;
onCallStateChanged(context, state, number);
//Derived classes should override these to respond to specific events of interest
protected abstract void onIncomingCallReceived(Context ctx, String number, Date start);
protected abstract void onIncomingCallAnswered(Context ctx, String number, Date start);
protected abstract void onIncomingCallEnded(Context ctx, String number, Date start, Date end);
protected abstract void onOutgoingCallStarted(Context ctx, String number, Date start);
protected abstract void onOutgoingCallEnded(Context ctx, String number, Date start, Date end);
protected abstract void onMissedCall(Context ctx, String number, Date start);
//Deals with actual events
//Incoming call- goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
//Outgoing call- goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
public void onCallStateChanged(Context context, int state, String number)
if(lastState == state)
//No change, debounce extras
return;
switch (state)
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallReceived(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
if(lastState != TelephonyManager.CALL_STATE_RINGING)
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
else
isIncoming = true;
callStartTime = new Date();
onIncomingCallAnswered(context, savedNumber, callStartTime);
break;
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING)
//Ring but no pickup- a miss
onMissedCall(context, savedNumber, callStartTime);
else if(isIncoming)
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
else
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
break;
lastState = state;
然后要使用它,只需从中派生一个类并实现一些简单的功能,无论您关心哪种调用类型:
public class CallReceiver extends PhonecallReceiver
@Override
protected void onIncomingCallReceived(Context ctx, String number, Date start)
//
@Override
protected void onIncomingCallAnswered(Context ctx, String number, Date start)
//
@Override
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end)
//
@Override
protected void onOutgoingCallStarted(Context ctx, String number, Date start)
//
@Override
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end)
//
@Override
protected void onMissedCall(Context ctx, String number, Date start)
//
此外,您还可以看到我写的关于为什么代码与我的blog 上的代码类似的文章。要点链接:https://gist.github.com/ftvs/e61ccb039f511eb288ee
编辑:更新为更简单的代码,因为我已经重新编写了该类以供自己使用
【讨论】:
此代码不显示任何内容。它的作用是在拨出电话开始/结束时呼叫您,并向您传递号码、开始时间和结束时间。实际上展示它是你的工作,因为我不知道你想怎么做。 @GabeSechan:太棒了!你能指导我处理呼叫等待的情况吗? 只是补充一下,当应用程序不在前台或后台时它不起作用,直到我在接收器中添加了这个:“android:enabled="true" 静态变量将一直存在,直到应用程序被踢出内存(这可能会很长一段时间,具体取决于服务是否正在运行和一般手机内存条件)。但是,是的,它们可能会丢失。您可以通过共享首选项将其写入磁盘,但这也可能导致您得到错误的结果 - 它会阻止您在某些情况下正确清除数据,例如在手机重启时。对于我的用例,罕见的空数据和丢失数据比不正确的数据要好。您可以根据自己的需要随意调整。 @GabeSechan :其中似乎有一个错误。lastState
不应初始化为 CALL_STATE_IDLE
。当我的应用程序在当前状态为RINGING
时被杀死时,我错过了几个电话。因为当它在通话结束时再次变为IDLE
时,静态变量会重新初始化为CALL_STATE_IDLE
并且它什么都不做会去抖动。所以我们失去了对lastState
的引用。【参考方案2】:
private MyPhoneStateListener phoneStateListener = new MyPhoneStateListener();
注册
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
注销
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
【讨论】:
我应该在 mainactivity 哪里使用它? 把它放在你正在监听变化的类中。通常注册在 oncreate 中完成,在 ondestroy 中取消注册。在类中全局声明对象 此选项不需要权限。 虽然 Gabe 的解决方案更适合更具侵入性的功能,即 Viber 类应用程序,但对于那些需要简单地对用户拨打电话的操作做出反应的人来说,这是最佳解决方案。在这种情况下请求运行时权限很可能是矫枉过正,可能不会被用户接受。 我想做点什么,电话来了,这段代码怎么办?【参考方案3】:使用 Android P - API 级别 28: You need to get READ_CALL_LOG permission
限制访问通话记录
Android P 将CALL_LOG
、READ_CALL_LOG
、WRITE_CALL_LOG
和PROCESS_OUTGOING_CALLS
权限从PHONE
权限组移至新的CALL_LOG
权限组。该组可让用户更好地控制和查看需要访问电话敏感信息(例如读取电话记录和识别电话号码)的应用。
要从 PHONE_STATE 意图操作中读取数字,您需要READ_CALL_LOG
权限和READ_PHONE_STATE
权限。
要从onCallStateChanged()
读取数字,您现在只需要READ_CALL_LOG
权限。您不再需要READ_PHONE_STATE
权限。
【讨论】:
对于那些只在AndroidManifest.xml
中添加READ_CALL_LOG
的人,重点在MainActivity
中添加权限请求。
我正在研究一些替代方法。我们可以从通知监听器中获取手机号码。但是存在一个问题,即来电不作为通知处理的某些设备。所以我没有得到手机号码。
如果我在我的应用程序中使用READ_CALL_LOG
权限,我应该在 Google Play Console 上填写声明。如果我使用READ_CALL_LOG
权限从电话簿中搜索姓名,我需要从列表中选择什么?【参考方案4】:
更新:Gabe Sechan 发布的非常棒的代码不再有效,除非您明确要求用户授予必要的权限。下面是一些代码,您可以在主 Activity 中放置这些代码来请求这些权限:
if (getApplicationContext().checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED)
// Permission has not been granted, therefore prompt the user to grant permission
ActivityCompat.requestPermissions(this,
new String[]Manifest.permission.READ_PHONE_STATE,
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
if (getApplicationContext().checkSelfPermission(Manifest.permission.PROCESS_OUTGOING_CALLS)
!= PackageManager.PERMISSION_GRANTED)
// Permission has not been granted, therefore prompt the user to grant permission
ActivityCompat.requestPermissions(this,
new String[]Manifest.permission.PROCESS_OUTGOING_CALLS,
MY_PERMISSIONS_REQUEST_PROCESS_OUTGOING_CALLS);
另外:正如某人在Gabe's post 下方的评论中提到的那样,您必须向接收器添加一点 sn-p 代码 android:enabled="true
,以便在应用程序当前未在前景:
<!--This part is inside the application-->
<receiver android:name=".CallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
【讨论】:
如果应用程序没有任何Activity,只有广播接收器和服务怎么办。那么我们在哪里编写这段代码来获得用户的权限,因为在获得此权限之前不会调用广播接收器。 你至少需要一个 MainActivity,即使它只打开一次。以我的呼叫拦截应用 RoboStop 为例:当用户第一次下载应用,然后点击应用图标启动应用时,系统会提示他们授予我的应用必要的权限。该应用程序还具有启用/禁用呼叫阻止的按钮,但用户无需再次启动应用程序/活动,呼叫阻止将在后台进行,而无需用户再次启动应用程序/活动。 那些难以实现的人,请遵循本教程studytutorial.in/…【参考方案5】:只是为了更新 Gabe Sechan 的答案。如果您的清单要求获得 READ_CALL_LOG 和 READ_PHONE_STATE 的权限,onReceive 将调用 TWICE。其中一个有 EXTRA_INCOMING_NUMBER,另一个没有。你必须测试哪个有它,它可以以任何顺序出现。
https://developer.android.com/reference/android/telephony/TelephonyManager.html#ACTION_PHONE_STATE_CHANGED
【讨论】:
【参考方案6】:这可能会对您有所帮助并添加要求权限
public class PhoneListener extends PhoneStateListener
private Context context;
public static String getincomno;
public PhoneListener(Context c)
Log.i("CallRecorder", "PhoneListener constructor");
context = c;
public void onCallStateChanged (int state, String incomingNumber)
if(!TextUtils.isEmpty(incomingNumber))
// here for Outgoing number make null to get incoming number
CallBroadcastReceiver.numberToCall = null;
getincomno = incomingNumber;
switch (state)
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("CallRecorder", "CALL_STATE_RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
【讨论】:
这似乎没问题。但是我如何从活动中使用它?请告诉我详情【参考方案7】:我修复了Gabe Sechan answer,我使用了以下代码并且它工作正常。 我注意到当接收者意图具有“incoming_number”键时,我可以获得电话号码。所以我过滤了传入意图并使用 EXTRA_INCOMING_NUMBER 和 EXTRA_PHONE_NUMBER 来获取电话号码。
public class ChangeCallStateListener extends BroadcastReceiver
private static String lastState = TelephonyManager.EXTRA_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber; //because the passed incoming is only valid in ringing
@Override
public void onReceive(Context context, Intent intent)
Log.d("CallObserver", "CallReceiver is starting ....");
List<String> keyList = new ArrayList<>();
Bundle bundle = intent.getExtras();
if (bundle != null)
keyList = new ArrayList<>(bundle.keySet());
Log.e("CallObserver", "keys : " + keyList);
if (keyList.contains("incoming_number"))
String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String phoneIncomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
String phoneOutgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String phoneNumber = phoneOutgoingNumber != null ? phoneOutgoingNumber : (phoneIncomingNumber != null ? phoneIncomingNumber : "");
if (phoneState != null && phoneNumber != null)
if (lastState.equals(phoneState))
//No change, debounce extras
return;
Log.e("CallObserver", "phoneState = " + phoneState);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(phoneState))
isIncoming = true;
callStartTime = new Date();
//
lastState = TelephonyManager.EXTRA_STATE_RINGING;
if (phoneNumber != null)
savedNumber = phoneNumber;
onIncomingCallStarted(context, savedNumber, callStartTime);
else if (TelephonyManager.EXTRA_STATE_IDLE.equals(phoneState))
if (lastState.equals(TelephonyManager.EXTRA_STATE_RINGING))
//
lastState = TelephonyManager.EXTRA_STATE_IDLE;
onMissedCall(context, savedNumber, callStartTime);
else
if (isIncoming)
//
lastState = TelephonyManager.EXTRA_STATE_IDLE;
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
else
//
lastState = TelephonyManager.EXTRA_STATE_IDLE;
Log.d("CallObserver", "onOutgoingCallEnded called !! : ");
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(phoneState))
if (lastState.equals(TelephonyManager.EXTRA_STATE_RINGING))
isIncoming = true;
else
isIncoming = false;
callStartTime = new Date();
savedNumber = phoneNumber;
//
lastState = TelephonyManager.EXTRA_STATE_OFFHOOK;
onOutgoingCallStarted(context, savedNumber, callStartTime);
protected void onIncomingCallStarted(Context ctx, String number, Date start)
Log.d("CallObserver", "onIncomingCallStarted : " + " number is : " + number);
protected void onOutgoingCallStarted(Context ctx, String number, Date start)
Log.d("CallObserver", "onOutgoingCallStarted : " + " number is : " + number);
protected void onIncomingCallEnded(Context context, String number, Date start, Date end)
protected void onOutgoingCallEnded(Context context , String number, Date start, Date end)
protected void onMissedCall(Context context, String number, Date start)
别忘了获得运行时权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
【讨论】:
您是如何绕过 READ_PRIVILEGED_PHONE_STATE 权限的?我在我的日志中看到了这个:requires android.permission.READ_PRIVILEGED_PHONE_STATE due to sender android (uid 1000)
现在记不太清楚了,试试READ_CALL_LOG权限【参考方案8】:
这是一个简单的方法,可以避免使用PhonestateListener
和其他并发症。
所以在这里我们接收到来自 android 的 3 个事件,例如 RINGING
、OFFHOOK
和 IDLE
。为了获得所有可能的调用状态,我们需要定义自己的状态,如RINGING
、OFFHOOK
、IDLE
、FIRST_CALL_RINGING
、SECOND_CALL_RINGING
。
它可以处理电话中的每个状态。
请以我们正在接收来自 android 的事件的方式思考,我们将定义我们的待命状态。查看代码。
public class CallListening extends BroadcastReceiver
private static final String TAG ="broadcast_intent";
public static String incoming_number;
private String current_state,previus_state,event;
public static Boolean dialog= false;
private Context context;
private SharedPreferences sp,sp1;
private SharedPreferences.Editor spEditor,spEditor1;
public void onReceive(Context context, Intent intent)
//Log.d("intent_log", "Intent" + intent);
dialog=true;
this.context = context;
event = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
incoming_number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d(TAG, "The received event : "+event+", incoming_number : " + incoming_number);
previus_state = getCallState(context);
current_state = "IDLE";
if(incoming_number!=null)
updateIncomingNumber(incoming_number,context);
else
incoming_number=getIncomingNumber(context);
switch (event)
case "RINGING":
Log.d(TAG, "State : Ringing, incoming_number : " + incoming_number);
if((previus_state.equals("IDLE")) || (previus_state.equals("FIRST_CALL_RINGING")))
current_state ="FIRST_CALL_RINGING";
if((previus_state.equals("OFFHOOK"))||(previus_state.equals("SECOND_CALL_RINGING")))
current_state = "SECOND_CALL_RINGING";
break;
case "OFFHOOK":
Log.d(TAG, "State : offhook, incoming_number : " + incoming_number);
if((previus_state.equals("IDLE")) ||(previus_state.equals("FIRST_CALL_RINGING")) || previus_state.equals("OFFHOOK"))
current_state = "OFFHOOK";
if(previus_state.equals("SECOND_CALL_RINGING"))
current_state ="OFFHOOK";
startDialog(context);
break;
case "IDLE":
Log.d(TAG, "State : idle and incoming_number : " + incoming_number);
if((previus_state.equals("OFFHOOK")) || (previus_state.equals("SECOND_CALL_RINGING")) || (previus_state.equals("IDLE")))
current_state="IDLE";
if(previus_state.equals("FIRST_CALL_RINGING"))
current_state = "IDLE";
startDialog(context);
updateIncomingNumber("no_number",context);
Log.d(TAG,"stored incoming number flushed");
break;
if(!current_state.equals(previus_state))
Log.d(TAG, "Updating state from "+previus_state +" to "+current_state);
updateCallState(current_state,context);
public void startDialog(Context context)
Log.d(TAG,"Starting Dialog box");
Intent intent1 = new Intent(context, NotifyHangup.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
public void updateCallState(String state,Context context)
sp = PreferenceManager.getDefaultSharedPreferences(context);
spEditor = sp.edit();
spEditor.putString("call_state", state);
spEditor.commit();
Log.d(TAG, "state updated");
public void updateIncomingNumber(String inc_num,Context context)
sp = PreferenceManager.getDefaultSharedPreferences(context);
spEditor = sp.edit();
spEditor.putString("inc_num", inc_num);
spEditor.commit();
Log.d(TAG, "incoming number updated");
public String getCallState(Context context)
sp1 = PreferenceManager.getDefaultSharedPreferences(context);
String st =sp1.getString("call_state", "IDLE");
Log.d(TAG,"get previous state as :"+st);
return st;
public String getIncomingNumber(Context context)
sp1 = PreferenceManager.getDefaultSharedPreferences(context);
String st =sp1.getString("inc_num", "no_num");
Log.d(TAG,"get incoming number as :"+st);
return st;
【讨论】:
【参考方案9】:@Gabe Sechan,感谢您的代码。除了onOutgoingCallEnded()
,它工作正常。它永远不会被执行。测试手机是三星 S5 & Trendy。我认为有2个错误。
1:缺少一对括号。
case TelephonyManager.CALL_STATE_IDLE:
// Went to idle- this is the end of a call. What type depends on previous state(s)
if (lastState == TelephonyManager.CALL_STATE_RINGING)
// Ring but no pickup- a miss
onMissedCall(context, savedNumber, callStartTime);
else
// this one is missing
if(isIncoming)
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
else
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
// this one is missing
break;
2:lastState
不会被 state
更新,如果它位于函数的末尾。它应该被替换为该函数的第一行
public void onCallStateChanged(Context context, int state, String number)
int lastStateTemp = lastState;
lastState = state;
// todo replace all the "lastState" by lastStateTemp from here.
if (lastStateTemp == state)
//No change, debounce extras
return;
//....
另外,我已按照您的建议将 lastState
和 savedNumber
放入共享偏好中。
刚刚使用上述更改对其进行了测试。至少在我的手机上修复了错误。
【讨论】:
【参考方案10】:请使用以下代码。它将帮助您获取带有其他呼叫详细信息的来电号码。
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity" >
<TextView
android:id="@+id/call"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
private static final int MISSED_CALL_TYPE = 0;
private TextView txtcall;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtcall = (TextView) findViewById(R.id.call);
StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
while (managedCursor.moveToNext())
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode)
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
+ dir + " \nCall Date:--- " + callDayTime
+ " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
managedCursor.close();
txtcall.setText(sb);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
并在您的清单请求中获得以下权限:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
【讨论】:
READ_LOGS 这样做是为了让您的应用被禁止进入 Play 商店【参考方案11】:您需要一个用于ACTION_PHONE_STATE_CHANGED
的广播接收器,只要电话状态从空闲、响铃、摘机更改为之前的值和您可以检测到这是传入/传出呼叫的新值,这将调用您的接收。
所需的权限是:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
但如果您还想在该广播中接收 EXTRA_INCOMING_NUMBER,则需要另一个权限:“android.permission.READ_CALL_LOG”
代码如下:
val receiver: BroadcastReceiver = object : BroadcastReceiver()
override fun onReceive(context: Context, intent: Intent)
Log.d(TAG, "onReceive")
override fun onResume()
val filter = IntentFilter()
filter.addAction("android.intent.action.PHONE_STATE")
registerReceiver(receiver, filter)
super.onResume()
override fun onPause()
unregisterReceiver(receiver)
super.onPause()
在接收器类中,我们可以通过读取意图来获取当前状态,如下所示:
intent.extras["state"]
额外的结果可能是:
RINGING -> 如果您的手机正在响铃
OFFHOOK -> 如果您正在与某人交谈(传入或传出 打电话)
IDLE -> 如果通话结束(来电或去电)
使用 PHONE_STATE 广播,我们不需要使用 PROCESS_OUTGOING_CALLS 权限或已弃用的 NEW_OUTGOING_CALL 操作。
【讨论】:
【参考方案12】:参考Gabe Sechan的回答。如前所述,在拨出呼叫的情况下,我们有以下状态更改:IDLE -> OFFHOOK -> IDLE。在 Gabe 的原始答案中,savedNumber
仅在电话状态变为 RINGING 时才设置,这对于拨出电话来说不是真的。当电话状态变为 OFFHOOK 时,还设置 savedNumber
的一个小修复:
case TelephonyManager.CALL_STATE_OFFHOOK:
if(lastState != TelephonyManager.CALL_STATE_RINGING)
//IDLE to OFFHOOK for example.
isIncoming = false;
callStartTime = new Date();
savedNumber = number;
onOutgoingCallStarted(context, savedNumber, callStartTime);
...
此修复允许将拨打的号码传递给拨出呼叫方法,就像传入号码传递给来电或未接来电方法一样。
【讨论】:
以上是关于如何在 Android 设备中检测来电?的主要内容,如果未能解决你的问题,请参考以下文章