Android 第二周

课前作业:

  1. Service 的 start 和 bind 状态的区别?
  2. 同一个 Service,先 startService,然后再 bindService,如何把它停止掉?
  3. 注意过 Service 的 onStartCommand 方法的返回值吗?不同返回值有什么区别?
  4. Service 的生命周期方法 onCreate、onStart、onBind 等运行在哪个线程?

正式开始

一、Service 的启动方式:startService() 和 bindService()

MyService.java:

public class MyService extends Service {

    private static final String TAG = "MyService";

    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate: ");
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.i(TAG, "onStart: ");
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        super.onDestroy();
    }

	public class  MyBinder extends Binder {

        public MyService getService() {
            return MyService.this;
        }

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: ");
        return null;
    }
}

记得在 AndroidManifest.xml 中 application 标签下配置 <service android:name=".MyService"/>

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mIntent = new Intent(MainActivity.this, MyService.class);
    }

	//======测试 startService() 的代码,开始======
	//点击按钮,执行 startService()
    public void start_Service(View view) {
        startService(mIntent);
    }

	//点击按钮,执行 stopService()
    public void stop_Service(View view) {
        stopService(mIntent);
    }
    //======测试 startService() 的代码,结束======

	//======测试 bindService() 的代码,开始======
	//声明 Service
	private MyService myService;
	//是否已绑定 Service
    private boolean isBind;

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            isBind = true;
            //得到了 myService,就可以调用 Service 中的方法
            myService = ((MyService.MyBinder) service).getService();
            Log.i(TAG, "onServiceConnected: ");
        }

        /**
         * 一般不会被调用,异常情况下才被调用
         * 当服务丢失时调用,这通常当托管服务的进程崩溃或被杀死时发生
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            myService = null;
            Log.i(TAG, "onServiceDisconnected: ");
        }
    };

	//点击按钮,执行 bindService()
	public void bind_Service(View view) {
        bindService(mIntent, conn, BIND_AUTO_CREATE);
    }

	//点击按钮,执行 unbindService()
    public void unbind_Service(View view) {
        if (isBind) {
            unbindService(conn);
            isBind = false;
        }
    }
    //======测试 bindService() 的代码,结束======

	@Override
    protected void onDestroy() {
        super.onDestroy();
        if (isBind) {
            unbindService(conn);
            isBind = false;
        }
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

	<Button
        android:id="@+id/bind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/unbind_service"
        android:onClick="bind_Service"
        android:text="bind service"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/unbind_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/start_service"
        android:onClick="unbind_Service"
        android:text="unbind service"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/start_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/stop_service"
        android:onClick="start_Service"
        android:text="start service"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/stop_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="stop_Service"
        android:text="stop service"
        android:textAllCaps="false" />
	        
</RelativeLayout>
1. startService() 情况下的生命周期

首次调用 startService():执行 onCreate()、onStartCommand()、onStart()
首次调用 startService()
之后调用 startService() 或按返回键退出再进入后再调用 startService():执行 onStartCommand()、onStart(),不再执行 onCreate()
之后调用 startService() 或按返回键退出再进入后再调用 startService()
多次调用 stopService():只打印一次 onDestroy()
多次调用 stopService()

2. bindService() 情况下的生命周期

多次 bindService():只执行一次 onCreate()、onBind()、onServiceConnected()
多次 bindService()
多次 unBind():会抛异常,可声明一个 boolean,在 bindService() 置为 true,在 unBind() 中置为 false,防止多次 unBind() 后抛异常
多次 unBind()

二、同一个 Service,先 startService,然后再 bindService

先 startService(),再 bindService(),经测试,要停止那个 Service 需要执行完 stopService() 和 unbindService(),执行顺序不分先后

只要 stopService() 和 unbindService() 这两个方法执行完,就可以停止那个 Service,只是打印的效果不一样
a. 先 stopService() 后 unbindService() 时,先执行 stopService(),但是不打印 onDestory,当执行 unbindService() 后依次打印 onUnbind: 、onDestory
b. 先 unbindService() 后 stopService() 时,执行 unbindService() 打印 onUnbind,执行 stopService() 打印 onDestroy

MyService 对应的打印结果:
MyService 对应的打印结果

三、Service 的 onStartCommand 方法的返回值

1.START_STICKY:

系统会保留启动状态但不会保存 Intent 对象,之后系统会用一个 null 的 Intent 对象来调用 onStartCommand() 方法,在这个情况下,除非有一些被发送的 Intent 对象在等待启动服务,Intent 可能为空,需要做非空判断

2.START_NOT_STICKY:

除非重新执行 startService() 接收到新的 Intent,否则系统不会保留启动状态并重新创建该 Service

3.START_REDELIVER_INTENT:

系统在 onStartCommand() 方法返回后,系统会自动重启该服务(调用 stopSelf() 方法的除外),并且用发送给这个服务的最后的 Intent 对象调用了 onStartCommand() 方法,在该服务调用 stopSelf() 方法之前,能够一直保留 Intent 对象数据

4.START_STICKY_COMPATIBILITY:

START_STICKY 的兼容版本,但不保证服务被终止后一定能重启

四、Service 的生命周期方法运行在哪个线程

Service 的生命周期方法运行在主线程,不能在 Service 中做耗时操作

作业答案:

1.Service 的 start 和 bind 状态的区别?

a. 首次调用 startService():执行 onCreate()、onStartCommand(),之后再次调用 startService():只执行 onStartCommand()
b. 多次 bindService():只执行一次 onCreate()、onBind()、onServiceConnected()

2.同一个 Service,先 startService,然后再 bindService,如何把它停止掉?

先 startService(),再 bindService(),经测试,要停止那个 Service 需要执行完 stopService() 和 unbindService(),执行顺序不分先后

3.注意过 Service 的 onStartCommand 方法的返回值吗?不同返回值有什么区别?

参考上方 三、Service 的 onStartCommand 方法的返回值

4.Service 的生命周期方法 onCreate、onStart、onBind 等运行在哪个线程?

主线程

为了处理这种后台进程,Android引入了Service的概念。ServiceAndroid中是一种长生命周期的组件,它不实现任何用户界面。最常见的例子如:媒体播放器程序,它可以在转到后台运行的时候仍然能保持播放歌曲;或者如文件下载程序,它可以在后台执行文件的下载。 让我们来看下如何创建Service: 创建一个Service Android中已经定义了一个 ‘Service’类,所有其他的Service都继承于该类。Service类中定义了一系列的生命周期相关的方法,如: onCreate(), onStart(), onDestroy()。参见下例: package com.wissen.testApp.service; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Toast.makeText(this, “Service created…”, Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(this, “Service destroyed…”, Toast.LENGTH_LONG).show(); } }上例中的这个Service主要做了这些事:当服务创建和销毁时通过界面消息提示用户。 如Android中的其它部件一样, Service也会和一系列Intent关联。Service的运行入口需要在AndroidManifest.xml中进行配置,如下: 1. 2. 3. 4. 5. 复制代码 之后我们的Service就可以被其他代码所使用了。 使用Service: 应用程序可以通过调用 Context.startService方法来启动Service。如果当前没有指定的Service实例被创建,则该方法会调用 Service的onCreate方法来创建一个实例;否则调用Service的onStart方法。参见下列代码: 1. .. 2. Intent serviceIntent = new Intent(); 3. serviceIntent.setAction(”com.wissen.testApp.service.MY_SERVICE”); 4. startService(serviceIntent); 5. .. 复制代码 以上代码调用了startService方法,Service会持续运行,直到调用stopService()或stopSelf()方法。 还有另一种绑定Service的方式: 1. … 2. ServiceConnection conn = new ServiceConnection() { 3. @Override 4. public void onServiceConnected(ComponentName name, IBinder service) { 5. Log.i(”INFO”, “Service bound “); 6. } 7. 8. @Override 9. public void onServiceDisconnected(ComponentName arg0) { 10. Log.i(”INFO”, “Service Unbound “); 11. } 12. }; 13. 14. bindService(new Intent(”com.wissen.testApp.service.MY_SERVICE”), conn, Context.BIND_AUTO_CREATE); 15. … 复制代码 当应用程序绑定一个Service后,该应用程序和Service之间就能进行互相通信,通常,这种通信的完成依靠于我们定义的一些接口,请看下例: 1. package com.wissen.testApp; 2. 3. public interface IMyService { 4. public int getStatusCode(); 5. } 复制代码 这里定义了一个方法来获取Service的状态,但是Service是如何来使它起作用的呢?之前我们看到Service中有个返回IBinder对象的onBind方法,这个方法会在Service被绑定到其他程序上时被调用,而这个IBinder对象和之前看到的onServiceConnected方法中传入的那个IBinder是同一个东西。应用和Service间就依靠这个IBinder对象进行通信: 1. public class MyService extends Service { 2. private int statusCode; 3. private MyServiceBinder myServiceBinder = new MyServiceBinder(); 4. 5. @Override 6. public IBinder onBind(Intent intent) { 7. return myServiceBinder; 8. } 9. 10. public class MyServiceBinder extends Binder implements IMyService { 11. public int getStatusCode() { 12. return statusCode; 13. } 14. } 15. 16. … 17. } 复制代码 下列代码是说明getStatusCode是如何被调用的: 1. ServiceConnection conn = new ServiceConnection() { 2. @Override 3. public void onServiceConnected(ComponentName name, IBinder service) { 4. IMyService myService = (IMyService) service; 5. statusCode = myService.getStatusCode(); 6. Log.i(”INFO”, “Service bound “); 7. } 8. 9. … 10. }; 复制代码 或者,你也可以通过使用ServiceListener接口来达成相同的目的。 与远程Service通信(进程间Service通信): 如何两个进程间的Service需要进行通信,则需要把对象序列化后进行互相发送。 Android提供了一个 AIDL (Android接口定义语言)工具 来处理序列化和通信。这种情况下Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个 stub服务桩类。Service的onBind方法会返回实现类的对象,之后你就可以使用它了,参见下例: 先创建一个IMyRemoteService.aidl文件,内容如下: 1. package com.wissen.testApp; 2. 3. interface IMyRemoteService { 4. int getStatusCode(); 5. } 复制代码 如果你正在使用eclipse的 Android插件,则它会根据这个aidl文件生成一个Java接口类。生成的接口类中会有一个内部类Stub类,你要做的事就是去继承该Stub类: 1. package com.wissen.testApp; 2. 3. class RemoteService implements Service { 4. int statusCode; 5. 6. @Override 7. public IBinder onBind(Intent arg0) { 8. return myRemoteServiceStub; 9. } 10. 11. private IMyRemoteService.Stub myRemoteServiceStub = new IMyRemoteService.Stub() { 12. public int getStatusCode() throws RemoteException { 13. return 0; 14. } 15. }; 16. 17. … 18. } 复制代码 当客户端应用连接到这个Service时,onServiceConnected方法将被调用,客户端就可以获得IBinder对象。参看下面的客户端onServiceConnected方法: 1. … 2. ServiceConnection conn = new ServiceConnection() { 3. @Override 4. public void onServiceConnected(ComponentName name, IBinder service) { 5. IMyRemoteService myRemoteService = IMyRemoteService.Stub.asInterface(service); 6. try { 7. statusCode = myRemoteService.getStatusCode(); 8. } catch(RemoteException e) { 9. // handle exception 10. } 11. Log.i(”INFO”, “Service bound “); 12. } 13. 14. … 15. }; 复制代码 权限: 我们可以在AndroidManifest.xml文件中使用标签来指定Service访问的权限: 1. 2. 3. 4. 5. 复制代码 之后应用程序要访问该Service的话就需要使用标签来指定相应的权限: 1. 2. 复制代码 http://www.fulema.com/viewthread.php?tid=5&extra=page%3D1
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值