how do i start (and bind) a remote service in andr

2019-04-24 02:17发布

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...

1条回答
再贱就再见
2楼-- · 2019-04-24 02:55

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"))

查看更多
登录 后发表回答