我如何确定我的 android runnable 在工作线程上启动?

How can I be sure that my android runnable started on a worker thread?

我正在尝试学习如何使用线程在 Android/Java 上执行后台任务。我做了一个简单的测试应用程序,它纯粹做了一个新的线程(新的 Runnable())并在可运行的内部休眠。当这是 运行 时,我仍然可以点击 UI 上的按钮(我可以看到视觉反馈)。

很高兴这是有效的,我将相同的代码移到了我的主应用程序代码库中,但它似乎仍然是 运行 在 UI 线程上,因为 GUI 锁并在睡眠循环完成后弹出 ANR。

谁能帮我弄清楚为什么我的 backgroundThread 不是 运行 在后台或工作线程上?

LoginService.java

package com.example.services;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class LoginService implements ILoginService {

private Handler handler;

public LoginService() {
    handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
        // Handle a message from the background task
            Log.d("LoginService","Received a Message");
        }
    };
}

/*
    Check if the username exists in the list
 */
@Override
public boolean isUsernameValid(String username) {
    Log.d("LoginService","Starting Runnable");
    Thread backgroundThread = new Thread(new LoginRunnable(this));

    backgroundThread.run();
    Log.d("LoginService", "Started Runnable");
    return true;
}

/*
    Check if the password is correct for the downloaded user
 */
@Override
public boolean doesPasswordMatch(String password) {
    return false;
}

@Override
public void handleStatus(int i) {
    switch (i) {
        case 1:
            Message newMessage = handler.obtainMessage();
            newMessage.sendToTarget();
            break;
        default:
            break;
    }
}
}

LoginRunnable.java

package com.example.services;

import android.util.Log;

public class LoginRunnable implements Runnable {

public ILoginService loginService;

public LoginRunnable(ILoginService loginService)
{
    this.loginService = loginService;
}

@Override
public void run() {
    // Do the login tasks here
    try {
        for(int i = 0;i<=50;i++) {
            Thread.sleep(200);
            Log.d("LoginRunnable","200ms has passed");
            loginService.handleStatus(1);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    // Return some completion message to the LoginService.

}
}

来自应用程序的输出

11-08 18:06:36.757  11722-11722/com.example  D/LoginService﹕ Starting Runnable
11-08 18:06:36.971  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:37.181  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:37.391  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:37.601  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:37.811  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.021  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.231  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.441  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.652  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:38.861  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.072  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.282  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.492  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.701  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:39.912  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.121  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.332  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.542  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.752  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:40.962  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:41.172  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:41.381  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:41.592  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:41.802  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.012  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.222  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.432  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.642  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:42.851  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.062  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.272  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.482  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.692  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:43.902  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.112  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.322  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.532  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.741  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:44.952  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:45.162  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:45.372  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:45.582  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:45.792  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.002  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.212  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.422  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.632  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:46.842  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:47.052  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:47.262  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:47.472  11722-11722/com.example  D/LoginRunnable﹕ 200ms has passed
11-08 18:06:47.472  11722-11722/com.example  D/LoginService﹕ Started Runnable
11-08 18:06:47.482  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.482  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.483  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.484  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.485  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.486  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.487  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.487  11722-11722/com.example  D/LoginService﹕ Received a Message
11-08 18:06:47.487  11722-11722/com.example  D/LoginService﹕ Received a Message

工作测试程序(单独项目)的输出

11-08 17:57:31.284  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:31.284  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:31.493  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:31.494  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:31.704  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:31.704  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:31.914  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:31.914  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:32.123  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:32.124  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message
11-08 17:57:32.334  32596-32610/com.subarudev.threadingtest D/SleepRunnable﹕ Sleeping on thread
11-08 17:57:32.334  32596-32596/com.subarudev.threadingtest D/MainActivity﹕ Handle Message

backgroundThread.run() -> backgroundThread.start()?