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

本文介绍如何在Android应用中使用全局通知适配来电监听功能。通过在Service中添加特定代码,创建自定义的通知栏,实现来电时显示通知并提供接听和挂断操作。需添加USE_FULL_SCREEN_INTENT和FOREGROUND_SERVICE权限。

依据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);
  &n

`ActivityTaskManager`提示 “Abort background activity starts from 10067” 通常与 Android 系统的后台启动限制有关。以下是可能的原因及对应的解决办法: ### 原因 - **后台启动限制**:从 Android 8.0(API 级别 26)开始,Android 引入了后台执行限制,以减少应用在后台不必要的资源消耗。当应用在后台尝试启动 Activity 时,系统可能会阻止该操作,并给出相应提示。10067 一般代表应用的 UID(用户标识符),意味着该 UID 对应的应用尝试在后台启动 Activity 被系统中止。 - **应用权限问题**:应用可能没有获得必要的权限来在后台启动 Activity。例如,某些特殊的后台启动权限可能需要用户手动授予,如果应用没有这些权限,后台启动 Activity 会失败。 - **系统资源紧张**:当系统资源(如内存、CPU 等)紧张时,为了保证系统的稳定性和性能,系统会限制应用在后台的活动,包括阻止 Activity 的后台启动。 ### 解决办法 #### 调整启动逻辑 - **使用通知**:如果应用需要在后台提醒用户,可以使用通知(Notification)来代替直接启动 Activity。当用户点击通知时,再启动相应的 Activity。示例代码如下: ```java // 创建通知管理器 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 创建通知渠道(Android 8.0及以上需要) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel); } // 创建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.ic_notification) .setContentTitle("通知标题") .setContentText("通知内容") .setPriority(NotificationCompat.PRIORITY_DEFAULT); // 创建启动 Activity 的 PendingIntent Intent intent = new Intent(this, TargetActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); // 发送通知 notificationManager.notify(1, builder.build()); ``` - **判断应用是否在前台**:在启动 Activity 之前,先判断应用是否在前台。如果应用在前台,可以正常启动 Activity;如果在后台,则采取其他方式处理。示例代码如下: ```java public boolean isAppInForeground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses != null) { String packageName = context.getPackageName(); for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { return true; } } } return false; } // 在需要启动 Activity 的地方调用 if (isAppInForeground(this)) { Intent intent = new Intent(this, TargetActivity.class); startActivity(intent); } else { // 处理应用在后台的情况,如发送通知 } ``` #### 请求必要权限 - 如果应用确实需要在后台启动 Activity,可以请求相应的权限。例如,在 AndroidManifest.xml 中声明必要的权限,并在运行时请求权限(如果需要)。 ```xml <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> ``` #### 优化应用资源使用 - 优化应用的内存和 CPU 使用,避免在后台占用过多资源。可以通过释放不必要的对象、优化算法等方式来减少资源消耗,从而降低系统对应用后台活动的限制。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值