2、从客户端应用程序调用服务(摘自ProAndroid2)

2、客户端

(1)要实现将Person对象传递给服务的客户端,将客户端需要的所有内容从服务项目复制到客户端项目。所需要的是:IStockQuoteService.aidl、Person.java和Person.aidl文件。

(2)MainActivity.java:使用bindService和unbindService来连接和挂断服务。由于bindService是异步的,所以android平台提供了ServiceConnection回调。

package com.sayed;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.syh.IStockQuoteService;
import com.syh.Person;

public class MainActivity extends Activity {
    protected static final String TAG = "StockQuoteClient";
    private IStockQuoteService stockService = null;
    private Button bindBtn;
    private Button callBtn;
    private Button unbinBtn;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bindBtn = (Button)findViewById(R.id.bindBtn);
        bindBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getApplicationContext().bindService(new Intent(IStockQuoteService.class.getName()), serCon, Context.BIND_AUTO_CREATE);
                bindBtn.setEnabled(false);
                callBtn.setEnabled(true);
                unbinBtn.setEnabled(true);
            }
        });
        callBtn = (Button)this.findViewById(R.id.callAgainBtn);
        callBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                callService();
            }
        });
        callBtn.setEnabled(false);
        
        unbinBtn = (Button)this.findViewById(R.id.unbindBtn);
        unbinBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                getApplicationContext().unbindService(serCon);
                bindBtn.setEnabled(true);
                callBtn.setEnabled(true);
                unbinBtn.setEnabled(false);
            }
        });
        unbinBtn.setEnabled(false);
    }
    
    private ServiceConnection serCon = new ServiceConnection(){
        public void onServiceConnected(ComponentName name, IBinder service){
            Log.v(TAG, "On ServiceConnected() called");
            stockService = IStockQuoteService.Stub.asInterface(service);
            callService();
        }
        
        @Override
        public void onServiceDisconnected(ComponentName name){
            Log.v(TAG, "OnDisServiceDisconnected() called");
            stockService = null;
        }
    };
    private void callService(){
        try{
            Person person = new Person();
            person.setAge(23);
            person.setName("wangle");
            String val = stockService.getQuote("SYH", person);
            Toast.makeText(MainActivity.this, "Value from service is " + val, Toast.LENGTH_LONG).show();
        }catch(RemoteException e){
            Log.e(TAG, e.getMessage(), e);
        }
    }
}

(3)main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button android:id="@+id/bindBtn" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bind"/>
    <Button android:id="@+id/callAgainBtn" 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Call Again"/>
    <Button android:id="@+id/unbindBtn" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UnBind"/>
</LinearLayout>

(4)AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sayed"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 
         <service android:name="StockQuoteService2">
            <intent-filter>
                <action android:name="com.syh.IStockQuoteService"/>
            </intent-filter>
        </service>
         -->
    </application>

</manifest>
原文地址:https://www.cnblogs.com/wangle1001986/p/2651673.html