0

Android 3.1 タブレット アプリケーションを開発しています。

このアプリはスレッドを実行します。そのサービスで、私はこのコードを持っています:

public class UDPSocketBackgroundService extends Service
{

@Override
public void onCreate()
{
    super.onCreate();
    Log.v(TAG, "in onCreate()");

    // xxxxxx
    Looper.prepare();
    mServiceHandler = new Handler() {
        /**
         * 
         */
        public void handleMessage(Message msg)
        {
            DatagramPacket packet = (DatagramPacket) msg.obj;
            //String received = new String(packet.getData(), 0, packet.getLength());

            Message backMsg = Message.obtain();
            backMsg.arg1 = Activity.RESULT_OK;;

            Bundle bundle = new Bundle();
            bundle.putByteArray("CLIENT_DATA", packet.getData());
            bundle.putString("CLIENT_HOST", packet.getAddress().getHostAddress());
            bundle.putString("CLIENT_PORT", new Integer(packet.getPort()).toString());
            backMsg.setData(bundle);

            try
            {
                outMessenger.send(backMsg);
            }
            catch (android.os.RemoteException e1)
            {
                Log.w(getClass().getName(), "Exception sending message", e1);
            }
        }
    };
    Looper.loop();
}

しかし、理由はわかりません(サービスを初めて使用するため)ここで例外が発生します:

Looper.prepare();

アップデート

サービスの onBind イベント:

public IBinder onBind(Intent intent)
{
    Bundle extras = intent.getExtras();
    // Get messager from the Activity
    if (extras != null) {
        outMessenger = (Messenger) extras.get("MESSENGER");
    }

    try
    {
        myThread = new UDPServerThread("X", 8888, mServiceHandler);
        myThread.start();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return mMessenger.getBinder();
}

これは、メインアクティビティからサービスを開始する方法です:

protected void onResume()
{
    super.onResume();

    Intent intent = null;
    intent = new Intent(this, UDPSocketBackgroundService.class);
    // Create a new Messenger for the communication back
    // From the Service to the Activity
    Messenger messenger = new Messenger(handler);
    intent.putExtra("MESSENGER", messenger);

    bindService(intent, conn, Context.BIND_AUTO_CREATE);
}

私は何を間違っていますか?

4

3 に答える 3

3

1. Service doesn't run on Background thread, but on the Dedicated UI thread.

2. It was considered good practice to have UI work on UI thread, and Non-UI work on Non-UI thread, but from HoneyComb it became a rule.

3. So for your background thread use Thread will Handler, or AsyncTask.

4. Non-UI work are executed on the foreground using Handler, which in turn requires UI thread to call Looper.loop() to get the actual messages serviced, So there is no need to explicitly call Looper.loop() or prepare().

于 2012-07-04T07:41:14.313 に答える
1

サービスは UI スレッドで実行され、UI スレッドには既にルーパーがあるため、そのエラーが発生しています。

バックグラウンド スレッドで作業を行う場合は、実行するコード用に新しいスレッドを作成する必要がある場合があります。

たぶん、 HandlerThreadを調べることができます。

于 2012-07-04T07:21:10.877 に答える
1

まず、Serviceバックグラウンド スレッドで実行されないため、バックグラウンド作業にはThread、 、AsyncTaskまたは のいずれかを使用する必要があります。IntentService次に、Serviceすでに Looper が初期化されているため、コードからLooper.prepare()andを削除する必要がありLooper.loop()ます。

于 2012-07-04T07:24:07.217 に答える