Android的本地服务样品,bindservice(),和ServiceConnection() [英] Android Local Service Sample, bindservice(), and ServiceConnection()

查看:304
本文介绍了Android的本地服务样品,bindservice(),和ServiceConnection()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此有关问题被问@mnish大约一年前的问题。

I have a question which is related to this question that was asked by @mnish about a year ago.

请看看他的问题和code。他实现了一个ServiceConnection(),并将其传递到bindService()。这是继服务文档上方附近的本地服务示例。

Please have a look at his question and code. He implements a ServiceConnection() and passes it to bindService(). This follows the Local Service Sample in the Service documentation near the top.

我要实现的本地服务样品,所以我想从@mnish问题/答案补充一些细节。在ServiceConnection()@mnish有这条线,混淆我:

I want to implement the Local Service Sample, so I am trying to add some details from @mnish question/answer. In ServiceConnection() @mnish has this line that confuses me:

mService = ILocService.Stub.asInterface(iservice);

我明白@mnish写这code,但没有任何人有任何想法ILocService是任何想法如何,我可能去创造我自己的ILocService?这哪里是构建记录?我需要它?此外哪里为的IBinder值iservice从何而来?

I understand @mnish wrote this code, but does anybody have any idea what ILocService is and any idea how I might go about creating my own ILocService? Where is this construct documented and do I need it? Also where does the value for IBinder iservice come from?

推荐答案

他很可能采用Android接口定义语言(AIDL) http://developer.android.com/guide/developing/tool​​s/aidl.html

He is probably using Android Interface Definition Language (AIDL) http://developer.android.com/guide/developing/tools/aidl.html

因此​​他必须使用服务器端执行类似的存根记载:

Therefore he has to use a stub of the server side implementation like documented:

 // 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方法,其服务绑定到你的活动后,所谓的来临。呼叫bindService得到通过它实现了onServiceConnected方法ServiceConnection。

The iservice reference is coming from the onServiceConnected method which is called after binding the service to your activity. The call bindService gets passed the ServiceConnection which implements the onServiceConnected method.

您不需要IRemoteService.Stub.asInterface(服务)当你实施服务是本地的,那么你可以只投的服务,您的本地服务。

You don't need the "IRemoteService.Stub.asInterface(service)" when your implementation of the service is local, then you can just cast the service to you local service.

本地服务样品做这在服务:

The local service sample does this in the 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类活动:

And this in the Activity in the ServiceConnection class:

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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