代码之家  ›  专栏  ›  技术社区  ›  Kalu Khan Luhar

当传入呼叫活动置于后台时返回呼叫UI

  •  -2
  • Kalu Khan Luhar  · 技术社区  · 6 年前

    我正在开发一个自定义呼叫应用程序,当用户在传入呼叫期间将应用程序放在后台时,我必须在其中放置一个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());
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Kalu Khan Luhar    6 年前

    我通过如下更新代码解决了问题:

    @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);
    }
    

    也不要忘记设置 android:launchMode="singleTop" 属于 活动 在里面 AndroidManifest。xml 文件