1.定义bind实现类
package com.douzi.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
IBinder binder = new MyBinder();
class MyBinder extends Binder{
public MyService getService()
{
return MyService.this;
}
public String getName(String name){
return "你的名字是:"+ name;
}
}
public void helloService(){
for (int i = 0 ; i < 100 ; i++){
try {
Thread.sleep(1000);
Log.i("MyService", i+"");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
helloService();
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("MyService", "onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i("MyService", "onUnbind");
return super.onUnbind(intent);
}
}
2.调用
package com.douzi.servicedemo;
import com.douzi.servicedemo.MyService.MyBinder;
import android.app.Activity;
import android.app.Service;
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.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Intent mIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button01 = (Button)findViewById(R.id.button01);
Button button02 = (Button)findViewById(R.id.button02);
button01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("MainActivity", "button 01 start...");
// TODO Auto-generated method stub
mIntent = new Intent(MainActivity.this, MyService.class);
startService(mIntent);
}
});
button02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(mIntent);
}
});
Button button03 = (Button)findViewById(R.id.intentServiceButton);
button03.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent _intent = new Intent(MainActivity.this, MyIntentService.class);
startService(_intent);
}
});
Button button04 = (Button)findViewById(R.id.bindButton);
button04.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent _intent = new Intent(MainActivity.this, MyService.class);
bindService(_intent, conn, Service.BIND_AUTO_CREATE);
}
});
Button button05 = (Button)findViewById(R.id.unbindButton);
button05.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
unbindService(conn);
}
});
}
private 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
//String mName=((MyBinder)service).getName("李想");
//Toast.makeText(MainActivity.this, mName, 3000).show();
((MyBinder)service).getService().helloService();
}
};
}