轉載:http://liangruijun.blog.51cto.com/3061169/653344/
- JAVA基本數據類型不需要導入
- String,List,Map和CharSequence不需要導入
- package com.android.aidl;
- //BookInfo接口
- interface BookInfo{
- void setName(String name);
- void setPrice(int price);
- void ssetPublish(String pname);
- //顯示圖書的信息
- String display();
- }
- package com.android.aidl;
- import android.os.RemoteException;
- public class BookInfoImpl extends BookInfo.Stub {
- //聲明三個個變量
- private int price;
- private String name,pname;
- //顯示書名,價格,出版社
- public String display() throws RemoteException{
- return "書名:"+name+";價格:"+price+";出版社:"+price;
- }
- @Override
- //設置書名
- public void setName(String name) throws RemoteException {
- // TODO Auto
- this.name= name;
- }
- @Override
- //設置價格
- public void setPrice(int price) throws RemoteException {
- // TODO Auto-generated method stub
- this.price = price;
- }
- @Override
- //設置出版社
- public void setPublish(String pname) throws RemoteException {
- // TODO Auto
- this.pname= pname;
- }
- }
- package com.android.aidl;
- import com.android.aidl.BookInfo.Stub;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- public class RemoteService extends Service {
- //聲明BookInfo接口
- private Stub bookifo = new BookInfoImpl();
- public IBinder onBind(Intent intent){
- return bookifo;
- }
- }
- package com.android.aidl;
- import android.app.Activity;
- import android.app.Service;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- // 聲明IPerson接口
- private BookInfo bookInfo;
- // 聲明 Button
- private Button btn;
- // 實例化ServiceConnection
- private ServiceConnection conn = new ServiceConnection() {
- @Override
- synchronized public void onServiceConnected(ComponentName name, IBinder service) {
- // 獲得IPerson接口
- bookInfo = BookInfo.Stub.asInterface(service);
- if (bookInfo != null)
- try {
- // RPC 方法調用
- bookInfo.setName("Google Android SDK開發範例大全");
- bookInfo.setPrice(55);
- bookInfo.setPublish("人民郵電出版社");
- String msg = bookInfo.display();
- // 顯示方法調用返回值
- Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG)
- .show();
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 設置當前視圖佈局
- setContentView(R.layout.main);
- // 實例化Button
- btn = (Button) findViewById(R.id.Button1);
- //爲Button添加單擊事件監聽器
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 實例化Intent
- Intent intent = new Intent();
- // 設置Intent Action 屬性
- intent.setAction("com.android.aidl.action.MY_REMOTE_SERVICE");
- // 綁定服務
- bindService(intent, conn, Service.BIND_AUTO_CREATE);
- }
- });
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:text="遠程調用Service"
- android:id="@+id/Button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.aidl"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="10" />
- <application android:icon="@drawable/icon" 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="RemoteService">
- <intent-filter>
- <action android:name="com.android.aidl.action.MY_REMOTE_SERVICE"/>
- </intent-filter>
- </service>
- </application>
- </manifest>