代码之家  ›  专栏  ›  技术社区  ›  xenonite

如何在Android中启动(和绑定)在另一个应用程序(不同的包)中实现的远程服务?

  •  8
  • xenonite  · 技术社区  · 16 年前

    我有点困在安卓系统的远程服务上。我在包“a.b.c”中实现了一个远程服务,我希望其他应用程序能够访问这个服务。我摆脱了所有糟糕的AIDL内容,设计了服务的“接口”,通过广播的意图工作。到目前为止工作正常…

    问题是:我怎样才能得到 different 启动/停止服务的应用程序(不同的包、不同的项目、甚至可能是不同的开发人员…)

    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();
            }
        };
    }
    

    这将使我的应用程序在启动时立即崩溃。我做错了什么?我怎样才能让这个工作?

    一旦它运行起来,命令和数据将通过广播传递。所以这应该不是真正的问题…

    1 回复  |  直到 16 年前
        1
  •  20
  •   CommonsWare    16 年前

    步骤1:设置 <intent-filter> 为了你 <service> 用一个 <action> 字符串。

    步骤2:将该操作字符串用于 Intent 你用 bindService() (例如, new Intent("this.is.my.custom.ACTION") )