Service的使用

首先说一下Service(服务)是什么,服务是Android的四大组件,运行在后台的没有界面的Activity,后台是不依赖UI就可以独立存在的,服务一般在内存不够用的情况下才会被杀死,不然只能手动关闭,如果你发现你的应用很耗电,看看后台Service多不多,关掉一些没有用的Service.

给大家贴上简单代码,明天给大家上传一个多线程上传断电续传的demo

package com.xiaojie.com.xiaojie.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

/**
 * 创建Service服务
 * Created by Administrator on 2015/10/20.
 */
public class DownloadService extends Service {

    public static final String tag = "DownloadService";
    public MyBinder binder = new MyBinder();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(tag, "onCreate()");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(tag, "onStartCommand()");

        new Thread(){
            @Override
            public void run() {
               //开始后台执行任务
            }
        }.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {

        return binder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(tag, "onDestroy()");
    }

    public class MyBinder extends Binder {

        public void startDownLoad() {
            Log.e(tag, "downLoad()");
            new Thread(){
                @Override
                public void run() {
                    //开始后台执行任务
                }
            }.start();
        }
    }
}

package com.xiaojie.com.xiaojie.ui;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

import com.xiaojie.com.xiaojie.R;
import com.xiaojie.com.xiaojie.service.DownloadService;

/**
 * Created by Administrator on 2015/10/20.
 */
public class ServiceActivity extends Activity implements View.OnClickListener {
    private Button bt_start_server;
    private Button bt_stop_server;
    private Button bt_bind_server;
    private Button bt_unbind_server;
    private DownloadService.MyBinder binder;
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            binder = (DownloadService.MyBinder) iBinder;
            binder.startDownLoad();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.atv_service);
        bt_start_server = (Button) findViewById(R.id.bt_start_server);
        bt_stop_server = (Button) findViewById(R.id.bt_stop_server);
        bt_bind_server = (Button) findViewById(R.id.bt_bind_server);
        bt_unbind_server = (Button) findViewById(R.id.bt_unbind_server);

        bt_bind_server.setOnClickListener(this);
        bt_stop_server.setOnClickListener(this);
        bt_start_server.setOnClickListener(this);
        bt_unbind_server.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.bt_start_server:
                intent = new Intent(ServiceActivity.this, DownloadService.class);
                startService(intent);
                break;
            case R.id.bt_stop_server:
                intent = new Intent(ServiceActivity.this, DownloadService.class);
                stopService(intent);
                break;
            case R.id.bt_bind_server:
                intent = new Intent(ServiceActivity.this, DownloadService.class);
                bindService(intent, serviceConnection, BIND_AUTO_CREATE);
                break;
            case R.id.bt_unbind_server:
                unbindService(serviceConnection);
                break;
        }
    }
}

 

大家可以运行看下效果。多点点

大家可以发现,Service在第一次启动时会先执行onCreate,然后执行onStartCommand方法,之后添加再次调用只会执行onStartCommand方法。

启动服务分为两种方式,一种是startServer,一种是bindServer.

当一个Service被startServer后,只能被StopServer,服务被startServer后,并且还可以被bind,

bindServer方便与Activity进行交互,把Server的binder对象控制权交给Activity.

如果是startServer只能在onstartCommand方法中通过Handler然后发送广播在Activity中接收



Server分为两种,一种是本地服务,和远程服务


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值