gpt4 book ai didi

android - 在 Binder 接口(interface)中返回服务实例是否安全?

转载 作者:行者123 更新时间:2023-11-29 00:41:44 25 4
gpt4 key购买 nike

我有这样的服务:

public MyService extends Service {

// ...

IBinder binder = new MyBinder();

@Override
public IBinder onBind(Intent intent) {
return binder;
}

public class MyBinder extends Binder {

public MyService getService() {
return MyService.this;
}
}

// ...
}

在 Activity 中,我收到 Binder 从而获得 Service 实例,之后我可以访问它的所有方法。我想知道,这样做安全吗?或者我应该只通过 Binder 接口(interface)与 Service 交互?谢谢!

最佳答案

In Activity I receive Binder thereby get Service instance, and after that I have access to all its methods. I want to know, is it safe to do like that? Or I should interact with Service only through Binder interface?

Binder 是返回的内容,您只需转换为您知道的服务类即可。你这样做的方式是只使用 Binder ......

您完成它的方式通常就是它的完成方式。这是直接取自此处找到的“官方”示例的“本地服务”模式:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html在您的服务类上调用方法的其他方法非常老套(相信我,我以前尝试过)。

例子:

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.
myService = ((MyService.LocalBinder)service).getService();



}

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.

}
};

关于android - 在 Binder 接口(interface)中返回服务实例是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936550/

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