03_Service binding
12144 단어 service
When the Service started by Bind Service, the Application exits, and the Service exits.
Start Service and Stop Service just start the service and cannot communicate. Only by binding with the service through the Bind Service can it communicate with the service.
package com.example.servdemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener, ServiceConnection {
private Button btnStartService, btnStopService, btnBindService, btnUnbindService, btnGetCurrentNum;
private Intent serviceIntent;
private EchoService echoService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent = new Intent(this, EchoService.class);
btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService);
btnBindService = (Button) findViewById(R.id.btnBindService);
btnUnbindService = (Button) findViewById(R.id.btnUnbindService);
btnGetCurrentNum = (Button) findViewById(R.id.btnGetCurrentNum);
btnStartService.setOnClickListener(this);
btnStopService.setOnClickListener(this);
btnBindService.setOnClickListener(this);
btnUnbindService.setOnClickListener(this);
btnGetCurrentNum.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStartService:
startService(serviceIntent);
break;
case R.id.btnStopService:
stopService(serviceIntent);
break;
case R.id.btnBindService:
bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.btnUnbindService:
unbindService(this);
echoService = null;
break;
case R.id.btnGetCurrentNum:
if (echoService != null) {
System.out.println("The Number is "+echoService.getCurrentNum());
}
break;
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
System.out.println("onServiceConnected!");
echoService = ( (EchoService.EchoServiceBinder)binder ).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
package com.example.servdemo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class EchoService extends Service {
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind!");
return echoServiceBinder;
}
private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder();
public class EchoServiceBinder extends Binder{
public EchoService getService() {
return EchoService.this;
}
}
public int getCurrentNum(){
return i;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("Service Start!");
startTimer();
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("Service Stop!");
stopTimer();
super.onDestroy();
}
private int i = 0;
public void startTimer(){
if(timer == null){
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
i++;
System.out.println(i);
}
};
timer.schedule(timerTask, 1000, 1000);
}
}
public void stopTimer(){
if(timer != null){
timerTask.cancel();
timer.cancel();
timer = null;
timerTask = null;
}
}
private Timer timer = null;
private TimerTask timerTask = null;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
03_Service bindingThe Service started by the Start Service, the Application exits, and the Service will not exit; When the Service started...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.