标签:
package com.example.shiyanshi.learnservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
return new Binder(); //要被重写,否则会弹出上面的错误
}
//执行startService时被调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service onStartCommand");
new Thread(new Runnable(){
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("******************Service is running********************");
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
// @Override
// public void onCreate() {
// super.onCreate();
// System.out.println("**************Service onCreate*****************");
// }
//
// @Override
// public void onDestroy() {
// super.onDestroy();
// System.out.println("****************Service onDestroy***************");
// }
}
BIND_AUTO_CREATE
, BIND_DEBUG_UNBIND
, BIND_NOT_FOREGROUND
, BIND_ABOVE_CLIENT
, BIND_ALLOW_OOM_MANAGEMENT
, or BIND_WAIVE_PRIORITY
.注意当第二个参数传递this时,还要在当前Activity类中实现onServiceConnected和onServiceDisconnected,否则会出现类型不匹配的错误,其中onServiceConnected函数在绑定服务成功后执行,onServiceDisconnected服务所在的进程崩溃或被杀掉的时候被执行。public class MainActivity extends Activity implements View.OnClickListener, ServiceConnection {
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent=new Intent(MainActivity.this,MyService.class);
findViewById(R.id.btnStartService).setOnClickListener(this);
findViewById(R.id.btnStopService).setOnClickListener(this);
findViewById(R.id.btnBindService).setOnClickListener(this);
findViewById(R.id.btnUnbindService).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.btnStartService:
startService(intent);
break;
case R.id.btnStopService:
stopService(intent);
break;
case R.id.btnBindService:
bindService(intent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.btnUnbindService:
unbindService(this);
break;
default:
break;
}
}
//服务被绑定成功成功后执行
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
System.out.println("***************Service Connect Successful********************");
}
//服务所在的进程崩溃或被杀掉的时候被执行
@Override
public void onServiceDisconnected(ComponentName componentName) {
System.out.println("***************Service disconnect Successful********************");
}
}
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return new Binder();
}
//执行startService时被调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("**************Service onCreate*****************");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("****************Service onDestroy***************");
}
}
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return new Binder();
}
//执行startService时被调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("**************Service onCreate*****************");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("****************Service onDestroy***************");
}
}
标签:
原文地址:http://www.cnblogs.com/ql698214/p/5110974.html