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中的操作。