一. Service 简介
Android 开发者指导文档
二. 继承 Service 创建启动服务
继承 Service 来创建服务,是在当前进程当前线程里运行这个服务,也就是主线程来运行这个服务。
// 创建 Service 类
public class MyService extends Service{
@Override
public void onCreate() {
super.onCreate();
Log.i("test", "onCreate...");
}
// 启动 Service 传递过来的 Intent
// flags 是一个标志位,一般情况下是 0,或者是额外的配置信息
// startId 从1递增,表示第几次被调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("test", "onStartCommand...");
Log.i("test", "flags:"+flags);
Log.i("test", "startId:"+startId); // 1 2 3 4 5 6 ...
new Thread(){
@Override
public void run() {
super.run();
for(int i=1;i<=10;i++){
Log.i("test", "i:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// this.stopself(); // 停止自己
MyService.this.stopSelf(); // 停止自己
}
}.start();
// return Service.START_NOT_STICKY; // 当 Service 被系统销毁掉时不会重新创建
// return Service.START_STICKY; // 当 Service 被系统销毁掉时会重新创建
return Service.START_REDELIVER_INTENT; // 重新创建 Service 时,会重新传递 Intent
}
}
// 在 Activity 中启动 Service
public class MainActivity extends AppCompatActivity {
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, MyService.class);
}
// 这是一个 Button 的回调,启动 Service
public void start(View View) {
startService(intent);
}
// 这是一个 Button 的回调,停止 Service
public void stop(View View) {
stopService(intent);
}
}
三. 继承 IntentService 创建启动服务
使用 IntentService 是默认在另一个线程来运行这个服务。
// 创建 IntentService
public class MyService extends IntentService {
@Override
public void onCreate() {
super.onCreate();
Log.i("test", "onCreate");
}
public MyService() {
super("My Service");
Log.i("test", "MyService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("test", "onHandleIntent");
// 打印线程的名称
Log.i("test", "Thread name:"+Thread.currentThread().getName());
for(int i=1;i<=5;i++) {
Log.i("test", "i:" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.stopSelf();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("test", "onDestroy");
}
}
// Activity 中启动这个 Service
public class MainActivity extends AppCompatActivity {
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, MyService.class);
}
public void start(View view) {
startService(intent);
}
public void stop(View view) {
stopService(intent);
}
}
四. 继承 Binder 创建绑定服务
// 继承 Service 创建一个服务
public class MyService extends Service {
Random r = new Random();
@Override
public void onCreate() {
super.onCreate();
Log.i("test", "onCreate");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
// 当 Bind 时,会调用 onBind() 方法
Log.i("test", "onBind");
return new MyBinder();
}
public int nextInt() {
return r.nextInt(100);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("test", "onDestroy");
}
// 继承 Binder,创建一个 Binder 类
public class MyBinder extends Binder{
// 返回这个服务
public MyService getMyService() {
return MyService.this;
}
}
}
// Activity 绑定服务
public class MainActivity extends AppCompatActivity {
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, MyService.class);
}
// 绑定服务
public void bind(View view) {
// 调用 Activity 的 bindService() 函数
// Service.BIND_AUTO_CREATE 表示当绑定时服务没有被创建,将自动创建它
// conn 是 ServiceConnection 的对象
this.bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
// 解绑
public void unbind(View view) {
// 调用 Activity 的 unbindService() 函数
this.unbindService(conn);
}
//
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("test", "onServiceConnected");
// 拿到 Service
MyService.MyBinder myBinder = (MyService.MyBinder)service;
MyService myService = myBinder.getMyService();
Log.i("test", "i=" + myService.nextInt());
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("test", "onServiceDisconnected");
}
};
}
五. 使用 Messager 创建绑定服务
Messager 是 AIDL 的一种封装 ,用于 Client 和 Service 之间传递消息的通道。一般用于两个进程之间的通信。
// 服务端创建 Service
public class MyService extends Service {
// 处理 Client 发来的消息
class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Toast.makeText(getApplicationContext(), "Hello",Toast.LENGTH_SHORT).show();
}
}
// 用 handler 创建一个 Messager
Messenger m = new Messenger(new MyHandler());
// onBind 中返回 Messager 的实例
@Nullable
@Override
public IBinder onBind(Intent intent) {
return m.getBinder();
}
}
// 客户端绑定服务
public class MainActivity extends AppCompatActivity {
Intent intent;
Messenger m;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建 Intent,要设置包名
intent = new Intent("com.geek99.MY_SERVICE");
intent.setPackage("com.geek99.demo_08_05_server");
}
// 这是一个 Button 的回调函数,调用 bindService
public void bind(View view) {
bindService(intent,conn, Service.BIND_AUTO_CREATE);
}
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
m = new Messenger(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
// 这是一个 Button 的回调函数,用从 Service 得到的 Messager 对象发送消息
public void send(View view) {
if (m != null) {
try {
m.send(Message.obtain());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
六. 使用 AIDL 创建绑定服务
(未完待续)