}
public void showNumber(){
new Thread(new Runnable() {
int i=1;
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i++);
}
}
}).start();
}
}
HelloServiceActivity.java
package hb.android.service;
import hb.android.service.HelloService.HelloBinder;
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;
import android.widget.TextView;
public class HelloServiceActivity extends Activity {
Intent intent;
boolean flag = true;
HelloService mService;;
boolean mBound = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intent = new Intent();
initMyWidget();
setMyWidgetListener();
}
public void setMyWidgetListener() {
MyWidget.btn_bind.setOnClickListener(new WidgetOnClickListener());
MyWidget.btn_start.setOnClickListener(new WidgetOnClickListener());
MyWidget.btn_stop.setOnClickListener(new WidgetOnClickListener());
MyWidget.btn_unbind.setOnClickListener(new WidgetOnClickListener());
MyWidget.btn_kill.setOnClickListener(new WidgetOnClickListener());
}
class WidgetOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start:
intent.setClass(getApplicationContext(), HelloService.class);
intent.putExtra(“test”, “start”);
startService(intent);
flag = true;
MyWidget.btn_stop.setEnabled(flag);
break;
case R.id.btn_stop:
intent.putExtra(“test”, “stop”);
stopService(intent);
flag = false;
MyWidget.btn_stop.setEnabled(flag);
break;
case R.id.btn_kill:
android.os.Process.killProcess(android.os.Process.myPid());
break;
case R.id.btn_bind:
Intent intent = new Intent(getApplicationContext(), HelloService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);//自动调用Service里的onCreate方法
System.out.println(“bind:”+mService);
opeService();
break;
case R.id.btn_unbind:
if (mBound) {
unbindService(mConnection);//自动调用Service的onDestory方法
mBound = false;
}
break;
default:
break;
}
}
public void opeService() {
System.out.println(“opeService:”+mService);
// mService.showNumber();
}
}
public void initMyWidget() {
MyWidget.btn_bind = (Button) findViewById(R.id.btn_bind);
MyWidget.btn_start = (Button) findViewById(R.id.btn_start);
MyWidget.btn_stop = (Button) findViewById(R.id.btn_stop);
MyWidget.btn_unbind = (Button) findViewById(R.id.btn_unbind);
MyWidget.tv_test = (TextView) findViewById(R.id.tv_test);
MyWidget.btn_kill = (Button) findViewById(R.id.btn_kill);
}
private static class MyWidget {
static Button btn_start;
static Button btn_stop;
static Button btn_bind;
static Button btn_unbind;
static Button btn_kill;
@SuppressWarnings(“unused”)
static TextView tv_test;
}
@Override
protected void onDestroy() {
super.onDestroy();
stopService(intent);
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
// We’ve bound to LocalService, cast the IBinder and get LocalService instance
HelloBinder binder = (HelloBinder) service;
mService = binder.getService();
System.out.println(“ServiceConnection:”+mService);
mBound = true;
mService.showNumber();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
}
};
@Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(mConnection);
mBound = false;
}
};
}
注意:当服务创建之后由,onServiceConnected 这个方法负责建立与Servicer的连接
意思当Activit用BindService启动服务之后,由:
public ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println(“onServiceConnected”);
mBound = false;
}
/**
- 服务创建之后收些方法建立Serivce与Activity的连接