当来电活动置于后台时返回呼叫 UI
Posted
技术标签:
【中文标题】当来电活动置于后台时返回呼叫 UI【英文标题】:Return to call UI when incoming call activity put in background 【发布时间】:2019-01-23 02:40:15 【问题描述】:我正在开发一个自定义呼叫应用程序,当用户在来电期间将应用程序置于后台时,我必须在其中放置一个 UI。我正在显示通知,但我的 IncomingCallActivity 没有恢复。 是否可以在来电期间在所有应用程序上放置 UI“返回通话”?
通知代码如下:
private void createNotification()
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(this, IncomingCallActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.pref_noti_icon)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setContentText(this.getResources().getString(R.string.notification_return_to_call))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
【问题讨论】:
亲爱的@ADM 我已经编辑了我的问题,现在看来它指向了我真正的问题。请考虑撤回反对票。谢谢你 您不需要将"return to call
放在 Window 上。只需为 Activity 提供PendingIntent
,它将打开相同的 InCallActivity onClick 通知。确保您为 InCallActivity 使用正确的启动模式。此外,您应该使用带有通知的操作来挂断电话。只需从任何 System Phone APP 中获取参考即可。
【参考方案1】:
我已通过如下更新代码解决了问题:
@Override
public void onPause()
super.onPause();
if (!isNotificationShown)
createNotification();
@Override
public void onResume()
super.onResume();
if (isNotificationShown)
clearNotification();
@Override
public void onDestroy()
super.onDestroy();
clearNotification();
//Create Notification
private void createNotification()
isNotificationShown = true;
createNotificationChannel();
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(IncomingCallActivity.this, IncomingCallActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.pref_noti_icon)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setContentText(this.getResources().getString(R.string.notification_return_to_call))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
private void clearNotification()
isNotificationShown = false;
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager!=null)
notificationManager.cancel(NOTIFICATION_ID);
另外不要忘记在 androidManifest.xml 文件中设置 Activity 的android:launchMode="singleTop"
。
【讨论】:
以上是关于当来电活动置于后台时返回呼叫 UI的主要内容,如果未能解决你的问题,请参考以下文章