安卓Service(一)

一.什么是安卓Service
Service是安卓四大组件之一和Activity相当。
Service是可以长时间运行在后台的,是不可见、没有Ui界面的组件。
Service是运行在主线程中的。
Service是可以跨进程调用。
二.为什么要有Service
在实际运营在,绝大多数项目都有在后台运行的需求(如上传,下载),这时就需要通过Service在后台指定完成任务。
三.如何使用Service
这里先介绍startService方式
1.新建类继承Servic
2.重写onCreate方法
3.实现onBind方法
4.重写onStartCommand方法
5.重写onDestroy方法
6.在AndroidManifest中注册Service
7.在有Context环境中通过startService启动Service
8.在有Context环境中通过stopService停止Service
四.代码展示
先创个类继承Servic

package com.example.ll.storeapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by ll on 2018/3/21.
 */

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    //创建(只创建一次)
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("Service", "onCreate");
    }
    //开始(可以进行无数次)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //打印线程名字
        Log.e("Service" + Thread.currentThread().getName(), "onStartCommand");
        //进行一个9秒计时进程
        //主线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Log.e("Service" + Thread.currentThread().getName(), i + "*****");
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    //结束(只结束一次)
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("Service" + Thread.currentThread().getName(), "onDestroy");
    }
}

然后写xml界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ll.storeapplication.Main2Activity">

    <Button
        android:id="@+id/start_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="启动Service" />

    <Button
        android:id="@+id/stop_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="停止Service" />
</LinearLayout>

最后是Main2Activity代码

package com.example.ll.storeapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
private Button qdbtn;
private  Button tzbtn;
private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        bindId();
    }

    private void bindId() {
        qdbtn=findViewById(R.id.start_btn);
        tzbtn=findViewById(R.id.stop_btn);
        qdbtn.setOnClickListener(this);
        tzbtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
    switch (view.getId()){
        case R.id.start_btn:
             intent=new Intent(Main2Activity.this,MyService.class);
            startService(intent);
            break;
        case R.id.stop_btn:
            stopService(intent);
            break;
    }
    }
}

当然不要忘了在AndroidManifest加一句话

  <service android:name=".MyService"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值