服务或后台线程 [英] Service or background thread

查看:102
本文介绍了服务或后台线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建android的蓝牙井字应用程序。由于我不熟悉与Android,我学会了在网络上阅读源$ C ​​$ CS。截至目前,它的工作原理是这样的:

I have created a bluetooth tic tac toe application in android. As I am not familiar with android, I learned by reading source codes on the net. As of now how it works is like this:


  • 活动1>显示新游戏,大约,退出。没有什么这里

  • 活动2>列表视图与按钮,使发现和查找设备。发现的设备和连接上的用户触摸成立。另一个按钮可用于交换球员的名字,还有一键进入到下一个活动

  • 活动3>决定哪些球员先行。相应的岩石,纸,剪刀3个按钮,另一个按钮交换信息,和一个按钮以进入下一个活动

  • 活动4>实际的游戏。一些逻辑(它不是普通的井字),但基本上对用户触摸它使得计算,跨越一个整数发送和回报等待数据。

要实现通信我有3个线程:serverThread侦听连接,connectThread连接到设备并commsThread发送和接收数据。建立连接后只在需要commsThread。现在,这是我失去了清晰度。我在游戏类类型commsThread的对象,我在练习2调用commsThreadObject.start()一旦建立连接,并且我保留这个线程中运行,直到应用程序退出。在每一个活动,我通过属于该活动到commsThread的句柄。

To implement the communication I have 3 threads: serverThread to listen for connections, connectThread to connect to a device and commsThread to send and receive data. After connection is established only the commsThread is needed. Now this is where I lose clarity. I have an object of type commsThread in the Game class, I call commsThreadObject.start() in Activity 2 once connection is established and I keep this thread running till the application exits. On each activity, I pass a handle belonging to that activity to the commsThread.

现在的code确实为我工作,但它只是不利落的感觉(其实感觉非常肮脏code)。比从处理程序更新UI以外的任何需要静态对象,所以我结束了很多东西转换成静态的。我也明白,保持运行这样一个线程可以导致内存泄漏。现在我正在学习有关的服务,我只是想知道,如果这是这样做的正确方法?

Now the code does work for me, but it just doesn't feel neat(in fact feels like very dirty code). Anything other than updating the UI from the handler requires static objects, so I ended up converting a lot of things to static. Also I understand that keeping a thread running like this can result in memory leaks. Now I am learning about services, I just wanna know if that is the right way to do this?

此外,在早期的实验,我只是有一个单一的活动,并开启按钮单击布局。这是一个好办法?

Also in earlier experiments I simply had a single activity and switched layouts on button click. Is that a good approach?

这是code为我在游戏类处理程序。

This is the code for my handler in Game class.

static Handler receiveCell = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        int numOfBytesReceived = msg.arg1;
        byte[] buffer = (byte[]) msg.obj;
        String strReceived = new String(buffer);
        strReceived = strReceived.substring(0, numOfBytesReceived);
            gameInstance.changeSymbol();
            int x = Integer.parseInt(strReceived);
            int cur = gameInstance.curCell;
            boardGroup[cur].put(x % 3, x / 3, gameInstance.current);
            gameInstance.state = boardGroup[cur].checkLines();
            gameInstance.checkState(cur % 3, cur / 3);
            gameInstance.changeSymbol();
            gameInstance.curCell = x;
            if (boardGroup[x].emptyBlock() == 0) {
                numPad.enableInput();
            } else {
                boardGroup[x].enableInput();
            }
            currentTurn.setText(playerName);
        }
    };

正如你可以看到有我的处理颇有几分。我不得不做出引用静态这里的所有变量。取而代之的是有反正我可以将这个出来的处理程序。也许知道一些方式,当收到消息?像本地 strReceived.wait() strReceived.notify()

推荐答案

好吧,如果你的屏幕在逻辑上是不同的(如色斑/设置/主/约),那么它是有道理的,使他们不同的活动 - 这样,当您的应用程序变得更加复杂,你还没有得到一个怪物的类,它的方式太多了。

Well, if your screens are logically different (like splash/setting/main/about) then it makes sense to make them different activities - so that as your app becomes more complex you still don't get a monster class that does way too much.

至于线程VS服务 - 如果你正确地管理你的线程(断开插座,杀死线程的onPause(),并把它带回在onResume(),等...),那么有一个与你的做法没有问题。但是,如果你想在背景或某些其他原因接收数据有更复杂的生命周期则服务是你的事。

As for thread vs service - if you manage your thread properly (disconnect socket and kill thread in onPause() and bring it back in onResume(), etc...) then there's no problem with your approach. However, if you want to receive data in background or for some other reason have a more complex lifecycle then Services are your thing.

这篇关于服务或后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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