public class MainActivity extends Activity {
private Button btn_start;
private Button btn_stop;
private Button btn_change;
private Button btn_bind;
private Button btn_unbind;
private MyConn myConn;
private IService myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_start = (Button) findViewById(R.id.btn_start);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_change = (Button) findViewById(R.id.btn_change);
btn_bind = (Button) findViewById(R.id.btn_bind);
btn_unbind = (Button) findViewById(R.id.btn_unbind);
buttonListener bl = new buttonListener();
btn_change.setOnClickListener(bl);
btn_start.setOnClickListener(bl);
btn_stop.setOnClickListener(bl);
btn_bind.setOnClickListener(bl);
btn_unbind.setOnClickListener(bl);
}
class buttonListener implements OnClickListener
{
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start:
Intent intent_start = new Intent(getApplicationContext(),BindService.class);
startService(intent_start);
break;
case R.id.btn_stop:
Intent intent_stop = new Intent(getApplicationContext(),BindService.class);
stopService(intent_stop);
break;
case R.id.btn_change:
if(myBinder != null)
myBinder.doChange("啦啦啦");
break;
case R.id.btn_bind:
if(myConn == null)
{
myConn = new MyConn();
Intent intent_bind = new Intent(getApplicationContext(),BindService.class);
bindService(intent_bind, myConn, BIND_AUTO_CREATE);
}
break;
case R.id.btn_unbind:
Intent intent_unbind = new Intent(getApplicationContext(),BindService.class);
if(myConn != null && myBinder != null)
{
unbindService(myConn);
myConn = null;
myBinder = null;
}
break;
default:
break;
}
}
}
private class MyConn implements ServiceConnection
{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("代理人返回回来了,onServiceConnected");
myBinder = (IService) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("接触绑定了,onServiceDisconnected");
}
}
}
public class BindService extends Service {
@Override
public IBinder onBind(Intent intent) {
System.out.println("Service绑定成功,onBind");
//返回自定义的代理对象
return new MyBinder();//这里的返回是返回到MainActivity里面的绑定myConn
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("Service解绑成功,onUnbind");
return super.onUnbind(intent);
}
public class MyBinder extends Binder implements IService
{
//间接的利用代理人调用了changeServiceThing的方法
public void doChange(String what)
{
changeServiceThing(what);
}
}
@Override
public void onCreate() {
System.out.println("Service开启,onCreate");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("Service关闭,onDestroy");
super.onDestroy();
}
public void changeServiceThing(String what)
{
Toast.makeText(getApplicationContext(), what+"变换了,changeServiceThing", Toast.LENGTH_LONG).show();
}
}
public interface IService {
public void doChange(String what);
}
public interface IService {
public void doChange(String what);
}
public class MainActivity extends Activity {
private Intent intent;
private IService iService;
private ServiceConnection myConn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void bind(View view) {
intent = new Intent();
intent.setAction("com.yydcdut.alipay");
myConn = new MyConn();
boolean flag = bindService(intent, myConn, BIND_AUTO_CREATE);
System.out.println("flag------>" + flag);
}
private class MyConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iService = IService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
public void method(View view) {
try {
iService.callMethodInService();
} catch (RemoteException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}