Android在单独的线程上执行BluetoothAdapter.LeScanCallback [英] Android execute BluetoothAdapter.LeScanCallback on seperate thread

查看:813
本文介绍了Android在单独的线程上执行BluetoothAdapter.LeScanCallback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,BluetoothAdapter.LeScanCallback在主线程上运行。

In Android, BluetoothAdapter.LeScanCallback runs on main thread.

private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                //The code here is executed on main thread
                Log.e("LeScanCallback", Thread.currentThread().getName());//Prints main
            }
        };

我可以通过执行以下操作使代码在独立线程上运行:

I can make the code run on a seperate thread by doing the following:

private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                @Override
                public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //The code here is executed on on new thread everytime 
                            Log.e("LeScanCallback", Thread.currentThread().getName());//Prints Thread-xxxx
                        }
                    }).start();
                }
            };

但是这会为每个回调创建一个新的线程。我想要回调中的代码在不是主线程的单个线程上运行。

But this creates a new thread for every callback. I want the code inside the callback to run on one single thread that is not the main thread. What is the right way of doing this?

推荐答案

您可以创建一个线程和looper:

You can create a thread and looper:

import android.os.Looper;
import android.os.Handler;
public class LooperThread extends Thread {
       public Handler handler;
       public void run(){
            Looper.prepare();
            handler = new Handler();
            looper.loop(); 
            /*
                 if you want to stop thread you can create you own exception, throw it and catch. Example: 
                 try
                 {
                       looper.loop();
                 } catch(MyException e){

                 }
                 And now thread is stopping (and not handling runnables from handler.post()
                 In other thread write this: 
                 thread.handler.post(new Runnable(){
                          throw new MyException(); // And now exception will be caught and thread will be stopping.
                 });
            */
       }
}



private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                private LooperThread thread = new LooperThread();
                {
                      thread.start(); // start thread once
                }
                @Override
                public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
                    if(thread.handler != null)
                        thread.handler.post(new Runnable() {
                        @Override
                        public void run() {
                            // And now this code is running on seperate thread. (Not creating new)
                            Log.e("LeScanCallback", Thread.currentThread().getName());//Prints Thread-xxxx
                        }
                    });
                }
            };   

这篇关于Android在单独的线程上执行BluetoothAdapter.LeScanCallback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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