一、使用Service
1.右击java文件夹,选择新建Service,然后重写其中的onStartCommand函数,只要执行了startService函数,onStartCommand便会被执行
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***************");
// }
}
2.启动service时调用startService函数,关闭service时调用stopService函数,该函数需要一个Intent,无论是像下述方式创建intent还是通过
startService(new Intent(MainActivity.this,MyService.class));stopService(new Intent(MainActivity.this,MyService.class));打开或者关闭的服务均是同一个
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(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(intent);
}
});
findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(intent);
}
});
二、绑定Service
1.绑定Service时调用bindService函数,public abstract boolean bindService (Intent service, ServiceConnection conn, int flags) 其第二个参数表示服务的连接,服务的连接主要用来侦听服务的状态,可以传递this指针(当传递this时该服务便与当前Activity绑定在一起了,当退出当前Activity时,服务便会自动被系统关闭(调用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***************");
}
}
2.Service程序
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***************");
}
}