标签:android des style blog java color
代码:
public void funclick(View view){ Intent _intent = new Intent(MainActivity.this,MyService.class); bindService(_intent, conn, BIND_AUTO_CREATE); } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName arg0) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { String mynameStr = ((MyBinder)service).helloWorld("bitch"); Log.e("mynameStr", mynameStr+"~"); } };
到此,我们仍然不会满足,因为我们只是调用了MyBinder中的方法,并没有调用Myservice方法嘛。所以。我们要增加一个返回MyserVice的方法:
package com.example.mydownload; 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 mybBinder = new MyBinder(); class MyBinder extends Binder{ <span style="color:#ff0000;">public MyService getMyService(){ return MyService.this; }</span> public String helloWorld(String name){ return name; } } @Override public void onCreate() { Log.e("log", "onCreate"); super.onCreate(); } Log.e("log", "hello,bitch!"); @Override public void onDestroy() { Log.e("log", "onDestroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("log", "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent arg0) { return mybBinder; } }
@Override public void onServiceConnected(ComponentName name, IBinder service) { ((MyBinder)service).getMyService().helloService(); }
客户端如何调用IBinder接口对象,布布扣,bubuko.com
标签:android des style blog java color
原文地址:http://blog.csdn.net/howlaa/article/details/37993653