如何在 Android 中拨打电话并在通话结束后返回到我的活动?
我正在启动一项活动来拨打电话,但是当我按下“结束通话”按钮时,它不会返回到我的活动。您能告诉我如何启动一个通话活动,当按下“结束通话”按钮时该活动会返回给我吗?这就是我打电话的方式:
String url = "tel:3334444";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
I am launching an activity to make a phone call, but when I pressed the 'end call' button, it does not go back to my activity. Can you please tell me how can I launch a call activity which comes back to me when 'End call' button is pressed? This is how I'm making the phone call:
String url = "tel:3334444";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
使用 PhoneStateListener 查看通话何时结束。您很可能需要触发侦听器操作以等待呼叫开始(等到再次从 PHONE_STATE_OFFHOOK 更改为 PHONE_STATE_IDLE),然后编写一些代码以使您的应用程序恢复到 IDLE 状态。
您可能需要在服务中运行侦听器以确保其保持运行并重新启动您的应用程序。一些示例代码:
监听器定义:
在您的
Manifest.xml
文件中添加以下权限:use a PhoneStateListener to see when the call is ended. you will most likely need to trigger the listener actions to wait for a the call to start (wait until changed from PHONE_STATE_OFFHOOK to PHONE_STATE_IDLE again) and then write some code to bring your app back up on the IDLE state.
you may need to run the listener in a service to ensure it stays up and your app is restarted. some example code:
Listener definition:
In your
Manifest.xml
file add the following permission:这是关于Starter提出的问题。
您的代码的问题是您没有正确传递号码。
代码应该是:
不要忘记在清单文件中添加权限。
或
在使用
DIAL
时拨打紧急电话号码。This is regarding the question asked by Starter.
The problem with your code is that you are not passing the number properly.
The code should be:
Do not forget to add the permission in manifest file.
or
for emergency number in case
DIAL
is used.我们遇到了同样的问题,并设法通过使用
PhoneStateListener
来识别通话何时结束来解决该问题,但此外,我们还必须在再次启动之前finish()
原始活动与startActivity
一起使用,否则调用日志将位于其前面。We had the same problem and managed to solve it by using a
PhoneStateListener
to identify when the call ends, but additionally we had tofinish()
the original activity before starting it again withstartActivity
, otherwise the call log would be in front of it.我发现 EndCallListener 是最实用的示例,为了获取所描述的行为(finish()、调用、重新启动),我添加了一些 SharedPreferences,以便侦听器有一个引用来管理此行为。
我的 OnClick、initialise 和 EndCallListener 仅响应来自应用程序的调用。其他呼叫被忽略。
将这些添加到 strings.xml
以及清单中的类似内容,如果您需要返回到调用之前的外观
并将它们放入“myActivity”中,
请使用它来初始化 myActivity 中 onClick 的行为,例如在 onCreate() 之后
您应该发现,单击电话号码列表即可完成您的活动,拨打该号码并在通话结束时返回您的活动。
在应用程序仍在运行时从外部拨打电话不会重新启动您的活动(除非它与上次呼叫的 BParty 号码相同)。
:)
I found the EndCallListener the most functional example, to get the behaviour described (finish(), call, restart) I added a few SharedPreferences so the Listener had a reference to manage this behaviour.
My OnClick, initialise and EndCallListener only respond to calls from app. Other calls ignored.
add these to strings.xml
and something like this in your Manifest if you need to return to the look and feel before the call
and put these in your 'myActivity'
use this to initilaise the behaviour for your onClick in myActivity e.g. after onCreate()
You should find that clicking your list of phone numbers finishes your activty, makes the call to the number and returns to your activty when the call ends.
Making a call from outside your app while it's still around won't restart your activty (unless it's the same as the last BParty number called).
:)
您可以使用 startActivityForResult()
you can use startActivityForResult()
从我的角度来看,这是解决方案:
当然,在 Activity (类)定义中,您必须实现 View.OnClickListener 。
This is solution from my point of view:
Of course in Activity (class) definition you have to implement View.OnClickListener .
这是我的示例,首先用户输入他/她想要拨打的号码,然后按通话按钮并被定向到电话。呼叫取消后,用户将被发送回应用程序。为此,按钮需要在 xml 中具有 onClick 方法(本例中为“makePhoneCall”)。您还需要在清单中注册权限。
清单
活动
XML
Here is my example, first the user gets to write in the number he/she wants to dial and then presses a call button and gets directed to the phone. After call cancelation the user gets sent back to the application. In order to this the button needs to have a onClick method ('makePhoneCall' in this example) in the xml. You also need to register the permission in the manifest.
Manifest
Activity
XML
如果您要使用侦听器,您还需要将此权限添加到清单中。
If you are going to use a listener you will need to add this permission to the manifest as well.
在看到呼叫完成后,在 PhoneStateListener 内部更好地使用:
其中 CallDispatcherActivity 是用户发起呼叫的活动(在我的例子中是打给出租车服务调度员)。这只是从顶部删除了 Android 电话应用程序,用户返回而不是我在这里看到的丑陋代码。
Inside PhoneStateListener after seeing the call is finished better use:
Where CallDispatcherActivity is the activity where the user has launched a call (to a taxi service dispatcher, in my case). This just removes Android telephony app from the top, the user gets back instead of ugly code I saw here.
要返回到您的
Activity
,您需要监听TelephonyStates
。在该侦听器
上,您可以发送Intent
以在手机空闲时重新打开您的Activity
。至少我会这么做。
To return to your
Activity
, you will need to listen toTelephonyStates
. On thatlistener
you can send anIntent
to re-open yourActivity
once the phone is idle.At least thats how I will do it.
尝试
在活动结束时使用:。它会将您重定向到之前的活动。
Try using:
at the end of activity. It will redirect you to your previous activity.
使用
PhoneStateListener
时,需要确保使用PHONE_STATE_OFFHOOK
后面的PHONE_STATE_IDLE
来触发通话后要执行的操作。如果触发发生在看到PHONE_STATE_IDLE
时,您最终将在通话之前执行此操作。因为你会看到状态改变PHONE_STATE_IDLE -> PHONE_STATE_OFFHOOK -> 电话状态PHONE_STATE_IDLE。
When
PhoneStateListener
is used, one need to make surePHONE_STATE_IDLE
following aPHONE_STATE_OFFHOOK
is used to trigger the action to be done after the call. If the trigger happens upon seeingPHONE_STATE_IDLE
, you will end up doing it before the call. Because you will see the state changePHONE_STATE_IDLE -> PHONE_STATE_OFFHOOK -> PHONE_STATE_IDLE.
// 在 setonclicklistener 中放置以下代码:
// 在清单中授予调用权限:
// in setonclicklistener put this code:
// give permission for call in manifest:
@Dmitri Novikov,
FLAG_ACTIVITY_CLEAR_TOP
清除新实例之上的所有活动实例。因此,它可能会在完成该过程之前结束旧实例。@Dmitri Novikov,
FLAG_ACTIVITY_CLEAR_TOP
clears any active instance on top of the new one. So, it may end the old instance before it completes the process.添加这是您的 xml:
android:autoLink="phone"
Add this is your xml:
android:autoLink="phone"
步骤:
1)在
Manifest.xml
文件中添加所需的权限。2)创建电话状态变化的监听器。
3) 在
OnCreate
中初始化侦听器,但如果您想恢复应用程序的最后状态或将其从返回堆栈中恢复,则替换
FLAG_ACTIVITY_CLEAR_TOP 和
FLAG_ACTIVITY_SINGLE_TOP
参考此答案
Steps:
1)Add the required permissions in the
Manifest.xml
file.2)Create a listener for the phone state changes.
3)Initialize the listener in your
OnCreate
but if you want to resume your application last state or to bring it back from the back stack, then replace
FLAG_ACTIVITY_CLEAR_TOP
withFLAG_ACTIVITY_SINGLE_TOP
Reference this Answer
开始通话时,看起来不错。
不过,Android 11+ 和更低版本在将应用程序置于最前面方面存在差异。
Android 10 或更低版本您需要启动一个新 Intent,Android 11+ 您只需使用
BringTaskToFront
在呼叫状态 IDLE 中:
我在进行呼叫时设置
MyActivity.MyActivityTaskId
我的活动就像这样,如果这不起作用,请在您想要返回的页面的父活动页面上设置此变量。MyActivityTaskId
是我的活动类上的静态变量,我希望这对您有用。我使用上面的代码有点不同,我在接听电话后立即打开我的应用程序,以便用户可以看到呼叫者的详细信息。
我还在
AndroidManifest.xml
中设置了一些内容:和权限:
如果或当您遇到困难时,请提出问题。
When starting your call, it looks fine.
There is a difference between android 11+ and down in bringing your app to the front though.
Android 10 or less you need to start a new intent, android 11+ you simply use
BringTaskToFront
In the call state IDLE:
I set the
MyActivity.MyActivityTaskId
when making the call on my activity like so, it this doesnt work, set this variable on the parent activity page of the page you want to get back to.MyActivityTaskId
is a static variable on my activity classI hope this will work for you. I use the above code a bit differently, I open my app as soon as the call is answered sothat the user can see the details of the caller.
I have set some stuff in the
AndroidManifest.xml
as well:and permissions:
Please ask questions if or when you get stuck.
要从应用程序进行呼叫,请使用简单的呼叫意图,之后如果您想监听呼叫状态,请使用下面的代码
1] 在您的类中实现此功能 -
2] 使用重写方法添加以下代码
3]添加这个
To call from app use simple intent for call, after that if you want to listen the call status then use below code
1] implement this in your class -
2] Add below code with overridden method
3]Add this