zoukankan      html  css  js  c++  java
  • Service服务

    1:服务是什么?它是没有界面的进程。跟activity相比,只是少界面。
    2:服务的启动方式。
     (1)启动服务和停止服务。
     启动服务生命周期:--onCreate---onStartCommand---onStart---onDestory
     再次启动服务:onStartCommand ----onStart---onDestory
     当activity启动服务后,那么activity跟服务没有关系了。
     (2)绑定服务。
     生命周期:onCreate----onbind----onunbind---onDestory.
     再次绑定不会调用方法。
    3.比较启动服务和绑定服务的区别
     (1)生命周期的区别。
     启动服务---onStartCommand.
     绑定服务---onbind---onunbind方法。
     (2)服务和activity的关系
     启动服务后,启动者跟服务没有关系,会在后台一直运行。activity销毁,服务不会销毁。
     绑定服务后,服务跟activity一起销毁,activity销毁,启动的服务也会销毁。
     (3)能否调用服务中的方法
     启动服务后不能调用服务中的方法。
     绑定服务可以调用服务中的方法。
     (4)运行状态
     启动服务后,会在后台一直运行。
     但是绑定服务,看不到服务在运行。

    IntentService与Service有什么区别?

    IntentService是Service的子类

    1)Service如果要处理异步请求(如耗时操作)的话,需要自己启动一个worker thread,如果启动多个worker thread来处理相关事务的话,各线程之间无法很好的控制。而IntentService可以将多个异步请求的Intent放入队列中,封装了一个开启worker thread的onHandleIntent()方法,来依次处理放入队列中的Intent,只有一个worker thread在运行,同一时刻只有一个Intent被处理,当一个Intent处理完了之后才会处理另外一个Intent。

    2)Service可以通过startService()或者bindService()方法来启动,而IntentService则是通过startService()方法来启动。

    Service适合多线程处理异步请求。而IntentService则适合处理需要排队的请求。

    *使用服务下载图片

    public class MainActivity extends AppCompatActivity {
    
        private Intent intent;
        public static Handler handle = new Handler() ;
        public static ImageView imageView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            intent = new Intent(this,MyService.class);
            imageView = (ImageView) findViewById(R.id.imageview);
            intent.putExtra("key","http://www.baidu.com/img/bdlogo.gif");
        }
    
        public void startService(View view){
            startService(intent);
        }
    
        public void stopService(View view){
            stopService(intent);
        }
    }
    public class MyService extends Service {
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.i("service","---onCreate"+Thread.currentThread());
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i("service","---onStartCommand"+Thread.currentThread());
    
            final String path = intent.getStringExtra("key");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL(path);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        int code = connection.getResponseCode();
                        if(code == 200){
                            final Bitmap bitmap = BitmapFactory.decodeStream(connection.getInputStream());
                            MainActivity.handle.post(new Runnable() {
                                @Override
                                public void run() {
                                    MainActivity.imageView.setImageBitmap(bitmap);;
                                }
                            });
                        }
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            Log.i("service","---onStart"+Thread.currentThread());
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.i("onDestroy","---onDestroy"+Thread.currentThread());
        }
    }

     * MainActivity调用Service中的方法(前提是因为Service中的对象不能new出来)

    public class MyService extends Service {
    
        class MyBinder extends Binder{
            public MyService returnBinder(){
                return MyService.this;
            }
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            Log.i("Service","onbind");
            MyBinder binder = new MyBinder();
            return binder;
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Log.i("Service","onUnbind");
            return super.onUnbind(intent);
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.i("Service","onCreate");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.i("Service","onDestroy");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
        }
    
        public void play(){
            Log.i("Service","开始播放");
        }
    
        public void pause(){
            Log.i("Service","暂停播放");
        }
    }
    public class MainActivity extends AppCompatActivity {
    
        private Intent intent;
        private MyServiceConnection myServiceConnection;
        private MyService myService;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            intent = new Intent(this,MyService.class);
            myServiceConnection = new MyServiceConnection();
        }
    
        public void bind(View v){
            bindService(intent,myServiceConnection,BIND_AUTO_CREATE);
        }
    
        public void unbind(View v){
            unbindService(myServiceConnection);
        }
    
        public void play(View v){
            myService.play();
        }
    
        public void pause(View v){
            myService.pause();
        }
        class MyServiceConnection implements ServiceConnection{
    
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MyService.MyBinder binder = (MyService.MyBinder) service;
                myService = binder.returnBinder();
                Log.i("Service","onServiceConnected");
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.i("Service","onServiceDisconnected");
            }
        }
    }

    技巧:

    MyService extends Service

    MyBinder extends Binder extends IBinder

    MyService - onBind() = IBinder -> MyBinder - returnBinder() = MyService

    所以MyService通过onBind()方法得到IBinder,强转MyBinder后通过returnBinder()方法得到自己的对象。前半句是在Service中执行,后半句是在MainActiviy中执行,IBinder是中间人

  • 相关阅读:
    OpenLayer 3 鹰眼控件和全屏显示
    OpenLayer 3 鼠标位置坐标显示控件
    Openlayer 3 图层列表控件(自定义)
    OpenLayers 3 的地图基本操作
    小米范工具系列之十四:小米范网站批量爬虫工具
    ruby所有版本下载地址
    常用代码块:使用时间生成数据库文件名
    收集些常用的正则--以后慢慢添加
    小米范工具系列最新下载地址
    小米范工具系列之十三:小米范验证码登录爆破工具
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5390568.html
Copyright © 2011-2022 走看看