Service,是android的四大組件之一,沒有界面,優先級高於Activity,運行在後臺,執行一些耗時的操作,比如播放音樂,執行數據的加載,請求。
注意的是:運行在主線程,不能用它來做耗時的請求或者動作可以在服務中開一個線程,在線程中做耗時動作
Service的啓動與綁定:
工程概況:
佈局只有四個按鈕,不列:
清單文件的聲明:
MainActivity:
package ivo_chuanzhi.service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
private Intent intent;
ServiceConnection conn = new ServiceConnection() {
//服務被綁定成功的時候回調
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("綁定成功");
}
//被綁定的服務斷線時候會回調
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("綁定失敗");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//操作的是同一個Service,不管是一個Intent實例啓動還是兩個Intent實例
intent = new Intent(MainActivity.this, MyService.class);
Button btnStart = (Button) findViewById(R.id.btnStartService);
Button btnStop = (Button) findViewById(R.id.btnStopService);
Button btnBind = (Button) findViewById(R.id.btnBindService);
Button btnUnBind = (Button) findViewById(R.id.btnUbBindService);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
btnBind.setOnClickListener(this);
btnUnBind.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnStartService:
//啓動服務:1、startServicer 2、綁定服務
startService(intent);
break;
case R.id.btnStopService:
//停止服務
stopService(intent);
break;
case R.id.btnBindService:
//綁定服務
bindService(intent, conn, Context.BIND_AUTO_CREATE);
break;
case R.id.btnUbBindService:
//解除綁定
unbindService(conn);
break;
}
}
}
package ivo_chuanzhi.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
private boolean ServicerRunning = true;
public MyService() {
}
//Service都必須實現的方法
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//沒綁定的話
//throw new UnsupportedOperationException("Not yet implemented");
//如果實現了綁定了,需要實現onBind接口,返回一個Binder對象,通過Binder可以實現與綁定的activity通訊
return new Binder();//可以重寫Binder,方便業務與activity通訊
}
//在activity裏面啓動了startService(new Intent(MainActivity.this, MyService.class));
//的方法都會回調這個函數,可重複執行
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
/* new Thread(){
@Override
public void run() {
super.run();
while(true) {
System.out.println("服務在運行。。。");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();*/
System.out.println();
return super.onStartCommand(intent, flags, startId);
}
//創建服務後第一個回調該函數,只執行一次
@Override
public void onCreate() {
super.onCreate();
System.out.println("我的服務被創建了");
new Thread(){
@Override
public void run() {
super.run();
while(ServicerRunning) {
System.out.println("服務在運行。。。");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
//stopService被執行後會回調
@Override
public void onDestroy() {
//stopService和UnBind都會關掉服務
System.out.println("我的服務被關掉了");
ServicerRunning = false;
}
}
源碼裏面的註釋已經很詳細的,在此就不再贅述
謝謝