zoukankan      html  css  js  c++  java
  • Andriod Service 基础知识

    Service  分为两类 

    A started service

      被开启的service通过其他组件调用 startService()被创建。

      这种service可以无限地运行下去,必须调用stopSelf()方法或者其他组件调用stopService()方法来停止它。

      当service被停止时,系统会销毁它。

    A bound service

      被绑定的service是当其他组件(一个客户)调用bindService()来创建的。

      客户可以通过一个IBinder接口和service进行通信。

      客户可以通过 unbindService()方法来关闭这种连接。

      一个service可以同时和多个客户绑定,当多个客户都解除绑定之后,系统会销毁service。(简单来说就是依附于被绑定的Activity)

    下面贴代码:

    package com.jredu.helloworld.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.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    
    import com.jredu.helloworld.R;
    import com.jredu.helloworld.service.MyService;
    
    public class ServiceActivity extends AppCompatActivity {
        Button bind;
        Button unbind;
        Button start;
        Button stop;
        MyService myService;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_service);
            bind = (Button) findViewById(R.id.bind);
            unbind = (Button) findViewById(R.id.unbind);
            start = (Button) findViewById(R.id.start);
            stop = (Button) findViewById(R.id.stop);
            bind.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ServiceActivity.this, MyService.class);
                    bindService(intent, connection, Context.BIND_AUTO_CREATE);// 第一个参数为意图,第二个参数判断连接状态,第三个参数绑定的一种方式
                }
            });
            unbind.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myService != null) {
                        unbindService(connection);    //接触绑定
                        myService = null;
                    }
                }
            });
            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ServiceActivity.this, MyService.class);
                    startService(intent);    //开始服务
                }
            });
            stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ServiceActivity.this, MyService.class);
                    stopService(intent);    //关闭服务
                }
            });
        }
    
    
        /*ServiceConnection是一个接口,该接口用于监听服务与启动源之间的链接与断开状态*/
        ServiceConnection connection = new ServiceConnection() {
             /*当服务与启动源绑定时调用*/
    
            /**
             * 在这里还涉及到IBinder,当启动源和服务成功链接后,可以获取到IBinder对象,
             * 通过IBinder对象,启动源与服务可以完成通信。
             * 在实际开发中通常采用继承Binder(实现了IBinder接口)来实现自己IBinder对象。
             */
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myService = ((MyService.MyBinder) service).getService();
            }
    
            /*当程序因异常而断开服务与启动源之间链接时调用*/
            @Override
            public void onServiceDisconnected(ComponentName name) {
    
            }
        };
    }

    //自定义的一个服务类
    package
    com.jredu.helloworld.service; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.IBinder; import com.jredu.helloworld.R; public class MyService extends Service { MediaPlayer mediaPlayer; public MyService() { } //自定义类 public class MyBinder extends Binder { public MyService getService() { return MyService.this; } } @Override public void onCreate() { super.onCreate(); mediaPlayer = MediaPlayer.create(this, R.raw.bieli); //在创建方法里创建对象,拿到资源,这里放了一首歌曲资源,注意要改成英文 } @Override public IBinder onBind(Intent intent) { mediaPlayer.start(); return new MyBinder();//绑定成功后,返回了自定义的对象 } @Override public boolean onUnbind(Intent intent) { mediaPlayer.stop(); return super.onUnbind(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // a started service 这种服务一定要写该方法,否则没效果 mediaPlayer.start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); mediaPlayer.stop(); // a started service 必须在Activity关闭前调用stop方法,不然会报错。
        }
    }
  • 相关阅读:
    pytorch环境配置
    Java Review(三十八、网络编程)
    Java Review(三十六、IO)
    Java Review(三十五、注解)
    Java Review(三十四、JDBC)
    Java Review(三十三、异常处理----补充:断言、日志、调试)
    Java Review(三十二、异常处理)
    Java Review(三十一、泛型)
    Java Review (三十、集合----- 操作集合的工具类: Collections)
    Java Review (二十八、集合----- Queue 集合)
  • 原文地址:https://www.cnblogs.com/infernofranz/p/5940553.html
Copyright © 2011-2022 走看看