gpt4 book ai didi

android添加额外的并以编程方式调用

转载 作者:行者123 更新时间:2023-11-30 02:09:28 24 4
gpt4 key购买 nike

我正在尝试通过我的应用程序通过放置额外数据来调用电话,但我无法在通话期间接收到该数据,可以解释一下我哪里错了下面是我调用电话和接听电话的代码

String uri = "tel:"+my_name;
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
callIntent.putExtra("data", "mynewdata");
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(callIntent);

和调用接收者

public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

final Bundle bundle = intent.getExtras();
if(bundle != null) {
showToast(intent.getExtras().getString("data"));
}

if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Log.d(TAG,"PhoneStateReceiver**Call State=" + state);
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
if(recieveCall){

}
Log.d(TAG,"PhoneStateReceiver**Idle");
} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
}
} else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
// Outgoing call
String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d(TAG,"PhoneStateReceiver **Outgoing call " + outgoingNumber);

setResultData(null); // Kills the outgoing call

} else {
Log.d(TAG,"PhoneStateReceiver **unexpected intent.action=" + intent.getAction());
}
}
}

我可以完美地接听电话,甚至广播也可以正常工作,但我没有从 Intent “intent.getExtras().getBoolean("data")”获取数据

最佳答案

在这里您需要创建一个 java 文件(我们将其命名为 Constants.java):

public class Constants
{
public static boolean data;
public static String str = "";
}

现在查看我在项目中所做的更改以及评论。

String uri = "tel:"+my_name;
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
Constants.data = true; /*see change, set data in Constants.data*/
Constants.str = "some data to pass..."; /*see change*/
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(callIntent);

现在在广播接收器中看到所做的更改...

public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

final Bundle bundle = intent.getExtras();
if(bundle != null && Constants.data) { /*changes done, get boolean from the Constants.data*/
showToast("hi i have recieve");
showToast(Constants.str); /*changes done*/
}

if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
Log.d(TAG,"PhoneStateReceiver**Call State=" + state);
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
if(recieveCall){

}
Log.d(TAG,"PhoneStateReceiver**Idle");
} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
}
} else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
// Outgoing call
String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d(TAG,"PhoneStateReceiver **Outgoing call " + outgoingNumber);

setResultData(null); // Kills the outgoing call

} else {
Log.d(TAG,"PhoneStateReceiver **unexpected intent.action=" + intent.getAction());
}
}
}

希望这能解决您如何传递数据的问题

关于android添加额外的并以编程方式调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30319187/

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