Note that there are some explanatory texts on larger screens.

plurals
  1. POBest option to implement Networking Class
    primarykey
    data
    text
    <p>I am starting a project that will access a Xmpp server in the background. It will maintain the connection alive and reconnect when required + do other Xmpp things.</p> <p>I want to implement a class to do the work. The class will have to interact with other <code>Services</code> (<code>Location</code>...), and <code>BroadcastReceivers</code> (CONNECTIVITY_CHANGE ....). Basically, Activities and Broadcast receivers will ask the Xmpp Class to start an action like: CONNECT, DISCONNECT, RECONNECT, JOIN CHAT, SEND MESSAGE etc.</p> <p>First approach is to implement it as a <code>Service</code> but a service runs in the main thread so the implementation is wrong.</p> <p>Secondly, I wanted to make it as an <code>IntentService</code> because the <code>onHandleIntent</code> is run async and then I am out of the main thread.</p> <p>But the <code>onHandleIntent</code> is run only once to perform an async task. So, if I want an <code>Activity</code> to perform another "action", I can only send a broadcast event and I will fall in the main thread problem again. Also, <code>IntentService</code> is not really aimed to be 'live' all the time.</p> <p>In the Google documentation, they say you need to run <code>AsyncTask</code> for each network access ... is this the only way to make a network access ... this is pretty sad.</p> <p>I had a look on the implementation in the GTalkSMS and they seemed to have the same problem. Actually they use a <code>Service</code> with a <code>ServiceHandler</code> management like this:</p> <blockquote> <pre><code>// some stuff for the async service implementation - borrowed heavily from // the standard IntentService, but that class doesn't offer fine enough // control for "foreground" services. private static volatile Looper sServiceLooper; private static volatile ServiceHandler sServiceHandler; private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { onHandleIntent((Intent) msg.obj, msg.arg1); } } </code></pre> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload