5

Service MessengerService で bindService を呼び出しています。それは正常に動作します。その後、startService を呼び出します。

The code is exactly same as this link Remote messenger service example section http://developer.android.com/reference/android/app/Service.html except I add a startService in activity

This is client code: Intent intnt = new Intent(context, MessengerService.class); intnt.putExtra("msg", "String from activity to service to handler 11");

    bindService(intnt, mConnection, Context.BIND_AUTO_CREATE);

    intnt.putExtra("msg", "String from activity to service to handler 22");     

    startService(intnt);

In Service code: In onStartCommand, whatever message i receive in intent which is passed in startService, I send it back to client handler.

I am getting index out of bound exception in line mClients.get(0).send(msg1). mClients is the array of clients attached to this service and stored during binding process.

The code is exactly same as this link Remote messenger service example section http://developer.android.com/reference/android/app/Service.html except I am adding a onStartCommand in Service

@Override
public int onStartCommand(Intent intent, int flags, int startId){

    String str = intent.getStringExtra("msg");
    Message msg1 = Message.obtain(null, MSG_STR_VALUE);
    Bundle data = new Bundle();
    data.putString("message", str);
    msg1.setData(data);

    System.out.println(str);
    try {
        s1.acquire();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        mClients.get(0).send(msg1);
    } catch (RemoteException e) {
        e.printStackTrace();
    }

    return START_STICKY;
}
4

2 に答える 2

3

質問への回答はこちらでご覧いただけます

サービス ライフサイクルのフローチャート

onStartCommand() と onBind() は順不同で呼び出されます。

私は自分で答えを探していましたが、あなたの質問投稿に出くわしたとき、驚くほど見つけにくかったので、他の人が役に立つと思うかもしれないので投稿します。

于 2016-11-11T08:31:59.203 に答える