当前位置:操作系统 > 安卓/Android >>

Service的生命周期

Service是四大组件之一:主要用于在后台执行一些比较耗时的操作,如音乐播放,数据下载等...
Service分为两种:startService和bindService
 
下面分别介绍两种Service:
1、Started:
A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
 
解释:这种Service通过调用startService()方法启动,一旦启动,调用者和服务之间没有任何关系,即使调用者不存在了,服务仍然会执行
2、Bound:
A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server inte易做图ce that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed
 
解释:这种Service通过调用bindService启动,这种Service可以和调用者进行交互,一旦调用者调用unbindService,那么该服务就会停止
 
 
下面简单的介绍第一种Service的使用:
使用第一种Service可以继承IntentService,也可以继承Service,代码如下:
[java]
public class MyIntentService extends IntentService 

    private int count=0; 
 
    public MyIntentService() 
    { 
        super("intentService"); 
        // TODO Auto-generated constructor stub 
    } 
 
    @Override 
    protected void onHandleIntent(Intent intent) 
    { 
        // TODO Auto-generated method stub 
        System.out.println("onHandleIntent 执行"+count++); 
        try 
        { 
            Thread.sleep(1000); 
        } catch (InterruptedException e) 
        { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        System.out.println("Service Thread=====>"+Thread.currentThread().getId()); 
 
    } 
 

 
[java]
public class MyService extends Service 

     
     
    private int count=0; 
 
    @Override 
    public IBinder onBind(Intent intent) 
    { 
        // TODO Auto-generated method stub 
        return null; 
    } 
 
    @Override 
    public void onCreate() 
    { 
        // TODO Auto-generated method stub 
        super.onCreate(); 
        System.out.println("Service Thread=======>"+Thread.currentThread().getId()); 
        System.out.println("onCreate()"); 
    } 
 
    @Override 
    public void onDestroy() 
    { 
        // TODO Auto-generated method stub 
        super.onDestroy(); 
        System.out.println("onDestroy()"); 
    } 
 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
        // TODO Auto-generated method stub 
         
        new Thread() 
        { 
 
            @Override 
            public void run() 
            { 
                // TODO Auto-generated method stub 
                System.out.println("onStartCommand()"+count++); 
                try 
                { 
                    Thread.sleep(2000); 
                } catch (InterruptedException e) 
                { 
                    // TODO Auto-generated catch block 
                    e.printStackTrace(); 
                } 
                System.out.println("Service Thread=========>"+Thread.currentThread().getId()); 
                 
            } 
             
        }.start(); 
         
        return Service.START_STICKY; 
         
    } 
     
     
 

当我们使用第一种方式时,只需要改写protected void onHandleIntent(Intent intent),其他的生命周期函数父类已经帮我们完成好了,在这里要说明一点:
Service是和调用者运行在同一个线程里面的,

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,