您的位置:首页 > 移动开发 > Android开发

18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景

2015-07-15 00:15 851 查看
============================================================================服务的生命周期:一、采用start的方式开始服务生命周期如下: onStart()过时了开启服务:onCreate()-àonStartCommand()-àonDestory();如果服务已经开启,不会重复的执行onCreate(),而是会调用onStartCommand();如果停止的时候onDestory()服务只会被停止一次 二、服务还有一种开启方式,绑定的方式开启服务。onCreate()-àonBind();--àonunbind()--àonDestory();绑定服务不会调用onStart或者onStartCommand()方法 混合调用的服务的生命周期:服务长期后台运行,又想调用服务的方法:1.start方式开启服务(保证服务长期后台运行)2.bind方式绑定服务(保证调用服务的方法)3.unbind解除绑定服务4.stopService停止服务。 三、两种开启服务方法的区别。start方式开启服务。 一旦服务开启跟调用者(开启者)就没有任何关系了。开启者退出了,开启者挂了,服务还在后台长期的运行。开启者没有办法去调用服务里面的方法。(美国的司法独立) bind的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。不求同时生,但求同时死。开启者可以调用服务里面的方法。============================================================================总结:远程服务:调用者和服务在不同的工程代码里面。本地服务:调用者和服务在同一个工程代码里面。 每一个应用程序都是运行在自己独立的进程里面的。进程:操作系统分配内存空间的一个单位,进程的数据都是独立的。独立的内存空间。 aidl:android interface definition language 安卓接口定义语言aidl文件都是共有的,没有访问权限修饰符IPC:inter process communication进程间通讯 绑定本地服务调用方法的步骤:1.在服务的内部创建一个内部类 提供一个方法,可以间接调用服务的方法private class MiddlePerson extends Binderimplements IMiddlePerson{}2.实现服务的onbind方法,返回的就是中间人MiddlePerson3.在activity 绑定服务。bindService();4.在服务成功绑定的时候 会执行一个方法onServiceConnected 传递过来一个 IBinder对象5.强制类型转化 调用接口里面的方法。 绑定远程服务调用方法的步骤:1.在服务的内部创建一个内部类 提供一个方法,可以间接调用服务的方法2.把暴露的接口文件的扩展名改为aidl文件 去掉访问修饰符 publicprivate class MiddlePerson extendsIMiddlePerson.Stub{} IPC的子类3.实现服务的onbind方法,返回的就是中间人IMiddlePerson4.在activity 绑定服务。bindService();5.在服务成功绑定的时候 会执行一个方法onServiceConnected 传递过来一个 IBinder对象6.IMiddlePerson.Stub.asInterface(binder) 调用接口里面的方法。===========================================================================1 绑定服务(本地的方式),要做如下的案例:

