Android四大組件之Service小試牛刀

1,Service是什麼

 service:他是無界面,執行耗時較長的app 組件

 優先級高於Activity


2,service不是什麼

   service:不是進程   不是線程

3,service的分類

    StartSevice

        1, START_STICKY:如果service進程被kill掉,保留service的狀態爲開始狀態,但不保留遞送的intent對象。隨後系統會嘗試重新創建service,由於服務狀態爲開始狀態,所以創建服務後一定會調用onStartCommand(Intent,int,int)方法。如果在此期間沒有任何啓動命令被傳遞到service,那麼參數Intent將爲null。

        2,START_NOT_STICKY:“非粘性的”。使用這個返回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統不會自動重啓該服務。

        3,START_REDELIVER_INTENT:重傳Intent。使用這個返回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統會自動重啓該服務,並將Intent的值傳入。


        4,START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保證服務被kill後一定能重啓
    
    BinderSevice
4,啓動service
  • >1,通過startService()方法啓動的
  • >2,一旦被啓動  就會無限制的在後臺執行  直到裏面的操作執行完畢 纔會被銷燬
  • >3,一旦啓動不會向啓動者返回任何結果
  • >4,啓動的組件被銷燬   服務不會被銷燬

5綁定service
  • >1,通過bindService()方法綁定的
  • >2,可以和啓動者組件進行交互  啓動者組件可以獲取服務的對象  可以調用裏面的方法
  • >3,多個程序組件可以綁定到同一個服務上  多個組件的綁定被解除時  纔會被銷燬
6,intent servicer
>裏面封裝裏一個工作線程

一個線程能滿足操作時  那麼就可以用IntentService


7 ,使用場景

   播放音樂,監聽用戶某一操作,記錄地理位置變化

實例1--開啓和停止服務

MainActivity.java

package com.example.week5_day1_server01;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

/**
 * 步驟:1 創建一個類,繼承Service 
 * 2 實現onCreate,onStartCommand,onDestroy方法 
 * 3 在清單配置文件裏註冊服務 
 * 4使用startService啓動服務,發送一個意圖 
 * 5 使用stopService發送一個意圖停止服務
 */
public class MainActivity extends Activity {

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

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start:
			//創建意圖
			Intent service = new Intent(getApplicationContext(),
					MyService.class);
			startService(service);//開啓服務
			break;
		case R.id.stop:
			Intent service2 = new Intent(getApplicationContext(),
					MyService.class);//創建意圖
			stopService(service2);//停止服務
			break;

		default:
			break;
		}
	}
}
自定義Service類

package com.example.week5_day1_server01;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
/**
 * 新建一個類,繼承Service
 */
public class MyService  extends Service{

	private static final String TAG="MyService";
	@Override
	public void onCreate() {//服務創建時被調用
		// TODO Auto-generated method stub
		super.onCreate();
		Log.i(TAG, "--------onCreate--------");
	}
	/*
	 * 啓動服務時調用
	 *  
	 * intent:表示啓動的組件傳遞過來的intent對象
	 * flags:標記
	 * startId:表示請求的標識
	 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.i(TAG, "--------onStartCommand--------");
		return super.onStartCommand(intent, flags, startId);
	}
	/*
	 * 銷燬服務
	 */
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i(TAG, "--------onDestroy--------");
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.i(TAG, "--------onBind--------");
		return null;
	}

}
佈局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="啓動服務" />
    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="停止服務" />

</LinearLayout>
清單配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.week5_day1_server01"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.week5_day1_server01.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 註冊服務 -->
        <service android:name="com.example.week5_day1_server01.MyService"></service>
    </application>

</manifest>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章