该代码来自Mars老师的Android视频。
1、新建一个S01E26_Service01的Android项目。
2、新建一个SecondService的类,该类继承Service。onBind方法是继承Service方法必须复写的方法。新建一个内部类FirstBinder继承于Binder,当中的getData方法返回一个字符串。在onBind方法中新建一个IBinder对象,利用向下转型新建一个FirstBinder对象,然后返回这个IBinder对象。
package com.marschen.s01e26_service01;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class SecondService extends Service{
//其他应用程序组件(Activity)绑定至当前的Service对象时,会调用该方法
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
IBinder binder = new FirstBinder();
return binder;
}
class FirstBinder extends Binder{
public String getData() {
return "test data";
}
}
}
MainActivity.java代码如下。该类其实是新建了一个Button,给该Button添加了一个监听器。要实现Activity和Service中间的通信,必须使用bindService方法,而其中需要三个参数intent(新建Intent对象,其中Intent对象连接MainActivity.this和SecondService.class,把该intent传进去即可),第二个参数ServiceConnection对象(新建一个ServiceConnection内部类,需要重写两个方法,其中在onServiceConnected方法中创建在SecondService(要绑定的Service)中的Binder对象FirstBinder,调用其中的getData方法,得到返回的数据,最后打印出来,这样其实已经完成了Activity和Service的绑定大半工作),最后一个参数flags(这里直接用BIND_AUTO_CREATE即可),最后就完成了Activity和Service的绑定。效果是在对Button进行点击,就能从Service的数据传到Activity中,在控制台打印出在Service中传过来的数据。
package com.marschen.s01e26_service01;
import com.marschen.s01e26_service01.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 {
Button button=null;
@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) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(MainActivity.this, SecondService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
});
}
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
FirstBinder binder = (FirstBinder) service;
String data = binder.getData();
System.out.println("data----->"+ data);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
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"
tools:context=".MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="完成绑定操作" />
</RelativeLayout>
要注意在AndroidManifest.xml中对service进行注册。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.marschen.s01e26_service01"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.marschen.s01e26_service01.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.marschen.s01e26_service01.SecondService">
</service>
</application>
</manifest>