标签:
package com.example.yabushan.hello3;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
private boolean SERVICE_STATE=true;//为true时,将执行onStartCommand里面的线程
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return new Binder();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {//
System.out.println("startCommand");
new Thread(){
@Override
public void run() {
super.run();
while(SERVICE_STATE){
System.out.println("服务在运行。。。");
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("service create");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("service destroy");
SERVICE_STATE=false;
}
}
package com.example.yabushan.hello3;
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;
public class MainActivity extends Activity implements View.OnClickListener, ServiceConnection {
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
intent=new Intent(MainActivity.this,MyService.class);
findViewById(R.id.startService).setOnClickListener(this);
findViewById(R.id.stopService).setOnClickListener(this);
findViewById(R.id.bindService).setOnClickListener(this);
findViewById(R.id.unbindService).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.startService:
startService(intent);break;
case R.id.stopService:
stopService(intent);break;
case R.id.bindService:
bindService(intent,this, Context.BIND_AUTO_CREATE);break;
case R.id.unbindService:
unbindService(this);break;
}
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("服务绑定成功");
}
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("服务解绑成功");
}
}
服务启动的输出:
11-21 05:32:23.028 3906-3906/? I/System.out: service create
11-21 05:32:23.028 3906-3906/? I/System.out: startCommand
11-21 05:32:23.045 3906-3953/? I/System.out: 服务在运行。。。
11-21 05:32:25.059 3906-3953/? I/System.out: 服务在运行。。。
11-21 05:32:27.070 3906-3953/? I/System.out: 服务在运行。。。
服务停止:
11-21 05:32:37.314 3906-3906/? I/System.out: service destroy
服务绑定:
11-21 05:33:07.262 3906-3906/? I/System.out: service create
11-21 05:33:07.277 3906-3906/? I/System.out: 服务绑定成功
服务解绑:
11-21 05:33:19.618 3906-3906/? I/System.out: service destroy
标签:
原文地址:http://www.cnblogs.com/yabushan/p/4983624.html