android4.4 之service (下)
parcel的使用方法:
parcel可以想成是一个数据的包裹,什么东西都能放进去
生成parcel对象:parcel = Parcel.obtain();
给parcel赋值:
parcel.writeString("abc");
parcel.writeInt(123);
parcel.writeFloat(1.4f);
获取parcel的值:
parcel.setDataPosition(0);
String str = parcel.readString();
int i = parcel.readInt();
float f = parcel.readFloat();
在这里解释一下:在给parcel赋值时,可以理解为是指针赋值,每付一个值指针向后移动一位,在取值时parcel.setDataPosition(0);意思表示将指针放到第一个,再依次取出值。具体的例子就不贴出来了。
下面是一个绑定service的完整例子:
============MainActivity.java================
package com.yx.boundservicetransact;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button boundButton;
private Button sendButton;
private Binder binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boundButton = (Button) findViewById(R.id.boundButton);
sendButton = (Button) findViewById(R.id.sendButton);
boundButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
});
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Parcel data = Parcel.obtain() ;//创建空的data
Parcel reply = Parcel.obtain() ;//创建空的reply
data.writeString("avtivity中的data数据");//给data赋值
try {
binder.transact(0, data, reply, 0);//这里会运行SecondService中的MyBinder中的onTransact方法,并可以在其中得到reply的值
System.out.println("在activity得到返回后的reply数据:"+reply.readString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("onServiceConnected");
MainActivity.this.binder = (Binder) service;
}
};
}
=================SecondService.java================
package com.yx.boundservicetransact;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
public class SecondService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return new MyBinder();
}
class MyBinder extends Binder{
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
//data为发往service的数据,reply为返回activity的数据
System.out.println("service中接收的data-->"+data.readString());
reply.writeString("service中的reply数据");
return super.onTransact(code, data, reply, flags);
}
}
}
====================AndroidManifest.xml==========
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yx.boundservicetransact"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.yx.boundservicetransact.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="com.yx.boundservicetransact.SecondService"></service>
</application>
</manifest>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。