Android 향상 서비스 용법 인 스 턴 스 분석

7176 단어 AndroidService
앞의 글 은 Activity 와 Intent 의 사용 을 소개 하고 본 고 는 Service 를 소개 합 니 다.액 티 비 티 를 프런트 프로그램 에 비유 하면 서 비 스 는 백 스테이지 프로그램 이 고 서비스의 전체 라 이 프 사이클 은 백 스테이지 에서 만 수행 된다.서 비 스 는 Activity 와 마찬가지 로 Intent 에서 호출 됩 니 다.프로젝트 에 서 비 스 를 추가 하려 면 서 비 스 를 계승 하 는 클래스 를 새로 만 든 다음 AndroidManifest.xml->Application->Application Nodes 의 Service 태그 에 추가 합 니 다.
 서 비 스 는 Activity 가 startService 나 bindService 를 통 해 시작 하고 Intent 는 인 자 를 전달 하 는 것 을 책임 집 니 다.
 
 먼저 본 프로그램의 실행 캡 처 를 다음 과 같이 붙 입 니 다.

본 고 는 주로 Service 의 호출 과 그 생명 주 기 를 설명 한다.

위의 그림 은 startService 이후 stopService 의 서비스 상태 변화 입 니 다.

위의 그림 은 bindService 이후 unbindService 의 Service 상태 변화 입 니 다.
startService 와 bindService 는 모두 서 비 스 를 시작 할 수 있 습 니 다.그러면 그들 사이 에는 어떤 차이 가 있 습 니까?이들 의 차 이 는 바로 Service 의 주 기 를 바 꾸 는 것 이다.startService 에서 시작 하 는 서 비 스 는 stopService 가 있어 야 합 니 다.stopService 를 호출 하지 않 으 면 Activity 가 끝나 고 Service 가 실행 되 고 있 습 니 다.bindService 가 시작 하 는 서 비 스 는 unbindService 로 끝 날 수도 있 고 Activity 가 끝 난 후에(onDestroy)자동 으로 끝 날 수도 있 습 니 다.

위의 그림 은 startService 이후 Activity.finish()의 서비스 상태 변화 로 Service 가 달리 고 있다.

위의 그림 은 bindService 이후 Activity.finish()의 Service 상태 변화 이 고 Service 는 마지막 에 자동 으로 unbindService 입 니 다.
main.xml 코드 는 다음 과 같 습 니 다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:id="@+id/btnStartMyService"
 android:text="StartMyService"></Button>
 <Button android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:id="@+id/btnStopMyService"
 android:text="StopMyService"></Button>
 <Button android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:id="@+id/btnBindMyService"
 android:text="BindMyService"></Button>
 <Button android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:id="@+id/btnUnbindMyService"
 android:text="UnbindMyService"></Button>
 <Button android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:id="@+id/btnExit"
 android:text="    "></Button>
</LinearLayout>

testService.java 의 원본 코드 는 다음 과 같 습 니 다.

package com.testService;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class testService extends Activity {
  Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);
    btnStartMyService.setOnClickListener(new ClickEvent());
    
    btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);
    btnStopMyService.setOnClickListener(new ClickEvent());
    
    btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);
    btnBindMyService.setOnClickListener(new ClickEvent());
    
    btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);
    btnUnbindMyService.setOnClickListener(new ClickEvent()); 
    
    btnExit=(Button)this.findViewById(R.id.btnExit);
    btnExit.setOnClickListener(new ClickEvent());
  }
  @Override
  public void onDestroy()
  {
   super.onDestroy();
   Log.e("Activity","onDestroy");
  }
  
  private ServiceConnection _connection = new ServiceConnection() { 
 @Override
 public void onServiceConnected(ComponentName arg0, IBinder arg1) {
  // TODO Auto-generated method stub
 }

 @Override
 public void onServiceDisconnected(ComponentName name) {
  // TODO Auto-generated method stub
 } 
  }; 
  class ClickEvent implements View.OnClickListener{

 @Override
 public void onClick(View v) {
  Intent intent=new Intent(testService.this,MyService.class);
  if(v==btnStartMyService){
  testService.this.startService(intent);
  }
  else if(v==btnStopMyService){
  testService.this.stopService(intent);
  }
  else if(v==btnBindMyService){
  testService.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE);
  }
  else if(v==btnUnbindMyService){
  if(MyService.ServiceState=="onBind")//Service         
   testService.this.unbindService(_connection);
  }
  else if(v==btnExit)
  {
  testService.this.finish();
  }
  
 }
   
  }
}

MyService.java 의 원본 코드 는 다음 과 같 습 니 다.

package com.testService;

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

public class MyService extends Service {
 static public String ServiceState="";
 @Override
 public IBinder onBind(Intent arg0) {
 Log.e("Service", "onBind");
 ServiceState="onBind";
 return null;
 }
 @Override
 public boolean onUnbind(Intent intent){
 super.onUnbind(intent);
 Log.e("Service", "onUnbind");
 ServiceState="onUnbind";
 return false;
 
 }
 @Override
 public void onCreate(){
 super.onCreate();
 Log.e("Service", "onCreate");
 ServiceState="onCreate";
 }
 @Override
 public void onDestroy(){
 super.onDestroy();
 Log.e("Service", "onDestroy");
 ServiceState="onDestroy";
 }
 @Override
 public void onStart(Intent intent,int startid){
 super.onStart(intent, startid);
 Log.e("Service", "onStart");
 ServiceState="onStart";
 }

}

좋은 웹페이지 즐겨찾기