gpt4 book ai didi

Android 本地服务示例、bindservice() 和 ServiceConnection()

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:18 35 4
gpt4 key购买 nike

我有一个与此相关的问题 question @mnish 大约一年前问过这个问题。

请看一下他的问题和代码。他实现了一个 ServiceConnection() 并将其传递给 bindService()。这遵循 Service 中的本地服务示例靠近顶部的文档。

我想实现本地服务示例,因此我尝试添加来自@mnish 问题/答案的一些细节。在 ServiceConnection() @mnish 中有这一行让我感到困惑:

mService = ILocService.Stub.asInterface(iservice);

我知道 @mnish 写了这段代码,但是有没有人知道 ILocService 是什么以及我如何创建自己的 ILocService?这个构造在哪里记录,我需要它吗?另外,IBinder iservice 的值(value)从何而来?

最佳答案

他可能正在使用 Android 接口(interface)定义语言 (AIDL) http://developer.android.com/guide/components/aidl.html

因此,他必须像记录的那样使用服务器端实现的 stub :

 // This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. We are communicating with our
// service through an IDL interface, so get a client-side
// representation of that from the raw service object.
mService = IRemoteService.Stub.asInterface(service);

iservice 引用来自 onServiceConnected 方法,该方法在将服务绑定(bind)到您的 Activity 后调用。调用 bindService 传递给实现 onServiceConnected 方法的 ServiceConnection。

当您在本地实现服务时,您不需要“IRemoteService.Stub.asInterface(service)”,然后您可以将服务转换为本地服务。

本地服务示例在服务中执行此操作:

public class LocalService extends Service {
private NotificationManager mNM;

// Unique Identification Number for the Notification.
// We use it on Notification start, and to cancel it.
private int NOTIFICATION = R.string.local_service_started;

/**
* Class for clients to access. Because we know this service always
* runs in the same process as its clients, we don't need to deal with
* IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}

...

}

在 ServiceConnection 类的 Activity 中:

private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBoundService = ((LocalService.LocalBinder)service).getService();

// Tell the user about this for our demo.
Toast.makeText(Binding.this, R.string.local_service_connected,
Toast.LENGTH_SHORT).show();
}

public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mBoundService = null;
Toast.makeText(Binding.this, R.string.local_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};

关于Android 本地服务示例、bindservice() 和 ServiceConnection(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6399274/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com