/*********************************************************************************************************************/
語言:AIDL語言,即android interface definition language
interface ICat
{
String getColor();
double getWeight();
}
/**
*
*/
package org.crazyit.service;
import java.util.Timer;
import java.util.TimerTask;
import org.crazyit.service.ICat.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
/**
* Description:
* <br/>網站: <a href="http://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee [email protected]
* @version 1.0
*/
public class AidlService extends Service
{
private CatBinder catBinder;
Timer timer = new Timer();
String[] colors = new String[]{
"紅色",
"黃色",
"黑色"
};
double[] weights = new double[]{
2.3,
3.1,
1.58
};
private String color;
private double weight;
// 繼承Stub,也就是實現額ICat接口,並實現了IBinder接口
public class CatBinder extends Stub
{
@Override
public String getColor() throws RemoteException
{
return color;
}
@Override
public double getWeight() throws RemoteException
{
return weight;
}
}
@Override
public void onCreate()
{
super.onCreate();
catBinder = new CatBinder();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
// 隨機地改變Service組件內color、weight屬性的值。
int rand = (int)(Math.random() * 3);
color = colors[rand];
weight = weights[rand];
System.out.println("--------" + rand);
}
} , 0 , 800);
}
@Override
public IBinder onBind(Intent arg0)
{
/* 返回catBinder對象
* 在綁定本地Service的情況下,該catBinder對象會直接
* 傳給客戶端的ServiceConnection對象
* 的onServiceConnected方法的第二個參數;
* 在綁定遠程Service的情況下,只將catBinder對象的代理
* 傳給客戶端的ServiceConnection對象
* 的onServiceConnected方法的第二個參數;
*/
return catBinder; //①
}
@Override
public void onDestroy()
{
timer.cancel();
}
}
package org.crazyit.client;
import org.crazyit.service.ICat;
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.EditText;
/**
* Description: <br/>
* site: <a href="http://www.crazyit.org">crazyit.org</a> <br/>
* Copyright (C), 2001-2012, Yeeku.H.Lee <br/>
* This program is protected by copyright laws. <br/>
* Program Name: <br/>
* Date:
*
* @author Yeeku.H.Lee [email protected]
* @version 1.0
*/
public class AidlClient extends Activity
{
private ICat catService;
private Button get;
EditText color, weight;
private ServiceConnection conn = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
// 獲取遠程Service的onBind方法返回的對象的代理
catService = ICat.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name)
{
catService = null;
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
get = (Button) findViewById(R.id.get);
color = (EditText) findViewById(R.id.color);
weight = (EditText) findViewById(R.id.weight);
// 創建所需綁定的Service的Intent
Intent intent = new Intent();
intent.setAction("org.crazyit.aidl.action.AIDL_SERVICE");
// 綁定遠程Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
get.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
try
{
// 獲取、並顯示遠程Service的狀態
color.setText(catService.getColor());
weight.setText(catService.getWeight() + "");
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
});
}
@Override
public void onDestroy()
{
super.onDestroy();
// 解除綁定
this.unbindService(conn);
}
}
/**
*
*/
package org.crazyit.service;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Description:
* <br/>網站: <a href="http://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee [email protected]
* @version 1.0
*/
public class Person implements Parcelable
{
private Integer id;
private String name;
private String pass;
public Person()
{
}
public Person(Integer id, String name, String pass)
{
super();
this.id = id;
this.name = name;
this.pass = pass;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPass()
{
return pass;
}
public void setPass(String pass)
{
this.pass = pass;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((pass == null) ? 0 : pass.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null)
{
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
if (pass == null)
{
if (other.pass != null)
return false;
}
else if (!pass.equals(other.pass))
return false;
return true;
}
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
//把該對象所包含的數據寫到Parcel
dest.writeInt(id);
dest.writeString(name);
dest.writeString(pass);
}
// 添加一個靜態成員,名爲CREATOR,該對象實現了Parcelable.Creator接口
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>()
{
@Override
public Person createFromParcel(Parcel source)
{
// 從Parcel中讀取數據,返回Person對象
return new Person(source.readInt(), source.readString(), source.readString());
}
@Override
public Person[] newArray(int size)
{
return new Person[size];
}
};
}
}
/**
*
*/
package org.crazyit.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.crazyit.service.IPet.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
/**
* Description:
* <br/>網站: <a href="http://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee [email protected]
* @version 1.0
*/
public class ComplexService extends Service
{
private PetBinder petBinder;
private static Map<Person , List<Pet>> pets = new HashMap<Person , List<Pet>>();
static
{
// 初始化pets Map集合
ArrayList<Pet> list1 = new ArrayList<Pet>();
list1.add(new Pet("旺財" , 4.3));
list1.add(new Pet("來福" , 5.1));
pets.put(new Person(1, "sun" , "sun") , list1);
ArrayList<Pet> list2 = new ArrayList<Pet>();
list2.add(new Pet("kitty" , 2.3));
list2.add(new Pet("garfield" , 3.1));
pets.put(new Person(2, "bai" , "bai") , list2);
}
// 繼承Stub,也就是實現額IPet接口,並實現了IBinder接口
public class PetBinder extends Stub
{
@Override
public List<Pet> getPets(Person owner) throws RemoteException
{
// 返回Service內部的數據
return pets.get(owner);
}
}
@Override
public void onCreate()
{
super.onCreate();
petBinder = new PetBinder();
}
@Override
public IBinder onBind(Intent arg0)
{
/* 返回catBinder對象
* 在綁定本地Service的情況下,該catBinder對象會直接
* 傳給客戶端的ServiceConnection對象
* 的onServiceConnected方法的第二個參數;
* 在綁定遠程Service的情況下,只將catBinder對象的代理
* 傳給客戶端的ServiceConnection對象
* 的onServiceConnected方法的第二個參數;
*/
return petBinder; //①
}
@Override
public void onDestroy()
{
}
}
package org.crazyit.client;
import java.util.List;
import org.crazyit.service.IPet;
import org.crazyit.service.Person;
import org.crazyit.service.Pet;
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.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
/**
* Description: <br/>
* site: <a href="http://www.crazyit.org">crazyit.org</a> <br/>
* Copyright (C), 2001-2012, Yeeku.H.Lee <br/>
* This program is protected by copyright laws. <br/>
* Program Name: <br/>
* Date:
* @author Yeeku.H.Lee [email protected]
* @version 1.0
*/
public class ComplexClient extends Activity
{
private IPet petService;
private Button get;
EditText personView;
ListView showView;
private ServiceConnection conn = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
// 獲取遠程Service的onBind方法返回的對象的代理
petService = IPet.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name)
{
petService = null;
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
personView = (EditText) findViewById(R.id.person);
showView = (ListView) findViewById(R.id.show);
get = (Button) findViewById(R.id.get);
// 創建所需綁定的Service的Intent
Intent intent = new Intent();
intent.setAction("org.crazyit.aidl.action.COMPLEX_SERVICE");
// 綁定遠程Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
get.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
try
{
String personName = personView.getText().toString();
// 調用遠程Service的方法
List<Pet> pets = petService.getPets(new Person(1,personName, personName)); //①
// 將程序返回的List包裝成ArrayAdapter
ArrayAdapter<Pet> adapter = new ArrayAdapter<Pet>(ComplexClient.this,android.R.layout.simple_list_item_1, pets);
showView.setAdapter(adapter);
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
});
}
@Override
public void onDestroy()
{
super.onDestroy();
// 解除綁定
this.unbindService(conn);
}
}
完整代碼鏈接 代碼下載