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

Android本地服务示例、bindservice()和ServiceConnection(),android,android-service,stub,android-binder,Android,Android Service,Stub,Android Binder,一年前,@mnish向我提出了一个与此相关的问题 请看一下他的问题和代码。他实现了ServiceConnection()并将其传递给bindService()。这遵循顶部附近文档中的本地服务示例 我想实现本地服务示例,因此我尝试从@mnish question/answer添加一些细节。在ServiceConnection()@mnish中,有一行让我感到困惑: mService = ILocService.Stub.asInterface(iservice); 我知道@mnish写了这段代码

一年前,@mnish向我提出了一个与此相关的问题

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

我想实现本地服务示例,因此我尝试从@mnish question/answer添加一些细节。在ServiceConnection()@mnish中,有一行让我感到困惑:

mService = ILocService.Stub.asInterface(iservice);

我知道@mnish写了这段代码,但有人知道什么是ILocService,知道我如何创建自己的ILocService吗?这个构造在哪里有文档记录,我需要它吗?IBinder iSeries的价值从何而来?

他可能正在使用Android界面定义语言(AIDL)

因此,他必须使用服务器端实现的存根,如文档所示:

 // 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会传递ServiceConnection,ServiceConnection实现OnServiceConnection方法

当服务的实现是本地的时,您不需要“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类的活动中:

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

给你,我的例子。。这会让你明白这个LOL

// My MyServiceInterface.aidl
package com.mad.exam;

interface MyServiceInterface {
     int getNumber();
}

//MyService 
public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Service OnBind()", Toast.LENGTH_LONG).show();
        return mBinder;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed ", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Toast.makeText(this, "Service Started ", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        return super.onUnbind(intent);
    }

    private final MyServiceInterface.Stub mBinder = new MyServiceInterface.Stub() {
        public int getNumber() {
            return new Random().nextInt(100);
        }
    };
}

//My Activity 
public class ServiceDemo extends Activity implements OnClickListener {
    MyServiceInterface mService;
    ServiceConnection mConnection;
    Button retreive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service);
        retreive = (Button) findViewById(R.id.retreive);
        retreive.setOnClickListener(this);

        mConnection = new ServiceConnection() {

            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub
            }

            public void onServiceConnected(ComponentName name, IBinder service) {
                // TODO Auto-generated method stub
                mService = MyServiceInterface.Stub.asInterface(service);

                try {
                    int i;
                    i = mService.getNumber();
                    Toast.makeText(ServiceDemo.this, "The service value is: " + String.valueOf(i), Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("My Tag", "Clicked");
        Button btn = (Button) v;
        Intent callService = new Intent(this, MyService.class);
        bindService(callService, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Intent callService = new Intent(this, MyService.class);
        bindService(callService, mConnection, Context.BIND_AUTO_CREATE);
    }
}

谢谢,我要试一试。但正如我前面提到的,有一个将ServiceConnection()和IBinder传递给bindService()的函数,我认为它不使用AIDL。也许@mnish正在使用AIDL,就像你说的,存根的原因。对不起,我不明白。你补充了什么?本地服务示例的LocalBinder?不,已经有了。服务?不,已经有了。很抱歉这么迟钝,但我不明白你说的“添加了样本发挥作用的部分”是什么意思。你是否编辑了你的答案,而我只是看不到?是的,我在LocalService中添加了本地绑定器,绑定器的作用与存根对远程服务的作用相同。但你现在的问题到底在哪里?我发现实施起来还不够。但我会继续找的。感谢您回答我问题的原意。如果服务未运行而您尚未启动,该怎么办?这些支票在哪里?