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

來源:互聯網
上載者:User

標籤:

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);    }}

服務端代碼如下:

 
<pre name="code" class="java">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裡的各種方法的調用。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.