简体   繁体   English

具有延迟操作的处理程序 post(Runnable r)

[英]Handler post(Runnable r) with delayed action

So, I'm trying to implement a feature that makes my sip phone app wake up and send a registration to the server after receiving a call, which sends a push notification if it is asleep.因此,我正在尝试实现一项功能,使我的 sip 电话应用程序在接到电话后唤醒并向服务器发送注册,如果它处于睡眠状态,则发送推送通知。 I'm using the Linphone Library for such means.我正在使用 Linphone 库来实现这种方式。

This is done trough a method called dispatchOnUIThread(Runnable r), which is suposed to use a handler from its class and post said runnable.这是通过名为 dispatchOnUIThread(Runnable r) 的方法完成的,该方法应该使用其 class 中的处理程序并发布所述可运行。 The runnable itself implements the actions to be took after receiveing a push and determining if the registration is active or not, as follows: runnable 本身实现了在收到推送并确定注册是否处于活动状态后要采取的操作,如下所示:

on the class LinphoneUtils:在 class LinphoneUtils 上:

public final class LinphoneUtils {
private static final Handler sHandler = new Handler(Looper.getMainLooper());

  public static void dispatchOnUIThread(Runnable r) {
      sHandler.post(r);
  }
}

and it is called like this:它被称为这样的:

public void onMessageReceived(RemoteMessage remoteMessage) {
  LinphoneUtils.dispatchOnUIThread(mPushReceivedRunnable);
}

and the implementation of mPushReceivedRunnable:以及 mPushReceivedRunnable 的实现:

private Runnable mPushReceivedRunnable =
        new Runnable() {
            @Override
            public void run() {
                if (!LinphoneContext.isReady()) {
                    new LinphoneContext(getApplicationContext());
                    LinphoneContext.instance().start(true);
                } else {
                    Log.i(TAG, "[Push Notification] Notifying Core");
                    if (LinphoneManager.getLcIfManagerNotDestroyedOrNull() != null && LinphoneManager.getLc().getCallsNb() == 0) {
                        LinphoneCore core = LinphoneManager.getLc();
                        if (core != null) {
                            core.setNetworkReachable(true);
                            core.refreshRegisters();
                        }
                    }
                }
            }
        };

The thing is, if the registration is not active, after the dispatchOnUIThread method that follows a push notification, the app does not send a registration to the server until it receives a second push, in which case it sends the registration which was supposed to follow the first push and an invite for the call which sent the second push all together.问题是,如果注册不活跃,在推送通知之后的 dispatchOnUIThread 方法之后,应用程序不会向服务器发送注册,直到它收到第二次推送,在这种情况下,它会发送本应跟随的注册第一次推送和发送第二次推送的呼叫邀请一起。

The code is pretty similar to the one from Linphone itself, but I don't understend the reason for this delayed action, anyone can tell why it behaves like this?该代码与 Linphone 本身的代码非常相似,但我不明白这种延迟操作的原因,任何人都可以说出为什么它会这样吗?

I guess a later version of the Linphone library fixed the issue.我想 Linphone 库的更高版本解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM