Android 8.0来电流程分析为什么InCallService.this.onCallAdded(call)调用的父类InCallServiceImp.lonCallAdded(call)

https://blog.youkuaiyun.com/qq_27540925/article/details/79290319

本文以上文章,本人不理解部分补充

当看到以下部分 InCallService.this.onCallAdded(call);怎么就是调用父类InCallServiceImp.lonCallAdded(call);

很是不解;

10.在 InCallService 中handleMessage处理MSG_SET_IN_CALL_ADAPTER消息的时候就注册了监听,这里继续跟进到InCallService中的实现去

private Phone.Listener mPhoneListener = new Phone.Listener()
        ...
        @Override
        public void onCallAdded(Phone phone, Call call) {
            //调用InCallService对象的onCallAdded方法
            InCallService.this.onCallAdded(call);
        }
 

Dialer 
11.查找InCallService的子类

public class InCallServiceImpl extends InCallService 

  @Override
  public void onCallAdded(Call call) {

    if ((CallList.getInstance().getVideoUpgradeRequestCall() != null ||
            CallList.getInstance().getSendingVideoUpgradeRequestCall() != null ||
            /// M: When is cancel upgrade progress,we can't add another call in calllist. @{
            CallList.getInstance().getSendingCancelUpgradeRequestCall() != null)
            ///@}
            && !isEmergency(call)) {
       ...
    } else {
        InCallPresenter.getInstance().onCallAdded(call);
    }
  }
 

6.查看监听Call添加的观察者 
在CallsManager构造函数初始化的时候添加了一些列观察者,还有就是通过addListener去添加的。

        mListeners.add(mInCallWakeLockController);
        mListeners.add(statusBarNotifier);
        mListeners.add(mCallLogManager);
        mListeners.add(mPhoneStateBroadcaster);
        mListeners.add(mInCallController);
        mListeners.add(mCallAudioManager);
        mListeners.add(missedCallNotifier);
        mListeners.add(mHeadsetMediaButton);
        mListeners.add(mProximitySensorManager);
        ...
        mListeners.add(new CallConnectedVibrator(mContext));

这里我们继续跟进mInCallController,

    @Override
    public void onCallAdded(Call call) {
        if (!isBoundAndConnectedToServices()) {
            bindToServices(call);
        } else {
            // We are bound, and we are connected.
            adjustServiceBindingsForEmergency();

            // This is in case an emergency call is added while there is an existing call.
            mEmergencyCallHelper.maybeGrantTemporaryLocationPermission(call,
                    mCallsManager.getCurrentUserHandle());

            //添加Call
            addCall(call);

            List<ComponentName> componentsUpdated = new ArrayList<>();
            for (Map.Entry<InCallServiceInfo, IInCallService> entry : mInCallServices.entrySet()) {
                InCallServiceInfo info = entry.getKey();

                if (call.isExternalCall() && !info.isExternalCallsSupported()) {
                    continue;
                }

                if (call.isSelfManaged() && !info.isSelfManagedCallsSupported()) {
                    continue;
                }

                // Only send the RTT call if it's a UI in-call service
                boolean includeRttCall = info.equals(mInCallServiceConnection.getInfo());

                componentsUpdated.add(info.getComponentName());
                IInCallService inCallService = entry.getValue();

                ParcelableCall parcelableCall = ParcelableCallUtils.toParcelableCall(call,
                        true /* includeVideoProvider */, mCallsManager.getPhoneAccountRegistrar(),
                        info.isExternalCallsSupported(), includeRttCall);
                try {
                    //AIDL调用远程的addCall方法
                    inCallService.addCall(parcelableCall);

                } catch (RemoteException ignored) {
                }
            }
            Log.i(this, "Call added to components: %s", componentsUpdated);
        }
    }
 

InCallController.java

private boolean onConnected(InCallServiceInfo info, IBinder service) {
        Trace.beginSection("onConnected: " + info.getComponentName());
        Log.i(this, "onConnected to %s", info.getComponentName());

        IInCallService inCallService = IInCallService.Stub.asInterface(service);
        mInCallServices.put(info, inCallService);

       ..............略
    }

 

 

