MainActivity.java
package com.jing.example.nknu_.android_service_activity_tran;
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.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button1, button2,button3,button4;
private boolean flag;
private DownLoadService downLoadService;
private DownLoadService.LocalBinder localBinder; //全局的變量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)this.findViewById(R.id.button);
button2 = (Button)this.findViewById(R.id.button2);
button3 = (Button)this.findViewById(R.id.button3);
button4 = (Button)this.findViewById(R.id.button4);
textView = (TextView)this.findViewById(R.id.textView);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
///綁定Service
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag) {
int value = downLoadService.getRandom();
textView.setText("" + value);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(connection);
flag =false;
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("按鈕四號啟動");
//向Service傳遞數據
Parcel parcel = Parcel.obtain();
parcel.writeInt(23);
parcel.writeString("Jack");
//從Service中獲取數據
Parcel reply = Parcel.obtain();
try {
localBinder.transact(IBinder.LAST_CALL_TRANSACTION, parcel, 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
}
public ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
flag = false;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
localBinder = (DownLoadService.LocalBinder)arg1;
downLoadService = localBinder.getService();
flag = true;
}
};
}
DownLoadService.java
package com.jing.example.nknu_.android_service_activity_tran;
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;
import java.util.Random;
/**
* Created by nknu_ on 2016/3/6.
*/
public class DownLoadService extends Service {
private final String TAG = "DownLoadService";
private final 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 {
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);
}
}
/**
*
* 在Service中自定義的方法:提供給Client調用的,通常是Activity
*
* @return
*/
public int getRandom(){
return random.nextInt(100);
}
@Override
public void onCreate(){
Log.i(TAG, "---------->>onCreate");
super.onCreate();
}
@Override
public boolean onUnbind(Intent intent){
Log.i(TAG, "---------->>onUnbind");
return super.onUnbind(intent);
}
@Override
public IBinder onBind(Intent arg0){
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();
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.jing.example.nknu_.android_service_activity_tran.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="99dp"
android:textSize="30sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="綁定Service"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_marginTop="65dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="調用Service的方法"
android:id="@+id/button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="卸載Service服務"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="和Service進行數據通信"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_alignLeft="@+id/button3"
android:layout_alignStart="@+id/button3" />
</RelativeLayout>