《Android第四章》服务Servince

 《Android第一行代码》书本记录

 

Service(服务)是什么:

Service(服务):Service是Andorid中实现程序后台运行的解决方案,它非常适合去执行那些不需要和用户交互而且还要求长期运行的任务。服务的运行不依赖于任何用户界面,即使程序被切换到后台,或者用户打开了另外一个应用程序,服务仍然能够保持正常运行。

注意:Service并不是运行在一个独立的进程当中的,而是依赖于创建服务时所在的应用程序进程。当某个应用进程被杀掉时,所有依赖于该进程的服务也会停止运行,除非手动调用才能停止服务。当然这是只是通过startActivity()方法启动的Service,如果是通过bindService()方法绑定了Service,绑定服务,调用者挂了,服务也会跟着挂掉,绑定者可以调用服务里面的方法。

 

Service启动的第一种方式:

  •  采用startService的方式开启服务:
  • 使用Service的步骤:  
    
    1.定义一个类继承Service
       public class MyService extends Service {
    
            @Nullable
            @Override
            public IBinder onBind(Intent intent) {
                return null;
            }
    
            public MyService() {
                super();
            }
    
            @Override
            public void onCreate() {
                Log.d("MyService", "onCreate: executed ");
                super.onCreate();
            }
    
            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                //处理逻辑
                Log.d("MyService", "onStartCommand: executed ");
                return super.onStartCommand(intent, flags, startId);
            }
    
            @Override
            public void onDestroy() {
                Log.d("MyService", "onDestroy: executed ");
                super.onDestroy();
            }
        }
    
    2.在Manifest.xml文件中配置该Service
         <service android:name=".MainActivity$MyService"
                android:enabled="true"
                android:exported="true"
                >            
                
            </service>
    
    3.使用Context的startService(Intent)方法启动该Service
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Intent intent=new Intent(MainActivity.this,MyService.class);
            startService(intent);
    
        }
    
    }
    
    4.不再使用时,调用stopService(Intent)方法停止该服务
    public class MainActivity extends AppCompatActivity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Intent intent=new Intent(MainActivity.this,MyService.class);
            stopService(intent);
    
    
        }
    }
    注意:除了调用stopService()方法去停止服务,还可以在Service中任何一个位置调用stopSelf()方法去停止

    使用这种start方式启动的Service的生命周期如下:
    onCreate()--->onStartCommand()onStart()方法已过时) --->onDestory()

    说明:如果服务已经开启,不会重复的执行onCreate(), 而是会调用onStartCommand()
    服务停止的时候调用 onDestory()。服务只会被停止一次。

    特点:一旦服务开启跟调用者(开启者)就没有任何关系了。
    开启者退出了,开启者挂了,服务还在后台长期的运行。
    开启者不能调用服务里面的方法。

Service启动的第二种方式:

  •  采用bindService的方式开启服务:
  • 使用Service的步骤:
    
    
    1.定义一个类继承Service
     public class MyService extends Service {
    
            private DownloadBinder downloadBinder=new DownloadBinder();
            @Nullable
            @Override
            public IBinder onBind(Intent intent) {
                return downloadBinder;
            }
    
            public MyService() {
                super();
            }
    
            class DownloadBinder extends Binder{
                public void startDownload(){
                    Log.d("MyService", "startDownload:-----> ");
                }
    
            }
    
            @Override
            public void onCreate() {
                Log.d("MyService", "onCreate: executed ");
                super.onCreate();
            }
    
            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                //处理逻辑
                Log.d("MyService", "onStartCommand: executed ");
                return super.onStartCommand(intent, flags, startId);
            }
    
            @Override
            public void onDestroy() {
                Log.d("MyService", "onDestroy: executed ");
                super.onDestroy();
            }
        } 
    2.在Manifest.xml文件中配置该Service
    <service android:name=".MainActivity$MyService" android:enabled="true" android:exported="true" > </service> 3.使用Context的bindService(Intent, ServiceConnection, int)方法启动该Service public class MainActivity extends AppCompatActivity { private MyService.DownloadBinder downloadBinder; private ServiceConnection serviceConnection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) {
    ownloadBinder
    = (MyService.DownloadBinder) service;
    //可以调用downloadBinder里面的方法了 downloadBinder.startDownload(); } @Override
    public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent=new Intent(MainActivity.this,MyService.class); bindService(intent,serviceConnection,BIND_AUTO_CREATE);//第一个参数intent,第二个是serviceConnection,第三个是BIND_AUTO_CREATE常量,表示自动创建bind
    
    
    } }

    4.不再使用时,调用unbindService(ServiceConnection)方法停止该服务:
    unbindService(serviceConnection);
    //传入serviceConnection即可

    使用这种start方式启动的Service的生命周期如下:
    onCreate() --->onBind()--->onunbind()--->onDestory()

    注意:绑定服务不会调用onstart()或者onstartcommand()方法

    特点:bind的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。
    绑定者可以调用服务里面的方法。

 

 

 

 

posted @ 2021-06-30 12:49  白晨里的影子  阅读(80)  评论(0)    收藏  举报