1、服务端:
(1)Person类:
package com.syh; import android.os.Parcel; import android.os.Parcelable; public class Person implements Parcelable{ private int age; private String name; public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() { @Override public Person createFromParcel(Parcel in){ return new Person(in); } @Override public Person[] newArray(int size){ return new Person[size]; } }; public Person(){ } private Person(Parcel in){ readFromParcel(in); } public int describeContents(){ return 0; } public void writeToParcel(Parcel out, int flags){ out.writeInt(age); out.writeString(name); } public void readFromParcel(Parcel in){ this.age = in.readInt(); this.name = in.readString(); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
(2)Person.aidl文件:要求于Person.java在同一个文件夹下
package com.syh; parcelable Person;
(3)服务类StockQuoteService2.java:用到的Notification和PendingIntent,第一次用android手机调试,才明白“通知”和“即将发生的意图”的意思
package com.syh; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class StockQuoteService2 extends Service{ private NotificationManager notificationMgr; public class StockQuoteService2Impl extends IStockQuoteService.Stub{ public String getQuote(String ticker, Person requester){ return "Hello " + requester.getName() + "! Quote for" + ticker + " is 20.0"; } } public void onCreate(){ super.onCreate(); notificationMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); displayNotificationMessage("OnCreate() called in StockQuoteService2"); } public void onDestroy(){ displayNotificationMessage("onDestroy() called in StockQuoteService2"); super.onDestroy(); } public void onStart(Intent intent, int startId){ super.onStart(intent, startId); } public IBinder onBind(Intent intent){ displayNotificationMessage("onDestroy() called in StockQuoteService2"); return new StockQuoteService2Impl(); } private void displayNotificationMessage(String message){ Notification notification = new Notification(R.drawable.oneyear, message, System.currentTimeMillis()); PendingIntent currentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); notification.setLatestEventInfo(this, "StockeQuoteService2", message, currentIntent); notificationMgr.notify(R.id.app_notification_id, notification); } }
(4)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" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is where the service wourld ask for help" /> </LinearLayout>
(5)strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">AndroidServiceRemote2</string> <item type="id" name="app_notification_id"></item> </resources>
(6)AndroidMinifest.xml:服务在此声明,这样在同台android设备上的其他android程序可以通过bind/unbind进行访问。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.syh" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/oneyear" 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>