【问题标题】:How to know if the calling intent is running in background or foreground?如何知道调用意图是在后台运行还是在前台运行?
【发布时间】:2016-12-19 07:39:49
【问题描述】:

我在 Android 中使用呼叫电话意图如下:

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+Uri.encode(phone_num.trim())));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);

包名是com.android.incallui。我想知道调用 GUI 是在后台运行还是在前台运行。因此,我创建了一个函数checkcallingGUI,如果调用GUI 在前台运行,它将返回true,如果调用电话在后台运行,它将返回false。可以检查吗?谢谢大家

【问题讨论】:

  • calling GUI 是什么意思?你的活动?
  • 您可以跟踪您的活动状态回调,并在 GUI 活动的 onPause()onResume() 方法中设置一个布尔值
  • @marmor:不。电话的呼叫 GUI 意图。例如,当我拨打一个电话号码,然后按主页按钮时,呼叫 GUI 仍在运行,但它在后台运行。

标签: android android-intent call


【解决方案1】:

您需要在TelephonyManager 呼叫状态(IDLEOFFHOOKRINGING)上注册一个listener,并使用最后收到的状态来了解当前是否正在进行呼叫。

使用这个方法注册一个监听器: https://developer.android.com/reference/android/telephony/TelephonyManager.html#listen(android.telephony.PhoneStateListener, int)

在此处阅读可能的陈述及其含义: https://developer.android.com/reference/android/telephony/TelephonyManager.html#CALL_STATE_IDLE

并在此处查看完整代码: https://stackoverflow.com/a/14537914/819355

编辑

您可以使用 KeyguardManager 服务来检查设备当前是否已锁定/安全,请参阅此方法: https://developer.android.com/reference/android/app/KeyguardManager.html#isKeyguardLocked()

如果我没记错的话,如果通话状态不是 IDLE,并且启用了键盘保护,你会得到全屏 UI,如果没有,它应该在后台。

试试这样的:

KeyguardManager kg = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (kg.isKeyguardLocked()) {
   // If telephony-state is also not IDLE, then the call UI should be full screen.
}

【讨论】:

  • 我之前尝试过,但它并没有告诉我在后台或前台调用 GUI。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
  • 1970-01-01
  • 2014-06-27
  • 2015-01-08
相关资源
最近更新 更多