0

I am getting confused with all the different terminology when using Android: Activity, Service...

Right now I create a service:

startService(new Intent(this, RingerServer.class));

And this service starts a thread:

public class RingerServer extends Service {

    public void onCreate() {
        super.onCreate();

        new Thread(new Ringer()).start();
    }

    public class Ringer implements Runnable { ... }

    public void refuseConnection() { ... }

}

In this service, the RingerServer, I also have methods that I want to use. I would like to keep a reference to the RingerServer. I would basically like the Activity that created the service to be able to call refuseConnection(), but not make that method static.

startService returns a ComponentName, so I've been trying to cast it back to RingerServer but that doesn't seem to work. I see that it has getClass() and I've checked and getClassName() gives me the correct class. I haven't been able to use getClass() properly though.

Is there any way I can please keep a reference to the newly created RingerServer class? I am sure this is trivial, but I am stuck right now.

Thank you very much,

James

James
  • 213
  • 1
  • 4
  • 8

2 Answers2

0

You have two options

1.Override onStartCommand of the service and start the server with intent using an action. that intent will be received in service, based on the intent action you can call refuseConnection()

//In Activity 
...
//Start the service 
Intent intent=new Intent("com.xx.xx.REFUSE_CONNECTION");
startService(this,intent);
...
//In Service 

public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        if(intent.getAction().equals("com.xx.xx.REFUSE_CONNECTION")){
            //Refuse the connection
            refuseConnection();
        }else {
            //Do something else
        }
}

//In Manifest

<service android:name="RingerService">
     <intent-filter>
       <action android:name="com.xx.xx.REFUSE_CONNECTION"></action>
     </intent-filter>
</service>
  1. Implement AIDL interface and override onBind() of service , and use this interface to call refuseConnection(). Refer to this link http://developer.android.com/guide/developing/tools/aidl.html regarding AIDL.
Suresh
  • 9,495
  • 14
  • 49
  • 63
  • Thank you, I really like your first solution. However, startService complains, telling me it only take one argument, the intent. I am not supposed to specify RingerServer.class as well please? – James Nov 29 '10 at 06:54
  • yes, don't specify the component for intent, just give the action. Make sure you define the intent filter with that action in the manifest for the service. – Suresh Nov 29 '10 at 07:06
  • I also modified my AndroidManifest.XML, but it is still not working: – James Nov 29 '10 at 07:08
  • I am not sure what I am doing wrong, when calling: Intent intent = new Intent("com.voip.rudy.START_SERVICE"); startService(intent); – James Nov 29 '10 at 07:09
  • And for the service class: public void onStart(Intent intent, int startId) { super.onStart(intent, startId); if(intent.getAction() == "com.voip.rudy.START_SERVICE") { new Thread(new Ringer()).start(); } – James Nov 29 '10 at 07:09
  • My issue is the onStart never actually gets executed right now, I am not sure why. Thanks! – James Nov 29 '10 at 07:12
  • Manifest is wrong, intent filter is required, check the updated answer – Suresh Nov 29 '10 at 07:19
  • Thanks a lot! It works! I just had to change "==" to equals(). Thanks! – James Nov 29 '10 at 07:33
0

You can use a ServiceConnection to get access to your service class. See sample code here:

Android service running after pressing Home key

That said, managing things via the service's onStart handler is much simpler.

Community
  • 1
  • 1
Darrell
  • 1,945
  • 15
  • 21