Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用服务_Android - Fatal编程技术网

Android 如何使用服务

Android 如何使用服务,android,Android,我需要一个服务,运行在不同的进程和启动启动 我的应用程序也应该与此进行通信 另外,我不想让其他应用程序使用我的服务 有办法吗 private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { service = ((BoxService.MyBinder) b

我需要一个服务,运行在不同的进程和启动启动

我的应用程序也应该与此进行通信

另外,我不想让其他应用程序使用我的服务

有办法吗

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder binder) {
        service = ((BoxService.MyBinder) binder).getService();
        Toast.makeText(mainActivity.this, "Connected", Toast.LENGTH_SHORT)
                .show();
    }

    public void onServiceDisconnected(ComponentName className) {
        service = null;
    }
};

void doBindService() {
    bindService(new Intent(this, BoxService.class), mConnection,
            Context.BIND_AUTO_CREATE);
}
当我在我的进程中使用服务时,这很有效,但当我想在不同的进程中使用服务时,我会在绑定中出错

我能做什么?

请参阅此链接

AddFenceActivity.this.startService(new Intent(
                    AddFenceActivity.this, Location_Services.class));