android中Service使用startService

一.什么是Service?

  Service(服务)是一个一种可以在后台执行长时间运行操作而没有用户界面的应用组件。服务可由其他应用组件启动(如Activity),服务一旦被启动将在后台一直运行,即使启动服务的组件(Activity)已销毁也不受影响。
  其生命周期为:
  这里写图片描述

二.Service作为android四大组件之一,那么什么情况下我们会使用到Service呢?在这里我把它归纳成两种情况得运用场景:

1、用于长期执行某些操作,并且甚至与UI(主)线程没有交互。比如启动app直接去网络下载文件

2、跨进程间通信,比如appA程序中Service被appB中程序调用
三.使用startService:
1.新建一个类使其继承service:

package com.example.administrator.myapplicat456;

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

/**
 * Created by Administrator on 2018/3/21/021.
 */

public class TestStartService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("Myservice","oncreate executed");
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<30;i++){
                    try {
                        Thread.sleep(1000);
                        stopSelf();
                        Log.e("Myservice",i+".......");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        Log.e("Myservice","onStartCommand executed");
        return super.onStartCommand(intent, flags, startId);
    }

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

2.在Manifest中加入service:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.myapplicat456">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
        </activity>
        <activity android:name=".Main2Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <service
            android:name=".TestStartService"
            android:enabled="true"
            android:exported="true"></service>
    </application>

</manifest>

3.Mainactivity中代码为:

package com.example.administrator.myapplicat456;

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 startservice;
private Button stopservice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        startservice=findViewById(R.id.start_service);
        stopservice=findViewById(R.id.stop_service);
        startservice.setOnClickListener(this);
        stopservice.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_service:
                Intent startintent=new Intent(this,TestStartService.class);
                startService(startintent);
                break;
            case R.id.stop_service:
                Intent stopintent=new Intent(this,TestStartService.class);
                stopService(stopintent);
                break;
                default:

                    break;
        }

    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值