标签:des android style blog color io os ar java
代码:
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(); }
标签:des android style blog color io os ar java
原文地址:http://www.cnblogs.com/lcchuguo/p/3995648.html