Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Handler.handleMessage未在测试中调用,但在应用程序中调用_Java_Android_Android Service_Android Testing_Android Handler - Fatal编程技术网

Java Handler.handleMessage未在测试中调用,但在应用程序中调用

Java Handler.handleMessage未在测试中调用,但在应用程序中调用,java,android,android-service,android-testing,android-handler,Java,Android,Android Service,Android Testing,Android Handler,我有一个在单独进程中运行的服务。该服务在onCreate()方法中生成一个新线程。此线程将消息发送回服务 如果我手动启动应用程序,一切正常-我的服务中的处理程序会收到消息。但是在我的测试中,handleMessage()方法从未被调用 如何修复测试以使handleMessage()方法工作 谢谢 服务: public class MyService extends Service { private ServiceHandler mHandler; private final

我有一个在单独进程中运行的服务。该服务在
onCreate()
方法中生成一个新线程。此线程将消息发送回服务

如果我手动启动应用程序,一切正常-我的服务中的
处理程序会收到消息。但是在我的测试中,
handleMessage()
方法从未被调用

如何修复测试以使
handleMessage()
方法工作

谢谢

服务:

public class MyService extends Service {

    private ServiceHandler mHandler;
    private final int MSG_HELLO_WORLD = 1;

    private final String TAG = getClass().getSimpleName();

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
    }


    @Override
    public void onCreate() {
        Log.d(TAG, "MyService.onCreate");
        super.onCreate();

        mHandler = new ServiceHandler();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "Thread: run()");

                while (true) {

                    try {
                        Log.d(TAG, "sleeping...");
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        Log.d(TAG, "Thread was interrupted.");
                        break;
                    }

                    Log.d(TAG, "sending message...");
                    Message msg = Message.obtain(mHandler, MSG_HELLO_WORLD);
                    msg.sendToTarget();
                }
            }
        }).start();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "MyService.onStartCommand");
        return START_REDELIVER_INTENT;
    }

    private class ServiceHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "ServiceHandler.handleMessage");

            if (msg.what == MSG_HELLO_WORLD) {
                Log.d(TAG, "Hello World message received!");
            }
        }
    }

}
public class MyServiceTest extends ServiceTestCase<MyService> {

    private static final String TAG = MyService.class.getSimpleName();

    public MyServiceTest() {
        super(MyService.class);
    }


    public void setUp() throws Exception {
        super.setUp();
    }


    public void testStartService() throws Exception {

        Intent startServiceIntent = new Intent(getContext(), MyService.class);

        startService(startServiceIntent);

        MyService myService = getService();

        assertNotNull(myService);

        // give the service some time to send messages
        Thread.sleep(10000);
    }
}
测试:

public class MyService extends Service {

    private ServiceHandler mHandler;
    private final int MSG_HELLO_WORLD = 1;

    private final String TAG = getClass().getSimpleName();

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
    }


    @Override
    public void onCreate() {
        Log.d(TAG, "MyService.onCreate");
        super.onCreate();

        mHandler = new ServiceHandler();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "Thread: run()");

                while (true) {

                    try {
                        Log.d(TAG, "sleeping...");
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        Log.d(TAG, "Thread was interrupted.");
                        break;
                    }

                    Log.d(TAG, "sending message...");
                    Message msg = Message.obtain(mHandler, MSG_HELLO_WORLD);
                    msg.sendToTarget();
                }
            }
        }).start();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "MyService.onStartCommand");
        return START_REDELIVER_INTENT;
    }

    private class ServiceHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "ServiceHandler.handleMessage");

            if (msg.what == MSG_HELLO_WORLD) {
                Log.d(TAG, "Hello World message received!");
            }
        }
    }

}
public class MyServiceTest extends ServiceTestCase<MyService> {

    private static final String TAG = MyService.class.getSimpleName();

    public MyServiceTest() {
        super(MyService.class);
    }


    public void setUp() throws Exception {
        super.setUp();
    }


    public void testStartService() throws Exception {

        Intent startServiceIntent = new Intent(getContext(), MyService.class);

        startService(startServiceIntent);

        MyService myService = getService();

        assertNotNull(myService);

        // give the service some time to send messages
        Thread.sleep(10000);
    }
}
测试的logcat输出(
handleMessage()
未被调用):

我对你的问题很感兴趣,所以我开始挖掘

如果我手动启动应用程序,一切正常-我的服务中的
处理程序会收到消息

使用
mHandler=new ServiceHandler()
MyService
onCreate()
方法中初始化
mHandler
时,与当前执行的线程的消息队列相关联
mHandler
从队列中依次由其处理的进程。因此,您可以在日志中看到“Hello World message received!”

但是在我的测试中,
handleMessage()
方法从未被调用

当您测试服务时,它的方法在
InstrumentationThread
(请参阅
调试器中的线程)上被调用,而
活套
只是没有循环,因此无法处理消息,也不会显示在日志中

如何修复测试以使handleMessage()方法工作

我建议以下几种选择:

  • MyService测试
    绑定到调用
    MyService
    方法的线程上
    。为组件再编写一个
    单元测试
    ,然后将测试与
    MyServiceTest
    合并到一个公共
    .java
    测试文件中。这将显示所需的
    logcat
    输出
  • MyServiceTest
    中循环准备好的
    InstrumentationThread
    活套。您可以通过在
    MyServiceTest
    testStartService()
    方法中添加
    Looper.loop()
    来实现:

    public void testStartService() throws Exception {
    
        Intent startServiceIntent = new Intent(getContext(), MyService.class);
        startService(startServiceIntent);
        MyService myService = getService();
        assertNotNull(myService);
    
        // Run the Looper (this call is to be added)
        Looper.loop();
    
        Thread.sleep(10000);
    }
    
    注意:运行
    活套
    将显示所需的
    logcat
    输出,但测试不会因为
    Looper.loop()
    无限期运行而停止,直到调用