在Android Binder與Activity之間做簡單資料傳送

1.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.app.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="CountService" >
            <intent-filter>
                <action android:name="com.easy.CountService" />
            </intent-filter>
        </service>
    </application>

</manifest>

2.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:text="@string/hello_world" />

    <Button
        android:id="@+id/service_Btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="50dp"
        android:text="Get Service Count" />

</RelativeLayout>

3.ICountService.java

public interface ICountService {
public int getCount();
}

4.CountService.java

public class CountService extends Service implements ICountService {

private boolean threadDisable;
private ServiceBinder serviceBinder = new ServiceBinder();
private int count;

public class ServiceBinder extends Binder implements ICountService {
@Override
public int getCount() {
return count;
}
}

@Override
public IBinder onBind(Intent intent) {
return serviceBinder;
}

@Override
public void onCreate() {
super.onCreate();
new Thread(new Runnable() {

@Override
public void run() {
while (!threadDisable) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
count++;
Log.e("CountService", "Count is " + count);
}
}
}).start();
}

@Override
public void onDestroy() {
super.onDestroy();
this.threadDisable = true;
Log.e("CountService", "on destroy");
}

public int getCount() {
return count;
}
}

5.MainActivity.java

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
private ICountService countService;
Button service_Btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
service_Btn = (Button) this.findViewById(R.id.service_Btn);
this.bindService(new Intent("com.easy.CountService"),
this.serviceConnection, BIND_AUTO_CREATE);
service_Btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
getServiceCount();
}

});

}

public void getServiceCount() {
if (countService != null) {
Toast.makeText(this, "Number : " + countService.getCount(),
Toast.LENGTH_SHORT).show();
}
}

private ServiceConnection serviceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
countService = (ICountService) service;
Log.e("CountService",
"Activity count is " + countService.getCount());
}

@Override
public void onServiceDisconnected(ComponentName name) {
countService = null;
}

};

@Override
protected void onDestroy() {
super.onDestroy();
this.unbindService(serviceConnection);
}
}

沒有留言: