Android 在Binder接口中返回服务实例是否安全?

Android 在Binder接口中返回服务实例是否安全?,android,android-service,Android,Android Service,我有这样的服务: public MyService extends Service { // ... IBinder binder = new MyBinder(); @Override public IBinder onBind(Intent intent) { return binder; } public class MyBinder extends Binder { public MyService

我有这样的服务:

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

    // ...
}   
在活动中,我接收Binder,从而获得服务实例,之后我可以访问它的所有方法。我想知道,这样做安全吗?或者我应该只通过活页夹接口与服务交互?谢谢大家!

在活动中,我接收绑定器,从而获取服务实例,然后 我可以使用它的所有方法。我想知道,这样做安全吗 你喜欢吗?或者我应该只通过活页夹与服务交互 接口

绑定器是返回的对象,您只需将其转换为您知道的服务类。你这样做的方式只是使用活页夹

你做这件事的方式通常就是如何做的。这是直接从这里找到的“官方”示例中获取的“本地服务”模式:在服务类上调用方法的其他方法都非常粗糙(相信我,我以前也尝试过)

例如:

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.

        }
    };

非常感谢你!现在我明白了。