Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android4.4 之service (下)

android4.4 之service (下)

編輯:關於Android編程

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==========


package="com.yx.boundservicetransact"
android:versionCode="1"
android:versionName="1.0" >

android:minSdkVersion="8"
android:targetSdkVersion="18" />

android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.yx.boundservicetransact.MainActivity"
android:label="@string/app_name" >












  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved