我正在开发一个自定义呼叫应用程序,当用户在传入呼叫期间将应用程序置于后台时,必须在其中放置UI。我正在显示通知,但我的IncomingCallActivity没有恢复。 可以在来电过程中在所有应用程序上放置一个“返回通话”用户界面吗?
通知代码如下:
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());
}
答案 0 :(得分:0)
我通过更新如下代码解决了问题:
@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 文件中设置活动的android:launchMode="singleTop"
。