What happens if I call startService after calling bindService in the service class?

I am calling bindService in Service MessengerService. It is working fine. After that I call startService.

The code is exactly the same as in this link. An example of a remote message service example is http://developer.android.com/reference/android/app/Service.html except that I add startService to the action

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

bindService(intnt, mConnection, Context.BIND_AUTO_CREATE); intnt.putExtra("msg", "String from activity to service to handler 22"); startService(intnt); 

In the service code: In onStartCommand, any message that I receive in an intent that is sent to startService, I send it back to the client handler.

I get the index from the associated exception in the line mClients.get (0) .send (msg1). mClients is an array of clients connected to this service and stored during the binding process.

The code is exactly the same as in this link. An example of the remote message service example http://developer.android.com/reference/android/app/Service.html except that I add onStartCommand to the 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
source share
1 answer

You can find the answer to the question here .

Service Life Cycle Diagram

In any particular order, onStartCommand () and onBind () are called

I was looking for an answer that is surprisingly hard to find when I asked your question, so I post it as others may find it useful.

+1
source

Source: https://habr.com/ru/post/1486811/


All Articles