Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i start (and bind) a remote service in android that is implemented in another app (different package)?

i'm a bit stuck with remote services in android. thing is i implemented a remote service in package "a.b.c" and i want other applications to be able to access this service. i got rid of the whole crappy aidl-stuff and designed the "interface" of the service to work via broadcasted intents. works fine so far...

problem is: how do i get a different application (different package, different project, maybe even a different developer, ...) to start/stop the service?

package d.e.f;

import a.b.c.*;

public class main extends Activity {
    protected ImyService myService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = new Intent(ImyService.class.getName());
        bindService(intent, sConnection, Context.BIND_AUTO_CREATE);
    }

    protected ServiceConnection sConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder binder) {
            wlService = ImyService.Stub.asInterface(binder);
            ServiceConnected = true;
            Toast.makeText(main.this, "service connected", Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName className) {
            wlService = null;
            ServiceConnected = false;
            Toast.makeText(main.this, "service disconnected", Toast.LENGTH_SHORT).show();
        }
    };
}

this will crash my app immediately on startup. what did i do wrong? how will i get this to work?

once it's running, commands and data will be passed via broadcasts. so that should be no real problem...

like image 473
xenonite Avatar asked Jun 19 '10 10:06

xenonite


1 Answers

Step #1: Set up an <intent-filter> for your <service> with an <action> string.

Step #2: Use that action string for the Intent you use with bindService() (e.g., new Intent("this.is.my.custom.ACTION"))

like image 65
CommonsWare Avatar answered Oct 16 '22 05:10

CommonsWare