Android 향상 서비스 용법 인 스 턴 스 분석
서 비 스 는 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";
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.