Android Q Beta Background activity starts(来电页面) 适配

依据Google官方提供解决方案,使用全局通知来适配:

Android Q Beta Background activity starts(来电页面) 适配_第1张图片Android Q Beta Background activity starts(来电页面) 适配_第2张图片

 

在来电监听的在Service中添加如下代码

**
 * 启动通知
 */
private void startForegroundNotification(final int call_id, final String phone) {
    // 构建通知栏构造器
    NotificationCompat.Builder mBuilder;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                "ForegroundNotificationChannel", NotificationManager.IMPORTANCE_HIGH);
        mNotificationManager.createNotificationChannel(channel);
    }
    mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
    // 给构造器设置参数
    Intent fullScreenIntent = new Intent(this, CallActivity.class);
    fullScreenIntent.putExtra(CallActivity.EXTRA_CALL_NUM, phone);
    fullScreenIntent.putExtra(CallActivity.EXTRA_CALL_ID, call_id);
    fullScreenIntent.putExtra(CallActivity.EXTRA_CALL_TYPE, CallActivity.CALL_IN);
    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(
            this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle("来电")
            .setContentText(phone + "来电")
            .setChannelId(CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_CALL)
            .setFullScreenIntent(fullScreenPendingIntent, true);

    mNotificationManager.notify(NOTICE_ID_CALLING, mBuilder.build());
}

通话结束关闭前台通知:

mNotificationManager.cancel(NOTICE_ID_CALLING);// 接通/挂断停止通知

注意:使用全屏通知需要添加权限,使用前台Service 需要添加权限。

补充(自定义通知栏)

// 自定义通知栏视图
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
// 接听点击处理
fullScreenIntent.putExtra(CallActivity.EXTRA_CALL_ANSWER, true);
PendingIntent answerPendingIntent = PendingIntent.getActivity(
        this, (int) SystemClock.uptimeMillis(), fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn_answer, answerPendingIntent);
// 广播的形式处理挂断
Intent intent1 = new Intent("com.union.sdkdemo.action_close_incall_notice");
intent1.putExtra("CALL_ID", call_id);
intent1.putExtra("NOTICE_ID", NOTICE_ID_CALLING);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, (int) SystemClock.uptimeMillis(), intent1, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn_hangup, pendingIntent1);

mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
              .setCustomHeadsUpContentView(remoteViews);

 

你可能感兴趣的:(Android Q Beta Background activity starts(来电页面) 适配)