bound service
service 未與activity綁定前,activity只可以讓service工作,但是service無法返回資料給activity,所以這裡介紹一下bound service
boundservice還是service,所以在設定檔中還需要進行配置:
==========AndroidManifest.xml=================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yx.boundservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.yx.boundservice.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="com.yx.boundservice.SecondService"></service>
</application>
</manifest>
=====================MainActivity.java=============
package com.yx.boundservice;
import com.yx.boundservice.SecondService.FirstBinder;
import android.os.Bundle;
import android.os.IBinder;
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 button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondService.class);
bindService(intent, conn, BIND_AUTO_CREATE);//第三個參數為當綁定service時自動建立service對象
}
});
}
@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) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {//綁定時會調用的
FirstBinder binder = (FirstBinder) service;
String data = binder.getData();
System.out.println("data--->"+data);
}
};
}
===================SecondService.java===========
package com.yx.boundservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class SecondService extends Service {
//當其他的應用程式組件(activity)綁定至當前的Setvice時,就會調用該方法
@Override
public IBinder onBind(Intent intent) {
IBinder binder = new FirstBinder();
return binder;
}
class FirstBinder extends Binder{
public String getData(){
return "test data";
}
}
}
最後,由上面代碼,大概說一下執行的順序。
首先執行MainActivity的onClick事件,在onClick事件中建立intent對象,並關聯當前的activity和SecondService。利用 bindService(intent, conn, BIND_AUTO_CREATE)綁定intent和ServiceConnection,並會調用ServiceConnection中的onServiceConnected方法,在這個方法中有傳入參數IBinder,這個參數就是從SecondService檔案中onBind方法中返回的binder對象,並通過這個對象可以調用到service中的操作。