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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月17日 03时11分38秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
jQuery源码值得借鉴的地方
2019-04-30
vue、react、angular小小比较
2019-04-30
Node的应用场景
2019-04-30
js数据类型
2019-04-30
js有哪些内置对象
2019-04-30
eval是做什么的
2019-04-30
null,undefined 的区别
2019-04-30
javascript 代码中的“use strict“
2019-04-30
JSON 的了解
2019-04-30
defer和async
2019-04-30
严格模式的限制
2019-04-30
attribute和property的区别是什么
2019-04-30
面向对象编程思想
2019-04-30
reactNative 打包报错 mergeReleaseResources
2019-04-30
对web标准、可用性、可访问性的理解
2019-04-30
JS判断 array 类型
2019-04-30
map与forEach的区别
2019-04-30
箭头函数与普通函数的区别
2019-04-30
JS 数组和对象的遍历⽅式,以及⼏种⽅式的⽐较
2019-04-30
JS 事件的各个阶段
2019-04-30