《Android笔记3.7》 认识 Android Service

课程背景:
Service 是 Android 四大基本组件之一,是无界面的应用程序,可以长期在后台运行,在实际工作中非常重要,比如接收推送消息、在锁屏状态下侦听传感器信息。

核心内容:
1.启动Service
2.绑定Service

使用 Service

新建Service文件:MyService.java

 

重写Serivce的启动函数:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        new Thread(){
            @Override
            public void run() {
                super.run();
                while (true) {
                    System.out.println("服务正在运行...");

                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        return super.onStartCommand(intent, flags, startId);
    }

 

启动Service:

startService(new Intent(MainActivity.this, MyService.class));

停止Service:

//一个程序中Service只能存在一个,新创建一个Intent并不影响最终效果
stopService(new Intent(MainActivity.this, MyService.class));

 

 

绑定 Service

Service和Activity的连接可以用ServiceConnection来实现。你需要实现一个新的ServiceConnection,重写onServiceConnected和onServiceDisconnected方法,一旦连接建立,你就能得到Service实例的引用。

@Override
public IBinder onBind(Intent intent) {
  // 绑定后,必须写以下返回!
  return new Binder();
}


// Reference to the service
private MyService.Binder serviceBinder;

// Handles the connection between the service and activity
private ServiceConnection mConnection = new ServiceConnection(){
  
  public void onServiceConnected(ComponentName className, IBinder service) {
    // Called when the connection is made.
    serviceBinder = (MyService.MyBinder)service;
  }

  public void onServiceDisconnected(ComponentName className) {
    // Received when the service unexpectedly disconnects.
    serviceBinder = null;
  }

};

 

执行绑定,调用bindService方法,传入一个选择了要绑定的Service的Intent(显式或隐式)和一个你实现了的ServiceConnection实例

Intent bindIntent = new Intent(MyActivity.this, MyService.class);
bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);

一旦Service对象找到,通过onServiceConnected处理函数中获得serviceBinder对象就能得到它的公共方法和属性。

 

详细可以看我整理的《关于Activity绑定到Service上的作用和方法过程》:

http://www.cnblogs.com/woodk/articles/4705600.html

 

 

Service生命周期

使用者Activity:

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
    System.out.println("Service Connected");
}

@Override
    public void onServiceDisconnected(ComponentName name) {
}

 

被使用的Service:

有三个主要生命周期

  @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("Service start");
        serviceRunning = true;
        new Thread(){
            @Override
            public void run() {
                super.run();
                while (serviceRunning) {
                    System.out.println("服务正在运行...");

                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        return super.onStartCommand(intent, flags, startId);
    }

  //继承SerivceConnection接口后必须重载以下两个方法 @Override
public void onCreate() { super.onCreate(); System.out.println("Service create"); } @Override public void onDestroy() { super.onDestroy(); System.out.println("Service destroy"); serviceRunning = false; }

 

startService ——> onCreate, onStart ——> stopService ——> onDestroy

(start不能通过unBindService来停止,绑定解除是连接到Activity的,而Service start是整个应用程序的。

后退出界面会自动解除绑定,而不会停止服务)

同样的,bindService来创建的Service实例,也不能通过stopService来Destroy掉。

 

startService,bindService ——> onCreate, onStart ——> stopService,unBindService ——> onDestroy

其中后退出当前界面后等同于销毁了Activity,也就等于unBindService了.

posted @ 2015-08-05 16:07  暖风叔叔  阅读(341)  评论(0)    收藏  举报