Activity 调用Service的方法
2016-11-04 16:55
393 查看
一般来说,Activity调用Service 分为两种:进程内调用和进程间调用。进程内调用时比较常用的一种,在进程内调用中我们常常使用的是bindService来启动Service(关于这种启动方式的好处,才疏学浅就不再这卖弄了)。下面就这两种调用方式分别进行简单介绍:
2.在Activity中绑定Service并测试效果:
3.截个图吧,更清楚一点:
2.定义一个实现了aidl的服务:
3.在同一个app中模拟进程间调用的效果,给Service设置remote属性:
4.启动并调用Service中的方法:
5.附上效果截图:
6.关于aidl进程间调用是参考了http://www.cnblogs.com/zhangdongzi/archive/2012/01/09/2317197.html
大神之作,进行的精简修改。
1.通过bindService来启动Service,并调用Service中的方法。
1.一个简单的Service:package gu.chuan.hang; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return new MyBinder(); } public class MyBinder extends Binder{ public MyService getMyService(){ return MyService.this; } } /** * 在Service中定义这个方法,用于测试 * @return */ public String getAuthorName(){ return "guchuanhang"; } }
2.在Activity中绑定Service并测试效果:
package gu.chuan.hang; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent myServiceIntent = new Intent(MainActivity.this, MyService.class); bindService(myServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE); } ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { MyService myService = ((MyService.MyBinder) service).getMyService(); String authorName = myService.getAuthorName(); Toast.makeText(MainActivity.this, "author name is: " + authorName, Toast.LENGTH_LONG).show(); } @Override public void onServiceDisconnected(ComponentName name) { } }; }
3.截个图吧,更清楚一点:
2.进程间启动Service并调用Service中的方法
1.首先定义一个aidl文件(扩展名为.aidl):package gu.chuan.hang.remoteservice.aidl; interface AuthorInfo{ String getAuthorName(); }
2.定义一个实现了aidl的服务:
package gu.chuan.hang.remoteservice.aidl; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class MyService extends Service{ @Override public IBinder onBind(Intent intent) { return new MyStub(); } private class MyStub extends AuthorInfo.Stub{ @Override public String getAuthorName() throws RemoteException { return "guchuanhang"; } } }
3.在同一个app中模拟进程间调用的效果,给Service设置remote属性:
<service android:name="gu.chuan.hang.remoteservice.aidl.MyService" android:process=":remote" android:exported="false" > </service>
4.启动并调用Service中的方法:
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Intent intent=new Intent(this,MyService.class); startService(intent); bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); } private ServiceConnection mServiceConnection=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { AuthorInfo authorInfo=AuthorInfo.Stub.asInterface(service); try { String authorName=authorInfo.getAuthorName(); Toast.makeText(MainActivity.this, "author name is: "+authorName, Toast.LENGTH_LONG).show(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { } }; }
5.附上效果截图:
6.关于aidl进程间调用是参考了http://www.cnblogs.com/zhangdongzi/archive/2012/01/09/2317197.html
大神之作,进行的精简修改。
3.附上Demo下载地址:
http://download.csdn.net/detail/guchuanhang/9155121相关文章推荐
- 在Activity里调用Service中的方法以及MediaPlay类的使用
- Activity调用Service里的方法
- Activity调用Service的方法
- service调用Activity中的方法
- 在Activity中调用Service的非静态方法
- Android实现Activity、Service与Broadcaster三大组件之间互相调用的方法详解
- 在Activity中调用Service的方法——本地服务
- 如何从Activity中调用Service中的方法
- 在Activity中调用本地服务(Service)中的方法
- 在Activity中调用本地服务(Service)中的方法
- android java开发 第三天 之 Activity 与 Service 绑定(activiey 调用service中方法)
- 在Activity中调用本地服务(Service)中的方法
- activity调用service中方法的几种方案
- Activity与Service绑定(activity调用service方法)--安卓软件开发
- Activity与Service之间的相互通信方法小结
- Android开机启动Activity或者Service方法
- 关于Activity调用Ondestroy()方法之后内存管理器为什么没有释放占用资源
- DELPHI调用JAVA WEBSERVICE方法 出现 null object 异常的原因
- Fragment中调用父Activity的方法
- Android TabActivity无法正常bindService解决方法