在 Android 上如何知道我是否正在通话?
我想知道我是否正在通话中。
如果我正在通话,则启动服务(服务部分是明确的)。我该怎么做?
在参加电话会议时,我需要致电服务...我不知道该怎么做?有什么帮助吗?
I want to know whether I am in a call.
If I am in a call then start the service (service part is clear). How do I do this?
While attending the call I need to call the service... I am unaware of how to do this? Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要广播接收器...
在清单中声明广播接收器...
还声明使用权限...
广播接收器类...
以及一个用于自定义电话状态侦听器的类...
You need broadcast receiver ...
In manifest declare broadcast receiver ...
Also declare uses-permission ...
The broadcast receiver class ...
And one class to customize phone state listener...
TelephonyManager.getCallState()
< 之一CALL_STATE_IDLE
CALL_STATE_OFFHOOK
CALL_STATE_RINGING
如果此适合您的要求,它的代码比 Pied Piper 更全面的解决方案少得多。
TelephonyManager.getCallState()
returns one ofCALL_STATE_IDLE
CALL_STATE_OFFHOOK
CALL_STATE_RINGING
If this fits your requirements, it's much less code than Pied Piper's more comprehensive solution.
您只能知道有来电,但无法修改。 :(
看到这个
为什么是 2.3 版本android 没有 android.permission.MODIFY_PHONE_STATE 吗?解决方案是什么?
You can only come to know call is coming but you can't modify this. :(
see this
why 2.3 version of android does not hava android.permission.MODIFY_PHONE_STATE ? and what is the solution for this?
毫无疑问,这是一个老问题。但它作为谷歌上的第一个结果出现。因此,我决定添加另一个有用的方法。
当电话状态更改时,系统会发送
TelephonyManager.ACTION_PHONE_STATE_CHANGED
广播。此广播有一个额外的状态,可以使用TelephonyManager.EXTRA_STATE
提取。额外状态可以有三种选择:
EXTRA_STATE_IDLE
EXTRA_STATE_OFFHOOK
EXTRA_STATE_RINGING
您可以设计一个广播接收器来处理所有这些:
您可以阅读更多关于它在这里:
https://developer.android.com/reference/android/telephony/TelephonyManager#ACTION_PHONE_STATE_CHANGED
注意: 广播接收器将在后台触发,自Android O+,您无法从后台进程启动后台服务。考虑使用
context.startForegroundService(intent)
启动前台服务。在服务的onStartCommand(...)
中,调用startForeground(...)
。否则,系统将引发致命异常。但是,在 Android O 以下,您可以安全地使用context.startService(intent)
而不是startForegroundService(...)
。This is an old question, no doubt. But it pops up as the first result on Google. Hence, I decided to add another useful method.
When the phone state is changed, the system sends the
TelephonyManager.ACTION_PHONE_STATE_CHANGED
broadcast. This broadcast has an extra state that can be extracted usingTelephonyManager.EXTRA_STATE
.The extra state can have three alternatives:
EXTRA_STATE_IDLE
EXTRA_STATE_OFFHOOK
EXTRA_STATE_RINGING
You can design a broadcast receiver that will take care of all these:
You can read more about it here:
https://developer.android.com/reference/android/telephony/TelephonyManager#ACTION_PHONE_STATE_CHANGED
Note: The broadcast receiver will be triggered in background and as of Android O+, you cannot start a background service from a background process. Consider starting a foreground service with
context.startForegroundService(intent)
. In theonStartCommand(...)
of the service, callstartForeground(...)
. Otherwise, the system raise a fatal exception. However, below Android O, you can safely usecontext.startService(intent)
instead ofstartForegroundService(...)
.