操作步骤:“先点击绑定服务”,再点击“调用服务里面的方法”,然后查看服务的执行情况。2编写Android清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.servicelife" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.servicelife.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.itheima.servicelife.MyService"></service> </application> </manifest>
3 编写本地接口IMiddlePerson
package com.itheima.servicelife; public interface IMiddlePerson { /** * 代办暂住证 * @param money */ public void callMethodInService(int money); }
4 编写MyService
package com.itheima.servicelife; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.widget.Toast; public class MyService extends Service { // 2.实现服务成功绑定的代码 ,返回一个中间人。 @Override public IBinder onBind(Intent arg0) { System.out.println("服务被成功绑定了。。。。"); return new MiddlePerson(); } @Override public boolean onUnbind(Intent intent) { System.out.println("onunbind"); return super.onUnbind(intent); } @Override public void onCreate() { System.out.println("oncreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onstartcommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { System.out.println("ondestory"); super.onDestroy(); } /** * 这是服务里面的一个方法 */ public void methodInService() { Toast.makeText(this, "哈哈,服务给你办好了暂住证。", 0).show(); } // 1、第一步服务要暴露方法,必须要有一个中间人 private class MiddlePerson extends Binder implements IMiddlePerson { /** * 代办暂住证 * @param money 给钱 50块钱以上才给办。 */ public void callMethodInService(int money) { if (money >= 50) { methodInService(); } else { Toast.makeText(getApplicationContext(), "多准备点钱。", 0).show(); } } /** * 陪领导打麻将 */ public void playMajiang() { System.out.println("陪领导打麻将。"); } } }
5 编写MainActivity
package com.itheima.servicelife; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; public class MainActivity extends Activity { private MyConn conn ; private IMiddlePerson mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //绑定服务 public void bind(View view){ //3.activity采用绑定的方式去开启服务。 Intent intent = new Intent(this,MyService.class); conn = new MyConn(); bindService(intent, conn, BIND_AUTO_CREATE); } //解除绑定服务 public void unbind(View view){ unbindService(conn); } @Override protected void onDestroy() { System.out.println("啊啊啊,我是activity,我挂了"); super.onDestroy(); } //调用服务里面的方法。 public void call(View view){ //5.通过中间人调用服务里面的方法。 mp.callMethodInService(49); } private class MyConn implements ServiceConnection { // 4. 当服务被连接的时候调用 服务别成功 绑定的时候调用 @Override public void onServiceConnected(ComponentName name, IBinder service) { System.out.println("在activity里面成功得到了中间人"); mp = (IMiddlePerson) service; } // 当服务失去连接的时候调用(一般进程挂了,服务被异常杀死) @Override public void onServiceDisconnected(ComponentName name) { } } }
当把call方法里面的值设成49时,打印出的结果如下:

当把,值设置成大于50的时候,测试的结果如下:

下面通过aidl的方式进行远程服务调用编写服务端代码(注意:这个应用在手机端要先部署)

1 编写IMiddlePerson.aidl
package com.itheima.remoteservice; interface IMiddlePerson { /** * 调用服务里面的方法 */ void callMethodInService(); }
2 编写服务代码RemoteService:
package com.itheima.remoteservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class RemoteService extends Service { @Override public void onCreate() { System.out.println("远程服务被创建了。。。"); super.onCreate(); } @Override public void onDestroy() { System.out.println("远程服务被销毁了。"); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { return new MiddlePerson(); } private void methodInService(){ System.out.println("我是远程服务的方法,我被调用了。。。。"); } //1.创建一个中间人 远程服务继承的是ipc的一个实现类 private class MiddlePerson extends IMiddlePerson.Stub{ @Override public void callMethodInService() { methodInService(); } } }
3 编写MainActivity的代码如下:
package com.itheima.remoteservice; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
4 编写布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.itheima.remoteservice.MainActivity$PlaceholderFragment" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout>
5 编写Android清单文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.remoteservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.remoteservice.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.itheima.remoteservice.RemoteService"> <intent-filter > <action android:name="com.itheima.remoteservice"/> </intent-filter> </service> </application> </manifest>
程序运行后的效果如下:

编写调用服务的应用程序:

1 编写IMiddlePerson.aidl文件
package com.itheima.remoteservice; interface IMiddlePerson { /** * 调用服务里面的方法 */ void callMethodInService(); }
2 编写MainActivity
package com.itheima.bindremote; import com.itheima.remoteservice.IMiddlePerson; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.View; public class MainActivity extends Activity { private MyConn conn; private IMiddlePerson iMp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 绑定远程服务 * @param view */ public void bind(View view){ Intent intent = new Intent(); intent.setAction("com.itheima.remoteservice"); conn = new MyConn(); bindService(intent, conn, BIND_AUTO_CREATE); } private class MyConn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { iMp = IMiddlePerson.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } } public void call(View view){ try { iMp.callMethodInService(); } catch (RemoteException e) { e.printStackTrace(); } } @Override protected void onDestroy() { unbindService(conn); super.onDestroy(); } }
3 编写activity_main.xml的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="bind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="绑定远程服务" /> <Button android:onClick="unbind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="解除绑定远程服务" /> <Button android:onClick="call" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="调用远程服务的方法" /> </LinearLayout>
4 部署后的应用程序如下:

要想使用”调用远程服务的方法”,首先需要点击”绑定远程服务”。点击”绑定远程服务”后输出的内容如下:

再次点击”调用远程服务”后出现的运行效果图如下:

===========================================================================模拟一个应用调用“支付宝”远程服务的场景
1支付宝远程服务应用,应用程序结构:



ISafePay.aidl
package com.itheima.alipay; interface ISafePay { boolean callPay(long time,String pwd,double money); }
SafePayService
package com.itheima.alipay; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class SafePayService extends Service { @Override public IBinder onBind(Intent intent) { System.out.println("服务被绑定 onbind"); return new MyBinder(); } /** * 安全支付的方法 */ private boolean pay(long time,String pwd,double money) { if ("123".equals(pwd)) { return true; } else { return false; } } private class MyBinder extends ISafePay.Stub { /** * 调用安全支付的逻辑 */ public boolean callPay(long time, String pwd, double money) throws RemoteException { return pay(time, pwd, money); } } public void onCreate() { System.out.println("onCreate支付宝服务被创建,一直在后台运行,检查手机的安全状态"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("服务onStart"); return super.onStartCommand(intent, flags, startId); } @Override public boolean onUnbind(Intent intent) { System.out.println("onUnbind"); return super.onUnbind(intent); } @Override public void onDestroy() { System.out.println("ondestory支付宝服务被销毁"); super.onDestroy(); } }
MainActivity
package com.itheima.alipay; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
activity_main.xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.itheima.alipay.MainActivity$PlaceholderFragment" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="我是支付宝"/> </RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.alipay" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.alipay.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.itheima.alipay.SafePayService"> <intent-filter> <action android:name="com.itheima.alipay"/> </intent-filter> </service> </application> </manifest>


调用支付宝远程服务的程序:



ISafePay.aidl
package com.itheima.alipay; interface ISafePay{ boolean callPay(long time,String pwd,double money); }
MainActivity
package com.itheima.fish; import com.itheima.alipay.ISafePay; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private ISafePay iSafePay; private MyConn conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Intent intent = new Intent(); // intent.setAction("com.itheima.alipay"); // startService(intent); //保证服务长期后台运行。 } public void start(View view){ Intent intent = new Intent(); intent.setAction("com.itheima.alipay"); startService(intent); } public void stop(View view){ Intent intent = new Intent(); intent.setAction("com.itheima.alipay"); stopService(intent); } public void bind(View view){ Intent intent = new Intent(); intent.setAction("com.itheima.alipay"); conn = new MyConn(); bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作 } public void unbind(View view){ unbindService(conn); } public void click(View view){ Intent intent = new Intent(); intent.setAction("com.itheima.alipay"); conn = new MyConn(); bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作 //绑定服务调用服务的方法。 } private class MyConn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { iSafePay = ISafePay.Stub.asInterface(service); try { boolean result = iSafePay.callPay(System.currentTimeMillis(), "123", 3.52f); if(result){ Toast.makeText(getApplicationContext(), "支付成功,获取大炮弹", 0).show(); }else{ Toast.makeText(getApplicationContext(), "支付失败,请重试", 0).show(); } // unbindService(conn); // conn = null; } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { } } }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="混合调用的服务的生命周期(服务长期后台运行,又想调用服务的方法:):"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="点击顺序3:买5个炮弹" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="start" android:text="点击顺序1:开启服务(start方式开启服务(保证服务长期后台运行))" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="stop" android:text="点击顺序5:停止服务()" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="bind" android:text="点击顺序2:绑定服务(2.bind方式绑定服务(保证调用服务的方法))" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="unbind" android:text="点击顺序4:解除绑定服务" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.fish" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.fish.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>


===========================================================================1 管理服务的生命周期Service的生命周期,从它被创建开始,到它被销毁为止,可以有两种不同的路径:
A started service 被开启的service通过其他组件调用startService()被创建。 这种service可以无限地运行下去,必须调用stopSelf()方法或者其他组件调用stopService()方法来停止它。 当service被停止时,系统会销毁它。
A bound service 被绑定的service是当其他组件(一个客户)调用bindService()来创建的。 客户可以通过一个IBinder接口和service进行通信。 客户可以通过unbindService()方法来关闭这种连接。 一个service可以同时和多个客户绑定,当多个客户都解除绑定之后,系统会销毁service。
这两条路径并不是完全分开的。 即是说,你可以和一个已经调用了startService()而被开启的service进行绑定。 比如,一个后台音乐service可能因调用startService()方法而被开启了,稍后,可能用户想要控制播放器或者得到一些当前歌曲的信息,可以通过bindService()将一个activity和service绑定。这种情况下,stopService()或stopSelf()实际上并不能停止这个service,除非所有的客户都解除绑定。2 服务的生命周期图如下:

这个图说明了service典型的回调方法,尽管这个图中将开启的service和绑定的service分开,但是任何service都潜在的允许绑定。所以,一个被开启的service仍然可能被绑定。实现这些方法,你可以看到两层嵌套的service的生命周期:整个的生命周期:Service整体的生命时间是从onCreate()被调用开始,到onDestory()方法返回为止。和activity一样,service在onCreate()中进行他的初始化工作,在onDestory()中释放残留的资源。比如,一个音乐播放service可以在onCreate()中创建播放音乐的线程,在onDestory()中停止这个线程。onCreate()和onDestory()会被所有的service调用,不论service是通过startService()还是bindService()开始激活的生命周期:激活service的生命是从onStartCommand()或onBind()被调用开始的,它们个处理由startService()或bindService()方法传过来的Intent对象。如果service是通过onStartCommand()开启的,那么服务的生命周期和整个生命周期是相同的,最后一同结束。如果service是通过onBind()方法开启的,它的生命周期是在onUnbind()方法返回后结束。注意:如果服务是通过stopSelft()或stopService来停止的,这个过程中没有一个类似Activity的onStop()回调方法来调用它。当调用了stopSelft()或者stopService()方法后,除非这个service有绑定的客户组件,否则系统将会直接销毁它,在这个过程中,只有一个回调方法会被调用,那就是onDestory()方法。3 管理绑定服务的生命周期当绑定的service和所有的客户端都解除绑定之后,Android系统会销毁它,(除非它又同时被onStartCommand()方法调用)。因此,如果你的service是一个纯粹的绑定的service,那么你就不需要管理它的生命周期。如果你通过实现onStartCommand()回调方法,那么你必须显式的停止service,因为service此时被看做是开启的。这种情况下,service会一直运行到它自己调用stopSelf()或另一个组件调用stopService(),不论它是否和客户端绑定。另外,如果你的service被开启并且接受绑定,那么当系统调用你的onUnbind()方法时,如果你想在下次客户端绑定的时候接受一个onRebind()的调用(而不是调用onBind()),你可以选择在onUnbind()中返回true.onRebind()的返回值为void,但是客户端仍然在它的onServiceConnected()回调方法中得到IBinder对象。下图展示了这种service(被开启,还允许绑定)的生命周期:


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: