Service服务进程间通讯&Parcelable传递复杂数据
来源:互联网 发布:php怎么连接数据库 编辑:程序博客网 时间:2024/04/29 16:30
又是固定的模式,熟能生巧
service服务器端
Person.java 实现 Parcelable接口
package com.example.remoteparceservive;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>() { public Person createFromParcel(Parcel in) { return new Person(in); } public Person[] newArray(int size) { return new Person[size]; } }; public Person(){ } private Person(Parcel in){ readFromParcel(in); }@Overridepublic int describeContents() {// TODO Auto-generated method stubreturn 0;}@Overridepublic void writeToParcel(Parcel arg0, int arg1) {//写的顺序必须跟读的顺序一致// TODO Auto-generated method stubarg0.writeInt(age);arg0.writeString(name);}public void readFromParcel(Parcel in){age = in.readInt();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; }}
package com.example.remoteparceservive;parcelable Person;
//in 表示值由客户端设置,out表示值由服务设置,inout表示客户端服务端都设置了该值
package com.example.remoteparceservive;import com.example.remoteparceservive.Person;interface IStockQuoteService{String getQuote(in String ticker,in Person requester);}
package com.example.remoteparceservive;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class StockQuoteService2 extends Service {private NotificationManager notificationMgr;private int mNotificationId;//实现 aidl中方法public class StockQuoteServiceImpl extends IStockQuoteService.Stub{@Overridepublic String getQuote(String ticker, Person requester)throws RemoteException {// TODO Auto-generated method stubreturn requester.getName()+ticker;}} @Override public void onCreate() { super.onCreate(); notificationMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); displayNotificationMessage("onCreate() called in StockQuoteService2"); } @Override public void onDestroy() { displayNotificationMessage("onDestroy() called in StockQuoteService2"); // Clear all notifications from this service notificationMgr.cancelAll(); super.onDestroy(); } public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { displayNotificationMessage("onBind() called in StockQuoteService2"); return new StockQuoteServiceImpl(); //返回AIDL对象 } private void displayNotificationMessage(String message) { Notification notification = new Notification(R.drawable.ic_launcher, message,System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); notification.setLatestEventInfo(this, "StockQuoteService2",message, contentIntent); notification.flags = Notification.FLAG_NO_CLEAR; notificationMgr.notify(mNotificationId, notification); }}
最后就是在AndroidManifest.xml中声明服务,注意与本地service声明不同。
<service android:name="StockQuoteService2"> <intent-filter > <action android:name="com.example.remoteparceservive.IStockQuoteService"/> </intent-filter> </service>
客户端,从新建个应用
连同包路径拷贝Person.java,Person.aidl,IStockQuoteService.aidl到客户端应用的目录下,以便客户端可以找到调用里面的函数
MainActivity.java
package com.example.remoteparceclient;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.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;import com.example.remoteparceservive.*;public class MainActivity extends Activity {private IStockQuoteService stockService = null;private Button bindBtn;private Button callBtn;private Button unbindBtn;/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindBtn = (Button)findViewById(R.id.bindBtn); bindBtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view) { bindService(new Intent(IStockQuoteService.class .getName()), serConn, Context.BIND_AUTO_CREATE); bindBtn.setEnabled(false); callBtn.setEnabled(true); unbindBtn.setEnabled(true); }}); callBtn = (Button)findViewById(R.id.callBtn); callBtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view) { callService(); }}); callBtn.setEnabled(false); unbindBtn = (Button)findViewById(R.id.unbindBtn); unbindBtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view) { unbindService(serConn); bindBtn.setEnabled(true); callBtn.setEnabled(false); unbindBtn.setEnabled(false); }}); unbindBtn.setEnabled(false); } private void callService() { try { Person person = new Person(); person.setAge(47); person.setName("Dave"); String response = stockService.getQuote("SYH", person); Toast.makeText(MainActivity.this, "Value from service is "+response, Toast.LENGTH_SHORT).show(); } catch (RemoteException ee) { } } private ServiceConnection serConn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { stockService = IStockQuoteService.Stub.asInterface(service); callService(); } @Override public void onServiceDisconnected(ComponentName name) { stockService = null; } };}
<?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:id="@+id/bindBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bind" /> <Button android:id="@+id/callBtn" android:layout_width="wrap_content" android:layout_height="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>
0 0
- Service服务进程间通讯&Parcelable传递复杂数据
- AIDL进程间传递复杂数据类型—AIDL传递android.os.Parcelable接口
- Android四大组件之Service 远程服务 通过AIDL进行进程间复杂类型数据交换
- AIDL服务-传递复杂数据
- 利用 Parcelable 进行 Activity 之间的复杂数据传递
- Android通过Parcelable传递复杂的对象数据
- android之Service介绍之四 如何传递复杂数据的AIDL服务
- android 怎样用AIDL Service 传递复杂数据
- 怎样用AIDL Service 传递复杂数据
- 进程间通讯—MFC三种消息传递数据
- 进程间复杂数据类型传递
- 传递复杂数据的AIDL服务
- 如何传递复杂数据的AIDL服务
- Android Service ---传递复杂数据的AIDL Service 笔记
- android Service之四:传递复杂数据类型的远程服务
- 窗体间传递复杂数据
- 窗体间传递复杂数据
- 窗体间传递复杂数据
- Oracle Apps修改用户密码的API
- InputStreamReader 和 FileReader联系与区别
- Codeforces 459E Pashmak and Graph
- Java web filter 之一 基础实现
- axis2---2 --rpc
- Service服务进程间通讯&Parcelable传递复杂数据
- hdu 1896 Stones
- Codeforces Round #263 (Div. 2) A、B、C【淼】
- Java int 类型向String转换
- 第十三章_异步处理
- axis2----3
- [tech] 曼彻斯特编码和差分曼彻斯特编码
- Android之Tab分页标签的实现方法一-----可滑动的Tab的3种方式
- 关于map和set的浅显理解