135    private class InCallServiceBindingConnection extends InCallServiceConnection {

136
137        private final ServiceConnection mServiceConnection = new ServiceConnection() {
138            @Override
139            public void onServiceConnected(ComponentName name, IBinder service) {
140                Log.startSession("ICSBC.oSC");
141                synchronized (mLock) {
142                    try {
143                        Log.d(this, "onServiceConnected: %s %b %b", name, mIsBound, mIsConnected);
144                        mIsBound = true;
145                        if (mIsConnected) {
146                            // Only proceed if we are supposed to be connected.
147                            onConnected(service);
148                        }
149                    } finally {
150                        Log.endSession();
151                    }
152                }
153            }
        };

 

mServiceConnection

 

 @Override
        public int connect(Call call) {
            if (mIsConnected) {
                Log.addEvent(call, LogUtils.Events.INFO, "Already connected, ignoring request.");
                return CONNECTION_SUCCEEDED;
            }

            if (call != null && call.isSelfManaged() &&
                    !mInCallServiceInfo.isSelfManagedCallsSupported()) {
                Log.i(this, "Skipping binding to %s - doesn't support self-mgd calls",
                        mInCallServiceInfo);
                mIsConnected = false;
                return CONNECTION_NOT_SUPPORTED;
            }

            Intent intent = new Intent(InCallService.SERVICE_INTERFACE);
            intent.setComponent(mInCallServiceInfo.getComponentName());

            if (call != null && !call.isIncoming() && !call.isExternalCall()){
                intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS,
                        call.getIntentExtras());
                intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
                        call.getTargetPhoneAccount());
            }

            Log.i(this, "Attempting to bind to InCall %s, with %s", mInCallServiceInfo, intent);
            mIsConnected = true;
            if (!mContext.bindServiceAsUser(intent, mServiceConnection,
                        Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE |
                        Context.BIND_ABOVE_CLIENT,
                        UserHandle.CURRENT)) {

                Log.w(this, "Failed to connect.");
                mIsConnected = false;
            }

            if (call != null && mIsConnected) {
                call.getAnalytics().addInCallService(
                        mInCallServiceInfo.getComponentName().flattenToShortString(),
                        mInCallServiceInfo.getType());
            }

            return mIsConnected ? CONNECTION_SUCCEEDED : CONNECTION_FAILED;
        }

 

InCallService.SERVICE_INTERFACE = "android.telecom.InCallService";

 

incallUi的AndroidManifest.xml中service  InCallServiceImpl 配置     

  <action android:name="android.telecom.InCallService"/>

   <service
        android:directBootAware="true"
        android:exported="true"
        android:name="com.android.incallui.InCallServiceImpl"
        android:permission="android.permission.BIND_INCALL_SERVICE">
      <meta-data
          android:name="android.telecom.IN_CALL_SERVICE_UI"
          android:value="true"/>
      <meta-data
          android:name="android.telecom.IN_CALL_SERVICE_RINGING"
          android:value="false"/>
      <meta-data
          android:name="android.telecom.INCLUDE_EXTERNAL_CALLS"
          android:value="true"/>

      <intent-filter>
        <action android:name="android.telecom.InCallService"/>
      </intent-filter>
    </service>

所以:mContext.bindServiceAsUser(intent, mServiceConnection,
                        Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE |
                        Context.BIND_ABOVE_CLIENT,
                        UserHandle.CURRENT)

bindServiceAsUser 启动的Service就是InCallServiceImpl;

所以    InCallService.this.onCallAdded(call);会调用子类InCallServiceImpl的onCallAdded(call)方法;

就能理解了telecom 到InCallUI 界面显示的调用;

第一次写,感觉逻辑不是很清楚,见谅!如有不正确的地方,欢迎指正,谢谢!

 

 

