Android Q Beta Background activity starts(来电页面) 适配
发布日期:2021-07-16 18:25:42 浏览次数:8 分类:技术文章

本文共 2432 字,大约阅读时间需要 8 分钟。

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

 

在来电监听的在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);// 接通/挂断停止通知

注意:使用全屏通知需要添加<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />权限,使用前台Service 需要添加<uses-permission android:name="android.permission.FOREGROUND_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);

 

转载地址:https://blog.csdn.net/GuangkuoDing/article/details/89357745 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Failed to resolve: support-core-utils & Failed to resolve: appcompat
下一篇:Android Q Beta Didn’t find class “org.apache.http.client.methods.HttpPost”

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月17日 03时11分38秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章