Android 四大組件之Service 的生命周期和使用

Service簡介:

Service 是Android的四大組件之一,一般用於沒有UI界面,長期執行的後臺任務,即使程序退出時,後臺任務還在執行。比如:音樂播放。

Service的誤區:

1.service在UI線程中執行。 2.不可以在service中執行耗時任務,因為service是在UI線程中運行的。 3.如果需要執行後臺的耗時任務,必須在Service中開啟一個線程來執行。

Service的生命周期:

啟動和停止Service的兩種方式 1.context.startService();context.stopService(). 2.context.bindService();context.unbindService().

Service使用實例1:

客戶端代碼如下:

package com.xjp.broadcast;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

    private final static String action = "com.xjp.MainActivity";

    private TextView result;

    private Button startService;
    private Button stopService;



    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            int i = intent.getIntExtra("key", 0);

            result.setText(i + "");
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        result = (TextView) findViewById(R.id.result);

        startService = (Button) findViewById(R.id.startService);
        startService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent service = new Intent(MainActivity.this, CalculationService.class);
                startService(service);
            }
        });

        stopService = (Button) findViewById(R.id.stopService);
        stopService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent service = new Intent(MainActivity.this, CalculationService.class);
                stopService(service);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        initFilter();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }

    private void initFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(action);
        registerReceiver(broadcastReceiver, filter);
    }


}

1.依次點擊“啟動服務” 和“停止服務” ,生命周期 打印結果如下:

2.點擊兩次 “啟動服務” 打印結果如下:

這種方式可以多次啟動同一個Service,並且 隻有第一次才執行 onCreate。第二次之後 就隻執行 onStartCommand,並且當且僅當該應用退出之後,該服務依然存在後臺運行著。退出之後,在設置裡面查看正在運行的應用:

服務端代碼如下:

package com.xjp.broadcast;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

/**
 * Description:
 * User: xjp
 * Date: 2015/5/4
 * Time: 14:14
 */

public class CalculationService extends Service {

    private final static String TAG = "CalculationService";
    private final static String action = "com.xjp.MainActivity";

    private boolean quit = false;



    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "====onBind=====");
        return null;
    }


    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG, "====onUnbind=====");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        Log.e(TAG, "====onCreate=====");
        super.onCreate();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "====onStartCommand=====");
        startThread();
        return super.onStartCommand(intent, flags, startId);


    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "====onDestroy=====");
        quit = true;
        super.onDestroy();
    }


    /**
     * 開啟線程模擬耗時任務
     */
    public void startThread() {
        new Thread(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                while (i < 200 && !quit) {
                    try {
                        Thread.sleep(1000);
                        i++;
                        Intent intent = new Intent(action);
                        intent.putExtra("key", i);
                        sendBroadcast(intent);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

}

Service使用實例2:

客戶端代碼如下:

package com.xjp.broadcast;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

    private final static String action = "com.xjp.MainActivity";

    private TextView result;

    private Button startService;
    private Button stopService;

    private CalculationService calculationService;


    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            int i = intent.getIntExtra("key", 0);

            result.setText(i + "");
        }
    };

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            calculationService = ((CalculationService.CalulationBinder) service).getService();
            calculationService.startThread();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            calculationService = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        result = (TextView) findViewById(R.id.result);

        startService = (Button) findViewById(R.id.startService);
        startService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent service = new Intent(MainActivity.this, CalculationService.class);
                bindService(service, serviceConnection, Context.BIND_AUTO_CREATE);
            }
        });

        stopService = (Button) findViewById(R.id.stopService);
        stopService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(serviceConnection);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        initFilter();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }

    private void initFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(action);
        registerReceiver(broadcastReceiver, filter);
    }


}

服務端代碼如下:



package com.xjp.broadcast; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; /** * Description: * User: xjp * Date: 2015/5/4 * Time: 14:14 */ public class CalculationService extends Service { private final static String TAG = "CalculationService"; private final static String action = "com.xjp.MainActivity"; private boolean quit = false; private IBinder binder = new CalulationBinder(); public class CalulationBinder extends Binder { public CalculationService getService() { return CalculationService.this; } } @Override public IBinder onBind(Intent intent) { Log.e(TAG, "====onBind====="); return binder; } @Override public boolean onUnbind(Intent intent) { Log.e(TAG, "====onUnbind====="); return super.onUnbind(intent); } @Override public void onCreate() { Log.e(TAG, "====onCreate====="); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "====onStartCommand====="); startThread(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e(TAG, "====onDestroy====="); quit = true; super.onDestroy(); } /** * 開啟線程模擬耗時任務 */ public void startThread() { new Thread(new Runnable() { int i = 0; @Override public void run() { while (i < 200 && !quit) { try { Thread.sleep(1000); i++; Intent intent = new Intent(action); intent.putExtra("key", i); sendBroadcast(intent); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } } 


一次點擊“啟動服務”按鈕和 “停止服務” 按鈕,生命周期 打印結果如下:


綁定服務,如果退出的時候沒有解除綁定 ,也就是沒有調用 context.unbindService().的話,會報錯誤。所以,一般都在客戶端的 onDestory()中調用 context.unbindService(). 所以這種服務不太適合後臺長時間的任務。 這種服務綁定和解除綁定是成對出現的。

兩種Service總結:

第一種,非綁定的Service,應用退出時可以不停止服務退出。可以讓服務一直在後臺運行。缺點:啟動服務即 啟動後臺任務,不能很好的在客戶端隨時控制 服務的後臺任務執行時間。 第二種,綁定的Service,應用退出時必須解除綁定Service,否則程序報錯。優點:不過綁定的Service可獲得 Service 的Binder,可以靈活控制 Service裡的各種方法的調用。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。