gpt4 book ai didi

java - Android - 在打电话时启动应用程序

转载 作者:行者123 更新时间:2023-12-01 18:32:39 27 4
gpt4 key购买 nike

有没有办法开发一个在用户接到电话时启动的应用程序?我无法真正详细说明这个想法,但想知道是否有一些电话可以让这种情况发生。

最佳答案

您可以像这样使用广播接收器

public class PhoneStatReceiver extends BroadcastReceiver{        
private static final String TAG = "PhoneStatReceiver";
private static boolean incomingFlag = false;
private static String incoming_number = null;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
incomingFlag = false;
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, "call OUT:"+phoneNumber);

}else{
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
incomingFlag = true;//标识当前是来电
incoming_number = intent.getStringExtra("incoming_number");
Log.i(TAG, "RINGING :"+ incoming_number);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(incomingFlag){
Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if(incomingFlag){
Log.i(TAG, "incoming IDLE");
}
break;
}
}

}

}

像这样在 AndroidManifest 中注册此接收器

<receiver android:name=".filter.PhoneStatReceiver">  

<intent-filter>

<action android:name="android.intent.action.PHONE_STATE"/>

<action android:name="android.intent.action.NEW_OUTGOING_CALL" />

</intent-filter>

</receiver>

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

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

关于java - Android - 在打电话时启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23440765/

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