服务的生命周期:
一、采用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方法,返回的就是中间人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 部署后的应用程序如下:
要想使用”调用远程服务的方法”,首先需要点击”绑定远程服务”。
点击”绑定远程服务”后输出的内容如下:
再次点击”调用远程服务”后出现的运行效果图如下: