activity_main.xml (布局文件)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:text="卸载srvice服务" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button3"
android:text="service数据通信" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_centerVertical="true"
android:text="调用service方法" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:text="绑定service" />
</RelativeLayout>
DownloadService extends Service
DownloadService.java
package com.example.android_service_activity;
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 final Random random=new Random();
public DownloadService() {
}
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 {
// TODO Auto-generated method stub
System.out.println("---->>从activice获取的值;"+data.readInt());
System.out.println("---->>>从activice获取的值:"+data.readString());
reply.writeInt(getRandom());
reply.writeString(" rose+ jack");
return super.onTransact(code, data, reply, flags);
}
}
/**
* 自定义获取随机数:提供给客户端调用,通常是Activity
*
* @return 随机数
*/
public int getRandom()
{
return random.nextInt(100);
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i(TAG, "----->>>>>>>onCreate");
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "----->>>>>>>onBind");
return localBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "----->>>>>>>onUnbind");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stu
super.onRebind(intent);
Log.i(TAG, "----->>>>>>>onRebind");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG, "----->>>>>>>onDestroy");
}
}
MainActivity.java
package com.example.android_service_activity;
import com.example.android_service_activity.DownloadService.LocalBinder;
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.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button1, button2, button3, button4;
TextView textView;
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) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
textView = (TextView) findViewById(R.id.textView1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 绑定service
// bindService(downloadService, conn, flags)
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
int value = downloadService.getRandom();
textView.setText("" + value);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//解绑
unbindService(connection);
flag=false;
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//向service 传递数据
Parcel parcel=Parcel.obtain();
parcel.writeInt(23);
parcel.writeString("Jack");
//从service 获取数据
Parcel reply=Parcel.obtain();
try {
localBinder.transact(Binder.LAST_CALL_TRANSACTION, parcel, reply, 0);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("------>>从service中回传的值"+reply.readInt());
System.out.println("------>>从service中回传的值"+reply.readString());
}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
// 绑定Service
Intent intent = new Intent(MainActivity.this, DownloadService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
// @Override
// protected void onStop() {
// // TODO Auto-generated method stub
// super.onStop();
// }
public ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
flag = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
localBinder = (LocalBinder) service;
downloadService = localBinder.getService();
flag = true;
}
};
@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;
}
}