activity与service数据交互有很多种,下面介绍binder数据交互
binder数据交互方式是通过重写binder中的onTransact()方法来达到交互方式
MainActivity.java
package com.example.android_service_activty_tran;
import com.example.android_service_activty_tran.DownLoadService.LocalBinder;
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.Parcel;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button button1,button2,button3,button4;
private TextView textView1;
private boolean flag;
private DownLoadService downLoadService;
private LocalBinder localBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button3 = (Button) this.findViewById(R.id.button3);
button4 = (Button) this.findViewById(R.id.button4);
textView1 = (TextView) this.findViewById(R.id.textView1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// int value = downLoadService.getRandom();
//绑定service
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (flag) {
int value = downLoadService.getRandom();
textView1.setText("---"+value);
}
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
unbindService(connection);
flag = false;
}
});
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//向service中传递数据
Parcel data = Parcel.obtain();
data.writeInt(23);
data.writeString("jack");
//从service中获取数据
Parcel reply = Parcel.obtain();
try {
localBinder.transact(IBinder.LAST_CALL_TRANSACTION,
data, reply, 0);
} catch (RemoteException e) {
e.printStackTrace();
}
System.out.println("---从service中回传的值"+reply.readInt());
System.out.println("---从service中回传的值"+reply.readString());
}
});
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, DownLoadService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
//接触与service的绑定
@Override
protected void onStop() {
super.onStop();
}
public ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
flag = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
localBinder = (LocalBinder) service;
downLoadService = localBinder.getService();
flag = true;
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
DownLoadService.java
package com.example.android_service_activty_tran;
import java.util.Random;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
public class DownLoadService extends Service {
private final String TAG = "DownLoadService";
private Binder localBinder = new LocalBinder();
private Random random = new Random();
public DownLoadService() {
}
/**
* 在Service中自定义方法:通常是提供Client调用,通常是Activtiy调用的
* @return
*/
public int getRandom() {
return random.nextInt(100);
}
public class LocalBinder extends Binder {
public DownLoadService getService() {
return DownLoadService.this;
}
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
System.out.println("---从Activity中获取的值"+data.readInt());
System.out.println("---从Activity中获取的值"+data.readString());
reply.writeInt(getRandom());
reply.writeString("rose+jack");
return super.onTransact(code, data, reply, flags);
}
}
@Override
public void onCreate() {
Log.i(TAG, "---oncreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "---onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "---onUnbind");
return super.onUnbind(intent);
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "---onBind");
return localBinder;
}
@Override
public void onRebind(Intent intent) {
Log.i(TAG, "---onRebind");
super.onRebind(intent);
}
@Override
public void onDestroy() {
Log.i(TAG, "---onDestroy");
super.onDestroy();
}
}