Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intentService : why my onHandleIntent is never called?

Tags:

java

android

I'm working with android xml rpc to mount a server. For that I'm using and intentService. The only problem is that when the server class is launched, my onHandleIntent which contains the server is never called.

I've made some research and I found someone who had the same problem, he managed solving it by using super class but I'm new in programming and didn't manage to do what he did ==> link

Here is my code:

package tfe.rma.ciss.be;  import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xmlpull.v1.XmlPullParserException; import org.xmlrpc.android.MethodCall; import org.xmlrpc.android.XMLRPCServer;  import android.app.IntentService; import android.content.Intent; import android.util.Log; import android.widget.Toast;  import java.io.IOException; import java.io.StringReader; import java.net.MalformedURLException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList;  import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory;  public class Server extends IntentService {    public String myData="";    public String streamTitle = "",path="";     public void onCreate() {          Log.d("Server", ">>>onCreate()");      }      public Server() {         super("Server");       }     public void onStart (Intent intent, int startId) {         Log.d("Server", ">>>Started()");    }     @Override     protected void onHandleIntent(Intent intent) {         Log.d("Server", ">>>handlingIntent()");         try {             ServerSocket socket = new ServerSocket(8214);             XMLRPCServer server = new XMLRPCServer();             Log.d("Server", ">>>opening on port" + socket);             while (true) {                 Socket client = socket.accept();                 MethodCall call = server.readMethodCall(client);                 String name = call.getMethodName();                 if (name.equals("newImage")) {                     ArrayList<Object> params = call.getParams();                     // assume "add" method has two Integer params, so no checks done                    myData = (String)( params.get(0));                     //int i1 = (Integer) params.get(1);                     server.respond(client, new Object[] {200});                     /*intent = new Intent (this, ParseFunction.class);                  startService (intent);  */                      Toast.makeText(this, myData, Toast.LENGTH_SHORT).show();                       Log.d("ParseFunction", ">>>Started()");                       Intent i = new Intent( this, B.class );                      i.putExtra( "Azo", myData);                      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                     startActivity( i );                  } else {                     server.respond(client, null);                 }             }         } catch (IOException e) {             e.printStackTrace();         } catch (XmlPullParserException e) {             e.printStackTrace();         }      }    } 
like image 937
youssoua Avatar asked Jan 31 '12 13:01

youssoua


People also ask

Why IntentService is deprecated?

This class was deprecated in API level 30. IntentService is subject to all the background execution limits imposed with Android 8.0 (API level 26). Consider using WorkManager or JobIntentService , which uses jobs instead of services when running on Android 8.0 or higher.

Does IntentService run on background thread?

The Service runs in background but it runs on the Main Thread of the application. The IntentService runs on a separate worker thread.


2 Answers

If you got here and nothing worked, check your manifest looks like this:

    <service android:name=".subpackage.ServiceClassName" >     </service> 

And not like this:

    <service android:name=".subpackage.ServiceClassName" /> 

There's a problem with xml closing tags. The first one works. The second is legal but doesn't work.

like image 150
Mister Smith Avatar answered Sep 19 '22 21:09

Mister Smith


In case someone else wants the result here is what I should have done. Adding superclass to onCreate super.onCreate() and change onStart by onStartCommand (plus its superclass super.onStartCommand()), now it works as a charm

package tfe.rma.ciss.be;  import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xmlpull.v1.XmlPullParserException; import org.xmlrpc.android.MethodCall; import org.xmlrpc.android.XMLRPCServer;  import android.app.IntentService; import android.content.Intent; import android.util.Log; import android.widget.Toast;  import java.io.IOException; import java.io.StringReader; import java.net.MalformedURLException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList;  import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory;  public class Server extends IntentService {     public String myData="";     public String streamTitle = "",path="";      public void onCreate() {         super.onCreate();         Log.d("Server", ">>>onCreate()");     }      public Server() {         super("Server");     }      @Override     public int onStartCommand(Intent intent, int flags, int startId) {         super.onStartCommand(intent, startId, startId);         Log.i("LocalService", "Received start id " + startId + ": " + intent);          return START_STICKY;     }      @Override     protected void onHandleIntent(Intent intent) {         Log.d("Server", ">>>handlingIntent()");         try {             ServerSocket socket = new ServerSocket(8214);             XMLRPCServer server = new XMLRPCServer();             Log.d("Server", ">>>opening on port" + socket);              while (true) {                 Socket client = socket.accept();                 MethodCall call = server.readMethodCall(client);                 String name = call.getMethodName();                  if (name.equals("newImage")) {                     ArrayList<Object> params = call.getParams();                     // assume "add" method has two Integer params, so no checks done                     myData = (String)( params.get(0));                     //int i1 = (Integer) params.get(1);                     server.respond(client, new Object[] {200});                     /*intent = new Intent (this, ParseFunction.class);                     startService (intent);  */                      Toast.makeText(this, myData, Toast.LENGTH_SHORT).show();                       Log.d("ParseFunction", ">>>Started()");                       Intent i = new Intent( this, B.class );                     i.putExtra( "Azo", myData);                     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                     startActivity( i );                 } else {                     server.respond(client, null);                 }             }         } catch (IOException e) {             e.printStackTrace();         } catch (XmlPullParserException e) {             e.printStackTrace();         }     } } 
like image 36
youssoua Avatar answered Sep 19 '22 21:09

youssoua