無限にタスクを実行する子スレッドを実行しています。(1) 常にデータを UI スレッドに送り返し、(2) ときどきデータ (ボタンに対応) を子スレッドに送信して、無限タスクを一時停止/続行したいと考えています。私の問題は、子スレッドがルーパーで動かなくなることです。つまり、タスクが実行されません。
私の質問は次のとおりです:無限タスクをブロックせずに子スレッドが UI スレッドからメッセージを受信するようにするにはどうすればよいですか?
これは私がこれまでに持っているものです: タスク (1) については、動作する UI スレッドにハンドラーがあり、それ自体で動作するメッセージを送り返す子スレッドに無限ループがあります。
UI スレッド:
mMainHandler = new Handler() {
    public void handleMessage(Message msg) {
        Bundle b;
        b = msg.getData();
        if (msg.what==1)
            Log.i("main", "from child (running) - " + b.getBoolean("running"));
        else if (msg.what == 2)
            Log.i("main", "from child (count) - " + b.getInt("count"));
    }
};
子スレッドで (現在、フレームワークが完成するまでダミー タスクを使用しています):
while (true) {
    if (running) {
        try {
            curCount += up;
            if (curCount == maxCount)
                up = -1;
            else if (curCount == minCount)
                up = 1;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Log.e("", "local Thread error", e);
        }
        Bundle b = new Bundle(1);
        b.putInt("count", curCount);
        Message toMain = mMainHandler.obtainMessage();
        toMain.what = 2;
        toMain.setData(b);
        mMainHandler.sendMessage(toMain);
    }
}
タスク (2) では、UI スレッドにメソッドがあり、ボタンを押して子スレッドにメッセージを送信します。これは機能し、子スレッドにはハンドラーがあり、それ自体で機能します。
UI スレッド:
private void sendRunning(boolean running) {
    if (mChildHandler != null) {
        Bundle b = new Bundle(1);
        b.putBoolean("running", running);
        Message msg = mChildHandler.obtainMessage();
        msg.what = 1;
        msg.setData(b);
        mChildHandler.sendMessage(msg);
    }
}
子スレッドで:
Looper.prepare();
mChildHandler = new Handler() {
    public void handleMessage(Message msg) {
        Bundle b;
        if (msg.what==1){
            b = msg.getData();
            running = b.getBoolean("running");
            Log.i(INNER_TAG, "from main (running) - " + b.getBoolean("running"));
            Log.i(INNER_TAG, "running - " + running);
            try {
                Message toMain = mMainHandler.obtainMessage();
                toMain.what = 1;
                toMain.setData(b);
                mMainHandler.sendMessage(toMain);
            } finally {}
        }
    }
};
Looper.loop();
これらのシナリオはそれぞれ単独では問題なく機能しますが、両方を同時に実行しようとすると問題が発生します。Looper.loop() の後に無限タスクを配置すると、到達することはありません。Looper.prepare() の前に置くと、1 回実行されます。ルーパーと一緒に置いても、まだ一度しか実行されません。
どんなアイデアでも大歓迎です:)
参照用に私の完全なコード(パッケージ/インポートを除く)を次に示します。
public class MainActivity extends Activity {
Thread thread;
private Handler mMainHandler, mChildHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mMainHandler = new Handler() {
        public void handleMessage(Message msg) {
            Bundle b;
            b = msg.getData();
            if (msg.what==1)
                Log.i("main", "from child (running) - " + b.getBoolean("running"));
            else if (msg.what == 2)
                Log.i("main", "from child (count) - " + b.getInt("count"));
        }
    };
    thread = new ChildThread();
    thread.start();
    // Get a reference to the button
    Button buttonStart = (Button)findViewById(R.id.btnStart);
    Button buttonStop = (Button)findViewById(R.id.btnStop);
    // Set the click listener to run my code
    buttonStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,
                    "Starting...", Toast.LENGTH_SHORT).show();
            sendRunning(true);
        }
    });
    buttonStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,
                    "Stopping...", Toast.LENGTH_SHORT).show();
            sendRunning(false);
        }
    });
}
private void sendRunning(boolean running) {
    if (mChildHandler != null) {
        Bundle b = new Bundle(1);
        b.putBoolean("running", running);
        Message msg = mChildHandler.obtainMessage();
        msg.what = 1;
        msg.setData(b);
        mChildHandler.sendMessage(msg);
    }
}
@Override
protected void onDestroy() {
    Log.i("tag", "stop looping the child thread's message queue");
    mChildHandler.getLooper().quit();
    super.onDestroy();
}
class ChildThread extends Thread {
    private static final String INNER_TAG = "ChildThread";
    private boolean running = true;     
    final int maxCount = 10;
    final int minCount = 0;
    public int curCount = minCount;
    private int up = 1;
    public void run() {
        while (true) {
            if (running) {
                try {
                    curCount += up;
                    if (curCount == maxCount)
                        up = -1;
                    else if (curCount == minCount)
                        up = 1;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Log.e("", "local Thread error", e);
                }
                Bundle b = new Bundle(1);
                b.putInt("count", curCount);
                Message toMain = mMainHandler.obtainMessage();
                toMain.what = 2;
                toMain.setData(b);
                mMainHandler.sendMessage(toMain);
            }
            this.setName("child");
            Looper.prepare();
            mChildHandler = new Handler() {
                public void handleMessage(Message msg) {
                    Bundle b;
                    if (msg.what==1){
                        b = msg.getData();
                        running = b.getBoolean("running");
                        Log.i(INNER_TAG, "from main (running) - " + b.getBoolean("running"));
                        Log.i(INNER_TAG, "running - " + running);
                        try {
                            Message toMain = mMainHandler.obtainMessage();
                            toMain.what = 1;
                            toMain.setData(b);
                            mMainHandler.sendMessage(toMain);
                        } finally {}
                    }
                }
            };
            Log.i(INNER_TAG, "Child handler is bound to - " +
                    mChildHandler.getLooper().getThread().getName());
            Looper.loop();
        }
    }
}
}