### **分析 `RemoteServiceException: Context.startForegroundService() did not call Service.startForeground()`** 这个错误发生在 **Android 8.0(API 26)及以上版本**,原因是: > **应用调用了 `startForegroundService()` 启动前台服务,但未在 5 秒内调用 `Service.startForeground()` 设置通知。** --- ## **1. 错误原因** - **前台服务必须显示通知**(Android 8.0+ 强制要求)。 - **`startForegroundService()` 只是一个请求**,真正的“前台服务”状态需要 `startForeground()` 确认。 - **5 秒超时**:如果 `startForeground()` 未及时调用,系统会抛出此异常并终止服务。 --- ## **2. 解决方案** ### **(1)确保 `Service.startForeground()` 被调用** 在 `Service` 的 `onCreate()` 或 `onStartCommand()` 中设置通知: ```java @Override public void onCreate() { super.onCreate(); Notification notification = buildNotification(); // 构建通知 startForeground(NOTIFICATION_ID, notification); // 必须调用! } private Notification buildNotification() { return new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("前台服务") .setContentText("正在运行...") .setSmallIcon(R.drawable.ic_notification) .build(); } ``` ### **(2)处理极端情况(如初始化耗时)** 如果服务初始化可能超过 5 秒,可以先调用 `startForeground()` 占位,再更新通知: ```java @Override public int onStartCommand(Intent intent, int flags, int startId) { // 先快速启动前台服务(避免超时) startForeground(NOTIFICATION_ID, getPlaceholderNotification()); // 异步执行耗时任务 new Thread(() -> { doHeavyWork(); // 任务完成后更新通知 updateNotification(); }).start(); return START_STICKY; } ``` ### **(3)检查 `Service` 生命周期** 确保 **`startForeground()` 在所有代码路径中被调用**,例如: ```java @Override public int onStartCommand(Intent intent, int flags, int startId) { try { // 业务逻辑... startForeground(NOTIFICATION_ID, notification); } catch (Exception e) { // 即使崩溃也要调用,否则会触发 ANR startForeground(NOTIFICATION_ID, getErrorNotification()); } return START_STICKY; } ``` ### **(4)适配 Android 12+ 的“前台服务限制”** Android 12 要求 **前台服务必须声明权限**: ```xml <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> ``` 并确保通知渠道有效: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "Foreground Service", NotificationManager.IMPORTANCE_LOW ); getSystemService(NotificationManager.class).createNotificationChannel(channel); } ``` --- ## **3. 调试技巧** ### **(1)检查是否遗漏 `startForeground()`** - 在 `Service` 的所有 `return` 前添加日志: ```java Log.d("ForegroundService", "startForeground() called"); ``` - 使用 **Android Studio 的 Debugger** 断点检查调用流程。 ### **(2)查看崩溃堆栈** ```bash adb logcat | grep RemoteServiceException ``` **典型堆栈**: ``` E AndroidRuntime: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2196) ... ``` ### **(3)测试低版本兼容性** - 在 **Android 7.1(API 25)及以下**,`startForeground()` 非强制,但建议统一处理。 --- ## **4. 常见错误场景** | **场景** | **解决方案** | |----------|-------------| | 忘记调用 `startForeground()` | 在 `onCreate()`/`onStartCommand()` 中调用 | | 初始化耗时超过 5 秒 | 先调用占位通知,再异步更新 | | 代码崩溃导致未调用 | 在 `try-catch` 中确保调用 | | Android 12+ 权限缺失 | 添加 `FOREGROUND_SERVICE` 权限 | --- ## **5. 代码示例(完整实现)** ```java public class MyForegroundService extends Service { private static final int NOTIFICATION_ID = 1; private static final String CHANNEL_ID = "ForegroundServiceChannel"; @Override public void onCreate() { super.onCreate(); createNotificationChannel(); startForeground(NOTIFICATION_ID, buildNotification()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // 执行任务... return START_STICKY; } private Notification buildNotification() { return new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("服务运行中") .setContentText("正在后台执行任务...") .setSmallIcon(R.drawable.ic_notification) .build(); } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "Foreground Service", NotificationManager.IMPORTANCE_DEFAULT ); getSystemService(NotificationManager.class).createNotificationChannel(channel); } } @Override public IBinder onBind(Intent intent) { return null; } } ``` ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值