zoukankan      html  css  js  c++  java
  • Android Service

    Service概念和用途

    简单来说它是运行在后台的程序,当我们的应用关闭的时候它并没有关闭,而是在后台继续运行.就像我们的短信,虽然我们没有去请求,它还是会当有短信发过来的时候自动弹出提示框,"您有新的信息".又像音乐播放器,我们一遍看小说的时候可以一遍听音乐.

    Service的生命周期

    继承了onCreate(),onStart(),onDestroy()这三个方法.需要注意的是:当我们的服务如果已经开启.那么第二次重新创建它的时候,它不会走onCreate()方法,而是直接onStart().

    下面贴出代码

    Service类

    View Code
    public class BindService extends Service
    {
    private int count;
    private boolean quit;
    private double LAT;//经度
    private double LNG;//纬度
    // 定义onBinder方法所返回的对象
    private MyBinder binder = new MyBinder();
    // 通过继承Binder来实现IBinder类
    public class MyBinder extends Binder
    {
    public int getCount()
    {
    // 获取Service的运行状态:count
    return count;
    }
    public double getLAT(){
    return LAT;
    }
    public double getLNG(){
    return LNG;
    }
    }
    // 必须实现的方法
    @Override
    public IBinder onBind(Intent intent)
    {
    System.out.println("Service is Binded");
    // 返回IBinder对象
    return binder;
    }
    // Service被创建时回调该方法。
    @Override
    public void onCreate()
    {
    super.onCreate();
    System.out.println("Service is Created");
    // 启动一条线程、动态地修改count状态值
    new Thread()
    {
    @Override
    public void run()
    {
    while (!quit)
    {
    try
    {
    Thread.sleep(1000);
    }
    catch (InterruptedException e)
    {
    }
    count++;
    }
    }
    }.start();
    }
    // Service被断开连接时回调该方法
    @Override
    public boolean onUnbind(Intent intent)
    {
    System.out.println("Service is Unbinded");
    return true;
    }
    // Service被关闭之前回调。
    @Override
    public void onDestroy()
    {
    super.onDestroy();
    this.quit = true;
    System.out.println("Service is Destroyed");
    }

    @Override
    public void onRebind(Intent intent)
    {
    super.onRebind(intent);
    this.quit = true;
    System.out.println("Service is ReBinded");
    }

    }

    Activity类

    View Code
    Button bind , unbind , getServiceStatus;
    // 保持所启动的Service的IBinder对象
    BindService.MyBinder binder;
    // 定义一个ServiceConnection对象
    private ServiceConnection conn = new ServiceConnection()
    {
    // 当该Activity与Service连接成功时回调该方法
    @Override
    public void onServiceConnected(ComponentName name
    , IBinder service)
    {
    System.out.println("--Service Connected--");
    // 获取Service的onBind方法所返回的MyBinder对象
    binder = (BindService.MyBinder) service;
    }
    // 当该Activity与Service断开连接时回调该方法
    @Override
    public void onServiceDisconnected(ComponentName name)
    {
    System.out.println("--Service Disconnected--");
    }
    };
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // 获取程序界面中的start、stop、getServiceStatus按钮
    bind = (Button) findViewById(R.id.bind);
    unbind = (Button) findViewById(R.id.unbind);
    getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
    //创建启动Service的Intent
    final Intent intent = new Intent();
    //为Intent设置Action属性
    intent.setAction("org.crazyit.service.BIND_SERVICE");
    bind.setOnClickListener(new OnClickListener()
    {
    @Override
    public void onClick(View source)
    {
    //绑定指定Serivce
    bindService(intent , conn , Service.BIND_AUTO_CREATE);
    }
    });
    unbind.setOnClickListener(new OnClickListener()
    {
    @Override
    public void onClick(View source)
    {
    //解除绑定Serivce
    unbindService(conn);
    }
    });
    }
    }






  • 相关阅读:
    20190425-快速排序
    ExtJS合同管理信息系统源码
    2013年12月12日16Aspx源码发布详细
    大型商业手机进销存管理系统源码
    大管家固定资产管理系统源码
    地平线开源网站源码Deepsoon v1.2.3
    发票管理系统(Extjs)源码
    国讯通用OA协同办公系统源码
    物流管理系统源码
    2013年10月16Aspx源码发布详情
  • 原文地址:https://www.cnblogs.com/CoolChen/p/Service.html
Copyright © 2011-2022 走看看