18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景
服务的生命周期:
一、採用start的方式開始服务
生命周期例如以下:
onStart()过时了
开启服务:onCreate()-àonStartCommand()-àonDestory();
假设服务已经开启,不会反复的执行onCreate(),而是会调用onStartCommand();
假设停止的时候onDestory()
服务仅仅会被停止一次
二、服务另一种开启方式。绑定的方式开启服务。
onCreate()-àonBind();--àonunbind()--àonDestory();
绑定服务不会调用onStart或者onStartCommand()方法
混合调用的服务的生命周期:
服务长期后台执行。又想调用服务的方法:
1.start方式开启服务(保证服务长期后台执行)
2.bind方式绑定服务(保证调用服务的方法)
3.unbind解除绑定服务
4.stopService停止服务。
三、两种开启服务方法的差别。
start方式开启服务。 一旦服务开启跟调用者(开启者)就没有不论什么关系了。
开启者退出了,开启者挂了,服务还在后台长期的执行。
开启者没有办法去调用服务里面的方法。(美国的司法独立)
bind的方式开启服务,绑定服务。调用者挂了。服务也会跟着挂掉。不求同一时候生。但求同一时候死。
开启者能够调用服务里面的方法。
============================================================================
总结:
远程服务:调用者和服务在不同的project代码里面。
本地服务:调用者和服务在同一个project代码里面。
每个应用程序都是执行在自己独立的进程里面的。
进程:操作系统分配内存空间的一个单位,进程的数据都是独立的。独立的内存空间。
aidl:android interface definition language 安卓接口定义语言
aidl文件都是共同拥有的,没有訪问权限修饰符
IPC:inter process communication进程间通讯
绑定本地服务调用方法的步骤:
1.在服务的内部创建一个内部类 提供一个方法,能够间接调用服务的方法
private class MiddlePerson extends Binderimplements IMiddlePerson{}
2.实现服务的onbind方法,返回的就是中间人MiddlePerson
3.在activity 绑定服务。bindService();
4.在服务成功绑定的时候 会执行一个方法onServiceConnected 传递过来一个 IBinder对象
5.强制类型转化 调用接口里面的方法。
绑定远程服务调用方法的步骤:
1.在服务的内部创建一个内部类 提供一个方法,能够间接调用服务的方法
2.把暴露的接口文件的扩展名改为aidl文件 去掉訪问修饰符 public
private class MiddlePerson extendsIMiddlePerson.Stub{} IPC的子类
3.实现服务的onbind方法,返回的就是中间人IMiddlePerson
4.在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和全部的client都解除绑定之后。Android系统会销毁它,(除非它又同一时候被onStartCommand()方法调用)。
因此,假设你的service是一个纯粹的绑定的service。那么你就不须要管理它的生命周期。
假设你通过实现onStartCommand()回调方法,那么你必须显式的停止service。由于service此时被看做是开启的。
这样的情况下。service会一直执行到它自己调用stopSelf()或另一个组件调用stopService()。不论它是否和client绑定。
另外。假设你的service被开启而且接受绑定,那么当系统调用你的onUnbind()方法时,假设你想在下次client绑定的时候接受一个onRebind()的调用(而不是调用onBind()),你能够选择在onUnbind()中返回true.
onRebind()的返回值为void,可是client仍然在它的onServiceConnected()回调方法中得到IBinder对象。
下图展示了这样的service(被开启,还同意绑定)的生命周期:
posted on 2017-04-22 18:47 cynchanpin 阅读(303) 评论(0) 编辑 收藏 举报