zoukankan      html  css  js  c++  java
  • Service1

    一、认识Service

    它是在后台运行,不可交互的一个东西,不能自己运行,需要通过某一个Activity或者其它的Context对象来调用,如Context.startService()和Context.bindService()二种方式启动Service。

    需要说明的是:如果在Service的onCreate()或者onStart()方法里面做一些很耗时的动作,最好是启动一个新线程来运行这个Service。因为如果Service是运行在主线程中,会影响程序的UI操作或者阻塞主线程中的其它事情。

    应用场景:播放多媒体的时候用户启动了其它Activity,这个时候程序要在后台继续播放;检测SD卡上文件的变化等等。

    二、Service的生命周期

    1、通过startService启动

    onCreate->onStart->onDestroy。

    如果是调用者自己直接退出而没有调用stopService,Service会一直在后台运行。

    2、通过bindService启动

    只会运行onCreate,这个时候调用者和Service绑定在一起,如果调用者退出了,Service就会调用onUnbind->onDestroy,和绑定者一起退出。

    3、如果这几个方法交织在一起

    一个原则是Service的onCreate方法只会被调用一次,就是无论多少次的启动或者绑定,Service只被创建一次。

    如果是先绑定了,就停止不掉,也就是StopService不能用了,只能先UnbindService,再StopService,所以是先启动还是先绑定,是有区别的。

    三、可以通过以下示例来验证:

    先写一个Service:

    public class ServiceDemo extends Service {

    @Override
    public IBinder onBind(Intent intent) {
    System.out.println("onBind");
    return null;
    }

    @Override
    public void onCreate() {
    System.out.println("onCreate");
    super.onCreate();
    }

    @Override
    public void onDestroy() {
    System.out.println("onDestroy");
    super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    System.out.println("onStart");
    return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
    System.out.println("onUnbind");
    return super.onUnbind(intent);
    }

    }

    再写一个调用者:

    public class MainActivity extends Activity {
    private Button btstart, btstop, btbind, btunbind;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btstart = (Button) findViewById(R.id.bt_start);
    btstop = (Button) findViewById(R.id.bt_stop);
    btbind = (Button) findViewById(R.id.bt_bind);
    btunbind = (Button) findViewById(R.id.bt_unbind);

    btstart.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    startservice();
    }
    });
    btstop.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    stopservice();
    }
    });
    btbind.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    bindservice();
    }
    });
    btunbind.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    unbindservice();
    }
    });
    }

    public void startservice(){
    Intent i=new Intent(this,ServiceDemo.class);
    this.startService(i);
    }
    public void stopservice(){
    Intent i=new Intent(this,ServiceDemo.class);
    this.stopService(i);
    }

    private Boolean isBound=false;
    ServiceConnection conn=new ServiceConnection() {

    public void onServiceDisconnected(ComponentName name) {

    }

    public void onServiceConnected(ComponentName name, IBinder service) {

    }
    };
    public void bindservice(){
    Intent i=new Intent(this,ServiceDemo.class);
    this.bindService(i, conn, Context.BIND_AUTO_CREATE);
    isBound=true;
    }

    public void unbindservice(){
    if(isBound){
    unbindService(conn);
    }
    isBound=false;
    }
    }

     未完待续……………………

  • 相关阅读:
    File System Minifilter Drivers(文件系统微型过滤驱动)入门
    使用Windows驱动的虚拟打印机,打印Excel表格无表格线问题解决(2)
    使用Windows驱动的虚拟打印机,打印Excel表格无表格线问题解决(1)
    lettcode 上的几道哈希表与链表组合的数据结构题
    计蒜之道 百度AI小课堂-上升子序列
    Educational Codeforces Round 40 F. Runner's Problem
    第13届 广东工业大学ACM程序设计大赛 C题 平分游戏
    Educational Codeforces Round 39 (Rated for Div. 2) G
    Codeforces Round #466 (Div. 2) E. Cashback
    cf 460 E. Congruence Equation 数学题
  • 原文地址:https://www.cnblogs.com/lyz459/p/2540619.html
Copyright © 2011-2022 走看看