BluetoothChat同步onResume活动周期的方法,为什么呢? [英] BluetoothChat synchronized onResume Activity lifecycle method, why?

查看:202
本文介绍了BluetoothChat同步onResume活动周期的方法,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习,现在蓝牙Android的API,我跑进BluetoothChat例子。
<一href=\"http://developer.android.com/resources/samples/BluetoothChat/index.html\">http://developer.android.com/resources/samples/BluetoothChat/index.html

I'm studying right now the bluetooth Android API, and I ran into the BluetoothChat example. http://developer.android.com/resources/samples/BluetoothChat/index.html

它包含了许多错误,首先,它使用API​​ 11,但一个简单的事实清单不强制这个最小的API。

It contains many errors, first of all the simple fact that it uses API 11 but manifest does not force this minimum API.

其他有趣的事情是在活动的生命周期方法使用synchronized关键字,像onResume:

Other interesting thing is the use of synchronized keyword on Activity lifecycle methods, like on onResume:

    @Override
public synchronized void onResume() {
    super.onResume();
    if(D) Log.e(TAG, "+ ON RESUME +");

    // Performing this check in onResume() covers the case in which BT was
    // not enabled during onStart(), so we were paused to enable it...
    // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
    if (mChatService != null) {
        // Only if the state is STATE_NONE, do we know that we haven't started already
        if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
          // Start the Bluetooth chat services
          mChatService.start();
        }
    }
}

为什么这个关键字用来在那里?有没有合理的解释,或者干脆是谁写的code并不知道onResume将由同一个线程总是叫的人吗?还是我错过了什么?

Why this keyword is used there? Is there any reasonable explanation, or simply the one who wrote the code didn't know that onResume will be called always by the same thread? Or I miss something?

感谢你在前进!

推荐答案

这似乎是一个pretty老问题,但在这里我想可能会是什么:

This seems to be a pretty old question, but here's what I think may be going on:

我的猜测是,它要小心,当对话的回报。所述BluetoothChat例使用对话(以及覆盖对话样活性)用于使蓝牙,启用发现,以及发起配对/连接。

My guess is that it wants to be careful about when "dialogs" return. The BluetoothChat example uses dialogs (as well as an overlay dialog-like activity) for enabling Bluetooth, enabling discovery, and initiating pairing/connections.

我不知道这是肯定的,但我怀疑有哪里不同的线程正在返回到主活动中的错误,并造成混乱,如何处理 onResume

I don't know this for sure but I suspect there was a bug where different threads were returning to the main Activity and caused confusion as to how to handle onResume.

什么他们或许应该做的是同步对象和使用国旗的块来确定的状态。这样的意图,状态和功能都更加清晰 - 和应用程序知道它应该在 onResume 做;

What they probably should have done is synchronize a block on an object and used flags to determine the state. That way the intention, state and functionality are more clear -- and the app knows what it should do in onResume;

这样的事情也许:

//class fields    
private Object myLockObj = new Object();
private boolean isPausedForPairing = false;

public void onResume()
{
    super.onResume();

    synchronized (myLockObj)
    {
        if (isPausedForPairing)
        {
            //handle a "pairing" onResume
        }
    }
}

不过,由于它是一个示例应用程序,他们可能已经决定去与一些更简单。示例应用程序不总是遵循约定,因为这个想法是展示所需例如特定的code。有时以下约定可能会增添了不少喧宾夺主code的。无论你是否认同这一点是由你虽然。

However, due to it being an example app, they may have decided to go with something more simple. Example apps don't always follow convention because the idea is to demonstrate the particular code needed for the example. Sometimes following convention might add a lot of "distracting" code. Whether or not you agree with that is up to you though.

这篇关于BluetoothChat同步onResume活动周期的方法,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