zoukankan      html  css  js  c++  java
  • Service(一):认识service、绑定Service

         Activity是与用户打交道的,而Service是在后台运行的。

         这个程序介绍了下如何启动和停止一个Service,以及在后台打印消息,我添加了一些注释。

        在activity_main中将布局改为线性布局,方向改为垂直并添加两个按钮,

        

     android:orientation="vertical"
    <Button
            android:layout_width="69dp"
            android:layout_height="wrap_content"
            android:text="启动服务"
            android:id="@+id/btnStartService"
            android:layout_weight="0.06" />
        <Button
            android:layout_width="69dp"
            android:layout_height="wrap_content"
            android:text="停止服务"
            android:id="@+id/btnStopService"
            android:layout_weight="0.06" />
    View Code

         在MainActivity中

         

     intent = new Intent(MainActivity.this,MyService.class);//启动另一个活动
    
            findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startService(intent);
                }
            });
    
            findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stopService(intent);
                }
            });
    View Code

       

        在MyService中,负责在后台打印消息,注意如何创建一个线程:

       

     public int onStartCommand(Intent intent, int flags, int startId) {
            //startService()启动时,这个函数自动启动
            new Thread(){
                //创建一个新线程
                @Override
                public void run() {
                    super.run();
    
                    while (true) {
                        System.out.println("服务正在运行...");
    
                        try {
                            sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            return super.onStartCommand(intent, flags, startId);
        }
    View Code

    具体参考: http://www.jikexueyuan.com/course/683.html

         

     接下来讲的是绑定Service。课程只讲了怎么用,但没有讲为什么用。整个过程和上节差不多,定义两个按钮来监听启动绑定Serivce功能。

     在MainActivity中,增加两个按钮(注意接口监听的方式,和用匿名类的方式进行对比)

     

     //采用接口的方式,下面要实现相应的接口
      findViewById(R.id.btnBindService).setOnClickListener(this);
      findViewById(R.id.btnUnbindService).setOnClickListener(this);
     public void onClick(View v) {
    
            switch (v.getId()){
                case R.id.btnStartService:
                    startService(intent);
                    break;
                case R.id.btnStopService:
                    stopService(intent);
                    break;
                case R.id.btnBindService:
                    //第二个参数要求一个ServiceConnection,实现两个接口
    
                    bindService(intent,this, Context.BIND_AUTO_CREATE);
                    break;
                case R.id.btnUnbindService:
                    unbindService(this);
                    break;
            }
        }
    
     //实现的两个接口
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
    
            System.out.println("System connected!");
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {
    
        }
    View Code

    同时在MyService的onBind函数中,修改其返回值。

    @Override
        public IBinder onBind(Intent intent) {
           return new Binder();
        }

     Service生命周期

        服务的生命周期涉及到两个函数,onCreate()和onDestroy()。按下启动服务和绑定服务时,第一个会被调用;反之,第二个会被调用。如果同时按下启动和绑定服务,只需要同时解除绑定

    和停止服务,onDestroy()才会被调用。

       按下启动服务,按返回键(退出activity)后,服务仍会运行;但对于绑定服务,服务却会退出运行(绑定就是activity和服务之间的事啊!)。

     @Override
        public void onCreate() {
            super.onCreate();
            System.out.println("Service Create");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            System.out.println("Service destroy");
    
        }

         当不断按下启动服务时,onCreate只会调用一次,但onStartCommand()函数每次都会被调用(startService()每次都会调用它)。

    
    
  • 相关阅读:
    爬虫——Selenium与PhantomJS
    爬虫——多线程糗事百科案例
    爬虫——json模块与jsonpath模块
    爬虫——使用BeautifulSoup4的爬虫
    爬虫——BeautifulSoup4解析器
    爬虫——爬取百度贴吧每个帖子里面的图片
    爬虫——爬虫中使用正则表达式
    爬虫——正则表达式re模块
    爬虫——requests模块
    爬虫——Handler处理器 和 自定义Opener
  • 原文地址:https://www.cnblogs.com/573177885qq/p/4875738.html
Copyright © 2011-2022 走看看