四大组件之一Service

本文详细介绍了Android中的Service组件,包括为何需要Service、Service的特点及其不同类型的使用方法。通过实例演示了如何创建并使用startService、bindService及IntentService。

Service

为什么要学习Service

我们在做一些项目时需要让它在后台执行(如:下载,音乐播放)这个时候我们就需要Service。

什么是Service

1.Service是Android四大组件之一,和Activity的级别相当。
2.Service是可以长时运行在后台的, 是不可见,是没有界面的组件。
3.Service是运行在主线程中的。
4.Service可以跨进程调用。

如何使用Service

使用startService方式启动Service步骤
1.新建类继承Service。
2.重写onCreate方法。
(Service在创建时调用的方法,可以用来做数据初始化,Service在没有停止之前,只会执行一次。)
3.实现onBind抽象方法。
(由于Service是一个抽象类,定义了一个onBind抽象方法,return null即可,此方法在startService方式用不上,无需理会即可。)
4.重写onStartCommand方法。
(Service创建后会调用onStartCommand,此方法中可以接收调用者传递过来的参数,并且可以编写需要的逻辑代码,当重复调用Service时,onStartCommand会重复执行,onStart方法弃用)。
5.重写onDestroy方法。
(Service在退出时调用,此方法中可以编写释放资源的操作。)
6.在AndroidManifest中注册Service。
(使用< Service >标签注册,同Activity注册。)
7.在有Context环境中通过startService启动Service。
8.在有Context环境中通过stopService停止Service。

startService启动Service以及Service中耗时操作演示

1.onCreate只会执行一次,只要调用startService,onStartCommand定会执行。
2.Service运行在main线程中,做耗时操作需另开子线程。
3.通过Intent进行传参,在onStartCommand中接收参数。
4.无法获得Service对象,不能直接操作Service中的属性和方法。
5.调用stopService后,Service会执行onDestroy方法后停止。

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

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        //使用Service之前需要注册一下
        <service android:name="MyService" >
        </service>
    </application>

</manifest>
package com.example.pc.service;

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

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

public class MyService extends Service {

    public static final String TAG = "MyService";

    @Override
    public void onCreate() {
        super.onCreate();
        //Thread.currentThread().getName()显示子线程的名字
        Log.e("Service"+Thread.currentThread().getName(), "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("Service", "onStartCommand() executed");
        //Service进行耗时操作
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<=20;i++){
                    Log.e("Service"+Thread.currentThread().getName(),i+"***");
                    try {
                        Thread.sleep(1000);
//                        stopSelf();
                    } 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() executed");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
package com.example.pc.service;

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

public class MainActivity extends Activity implements View.OnClickListener {

    private Button startService;
    private Button stopService;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //赋ID,监听事件
        setContentView(R.layout.activity_main);
        startService = findViewById(R.id.start_service);
        stopService = findViewById(R.id.stop_service);
        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //启动Service
            case R.id.start_service:
                intent = new Intent(this, MyService.class);
                startService(intent);
                break;
            //关闭
            case R.id.stop_service:
                stopService(intent);
                break;
            default:
                break;
        }
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/start_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Service" />

    <Button
        android:id="@+id/stop_service"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop Service" />

</LinearLayout>
bindService操作演示

1.新建类继承Service。
2.实现onBind抽象方法。
3.重写onCreate方法。
4.重写onUnbind方法。
5.重写onDestroy方法。
6.在AndroidManifest中注册Service。
7.在有Context环境中实现ServiceConnection接口。
8.在有Context环境中通过bindService绑定Service。
9.在有Context环境中通过unbindService绑定Service。

package com.example.pc.service;

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

public class ZhangsanActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bindBtn;
    private Button unBindBtn;
    private Button lisiBtn;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //通过管家的中介作用,拿到TestService对象
            TestService testService = ((TestService.Guanjia) service).getServiceObject();
            testService.fly();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        bindID();
    }

    private void bindID() {
        bindBtn = findViewById(R.id.btn1);
        unBindBtn = findViewById(R.id.btn2);
        lisiBtn = findViewById(R.id.btn3);
        unBindBtn.setOnClickListener(this);
        bindBtn.setOnClickListener(this);
        lisiBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1:
//                Intent intent = new Intent(this,TestService.class);
//                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
                Intent intent = new Intent(this,MyIntentService.class);
                startService(intent);
                break;
            case R.id.btn2:
                unbindService(serviceConnection);
                break;
            case R.id.btn3:
                Intent intent1 = new Intent(this,LisiActivity.class);
                startActivity(intent1);
                break;
        }
    }
}
package com.example.pc.service;

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

/**
 * Created by pc on 2018/3/23.
 */

public class TestService extends Service {

    private String TAG = "TestService";

    private Guanjia guanjia = new Guanjia();

    class Guanjia extends Binder{
        //作用:得到当前Service的对象
        public TestService getServiceObject(){
            return TestService.this;
        }
    }

    public void fly(){
        Log.e(TAG,"开飞机");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG,"onBind...");
        return guanjia;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG,"onUnbind");
        return super.onUnbind(intent);
    }

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

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BinderService绑定"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BinderService解绑"/>

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到lisi"/>

</LinearLayout>
IntentService操作演示

1.新建类继承IntentService
2.实现父类的构造方法
3.重写onHandleIntent方法
4.重写Oncreat方法
5.重写onDestory方法
6.在AndroidManifest中注册Service
7.在有Context环境中实现ServiceConnection接口
8.在有Context环境中通过startService绑定Service
9.在有Context环境中通过stopService解绑Service

package com.example.pc.service;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.InputStream;

/**
 * Created by pc on 2018/3/23.
 */

public class MyIntentService extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public MyIntentService(String name) {
        super(name);
    }
    public MyIntentService(){
        super("");

    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        for (int i=0;i<10;i++){
            try {
                Log.e("MyIntentService",i+"***");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
<?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.pc.service.LisiActivity">
<Button
    android:id="@+id/lisi_bind_btn"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="绑定Service"/>
    <Button
        android:id="@+id/lisi_unbind_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="解绑Service"/>
</LinearLayout>
package com.example.pc.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class LisiActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bindBtn;
    private Button unbindBtn;

    private String TAG = "LisiActivity";
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG,"onServiceConnected...");
            TestService testService = ((TestService.Guanjia)service).getServiceObject();
            testService.fly();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG,"onServiceDisconnected...");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lisi);

        bindID();
    }

    private void bindID() {

        bindBtn = findViewById(R.id.lisi_bind_btn);
        unbindBtn = findViewById(R.id.lisi_unbind_btn);

        bindBtn.setOnClickListener(this);
        unbindBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.lisi_bind_btn:
                Intent intent = new Intent(this,TestService.class);
                bindService(intent,connection,BIND_AUTO_CREATE);
                break;
            case R.id.lisi_unbind_btn:
                unbindService(connection);
                break;
        }
    }
}
源码来自:https://pan.quark.cn/s/a4b39357ea24 《C++ Primer》作为C++编程领域中的一部权威著作,主要服务于初学者和经验丰富的开发者,致力于帮助他们深入掌握C++的核心知识。 第一章通常会详细讲解C++语言的基础概念和语法结构,包括变量的使用、数据类型的分类、常量的定义、运算符的应用以及基础的输入输出操作。 接下来,我们将对这一章中的核心知识点和可能的习题解答进行深入分析。 ### 1. 变量与数据类型在C++编程中,变量被视为存储数据的媒介。 每一个变量都必须预先声明其数据类型,常见的数据类型有整型(int)、浮点型(float)、双精度浮点型(double)以及字符型(char)。 例如:```cppint age = 25; // 声明一个整型变量age并赋予其初始值25float weight = 70.5f; // 声明一个浮点型变量weight并赋予其初始值70.5char grade = A; // 声明一个字符型变量grade并赋予其初始值A```### 2. 常量与字面量常量指的是不可更改的值,可以通过`const`关键字进行声明。 例如:```cppconst int MAX_SIZE = 100; // 声明一个整型常量MAX_SIZE,其值为100```字面量是指程序中直接书写的值,如`42`、`3.14`或`"Hello"`。 ### 3. 运算符C++提供了多种运算符,涵盖了算术运算符(+,-,*,/,%)、比较运算符(==,!=,<,>,<=,>=)、逻辑运算符(&&,||,!)以及赋值运算符(=,+=,-=,*=,/=,%=)等。 ### 4. 输入与输出在C++中,使用`std::cin`来实现输...
内容概要:本文详细介绍了一个基于C++的仓库存储管理系统的设计与实现,涵盖了项目背景、目标、挑战及解决方案,并系统阐述了整体架构设计、数据库建模、功能模块划分、权限安全、并发控制、数据一致性保障、异常处理与可扩展性等关键内容。通过面向对象编程思想,采用分层架构与模块化解耦设计,结合STL容器、多线程、锁机制等C++核心技术,实现了高效的库存管理功能,包括入库、出库、盘点、调拨、权限控制、日志追踪与智能报表分析。文中还提供了核心类如Inventory(库存)、User(用户权限)、LogEntry(操作日志)及WarehouseManager(主控制器)的代码示例,展示了数据结构设计与关键算法逻辑。; 适合人群:具备C++编程基础,熟悉面向对象设计与基本数据结构的软件开发人员,尤其适合从事企业级管理系统开发或希望深入理解系统架构设计的中级开发者(工作1-3年);也适用于计算机相关专业学生进行课程设计或毕业项目参考; 使用场景及目标:①学习如何使用C++构建复杂业务系统的整体架构与模块划分方法;②掌握高并发、数据一致性、权限控制、异常处理等企业级系统关键技术的实现思路;③理解仓储管理业务流程及其在软件系统中的建模与落地方式;④为开发类似ERP、MES等后台管理系统提供技术原型与设计参考; 阅读建议:此资源不仅提供理论架构与代码片段,更强调系统设计的完整性与工程实践性。建议读者结合代码示例动手实现核心模块,深入理解类之间的关系与交互逻辑,重点关注多线程安全、事务管理与权限校验等难点环节,并尝试扩展功能如对接GUI界面或数据库持久化模块,以全面提升系统开发能力。
农作物叶子健康与疾病实例分割数据集 一、基础信息 数据集名称:农作物叶子健康与疾病实例分割数据集 图片数量: - 训练集:7,446张图片 - 验证集:970张图片 - 测试集:182张图片 - 总计:8,598张图片 分类类别: - Apple Healthy(健康苹果叶) - Apple Rust Leaf(苹果锈病叶) - Apple Scab Leaf(苹果黑星病叶) - BellPepper Healthy(健康甜椒叶) - BellPepper Leaf Spot(甜椒叶斑病) - Corn Gray Leaf Spot(玉米灰斑病) - Corn Leaf Blight(玉米叶枯病) - Corn Rust Leaf(玉米锈病叶) - Grape Black Rot(葡萄黑腐病) - Grape Healthy(健康葡萄叶) - Squash Powdery Leaf(南瓜白粉病叶) - Tomato Bacterial Spot(番茄细菌性斑点病) - Tomato Healthy(健康番茄叶) - Tomato Septoria(番茄斑枯病) 标注格式:YOLO格式,包含实例分割的多边形标注,适用于实例分割任务。 数据格式:图片来源于农业图像数据库,细节清晰。 二、适用场景 农业植物疾病AI检测系统开发:数据集支持实例分割任务,帮助构建能够自动识别植物叶子疾病区域并分类的AI模型,辅助农民快速诊断和治理。 精准农业应用研发:集成至农业智能管理系统中,提供实时疾病识别功能,为农作物健康管理提供决策支持。 学术研究与创新:支持农业科学与人工智能交叉领域的研究,助力发表高水平农业AI论文。 农业教育与培训:数据集可用于农业院校或培训机构,作为学生学习植物疾病分类和诊断的重要资源。 三、数据集优势 精准标注与多样性:每张图片均经过准确标注,确保疾病区域分割精确。包
### Service 的作用 Service 是 Android 四大组件之一,其主要作用是在后台执行长时间运行的任务,而不需要与用户进行交互。它没有界面,适用于执行如网络请求、播放音乐、文件 I/O 等需要在后台持续运行的操作。Service 可以独立于 Activity 运行,即使用户切换到其他应用,Service 仍然可以在后台继续运行 [^1]。 此外,Service 也可以被其他应用调用,从而提供一些特定的功能,例如后台数据处理或资源共享 [^2]。 ### Service 的生命周期 Service 的生命周期相对简单,主要包含以下几个关键方法: - `onCreate()`:当 Service 第一次被创建时调用,用于执行一次性初始化操作。 - `onStartCommand(Intent intent, int flags, int startId)`:当通过 `startService()` 启动 Service 时调用,用于处理传入的 Intent 请求。 - `onBind(Intent intent)`:当通过 `bindService()` 绑定 Service 时调用,返回一个 `IBinder` 对象用于实现组件间的通信。 - `onUnbind(Intent intent)`:当解除绑定时调用。 - `onDestroy()`:当 Service 被销毁时调用,用于释放资源。 根据启动方式的不同,Service 的生命周期会有所不同。 ### Service 的启动方式 Service 可以通过两种方式启动: 1. **通过 `startService()` 启动** - 使用 `startService()` 启动的 Service 会独立运行,与启动它的组件没有直接的绑定关系。 - 一旦启动,Service 会一直运行直到调用 `stopSelf()` 或外部调用 `stopService()`。 - 适用于执行一次性任务,例如下载文件或播放音乐。 2. **通过 `bindService()` 启动** - 使用 `bindService()` 启动的 Service 会与调用者(例如 Activity)建立绑定关系。 - 通过 `onBind()` 方法返回的 `IBinder` 对象,调用者可以与 Service 进行交互。 - 当调用者解除绑定时,Service 不会立即停止,只有当所有绑定都解除后,才会调用 `onUnbind()` 并最终调用 `onDestroy()` [^4]。 ### Service 的使用示例 #### 启动 Service ```java Intent serviceIntent = new Intent(context, MyService.class); context.startService(serviceIntent); ``` #### 停止 Service ```java Intent serviceIntent = new Intent(context, MyService.class); context.stopService(serviceIntent); ``` #### 绑定 Service ```java Intent serviceIntent = new Intent(context, MyService.class); context.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE); ``` 其中,`serviceConnection` 是一个实现了 `ServiceConnection` 接口的对象,用于接收 Service 绑定后的回调。 #### 解除绑定 ```java context.unbindService(serviceConnection); ``` ### Service 的声明 每个 Service 都需要在 `AndroidManifest.xml` 文件中进行声明,否则无法正常运行。声明方式如下: ```xml <service android:name=".MyService" /> ``` ### 前台 Service 如果需要让 Service 在前台运行,以避免被系统优先级机制杀死,可以使用 `startForegroundService()` 方法启动 Service,并在 Service 中调用 `startForeground()` 方法,将 Service 提升为前台服务 [^3]。 ```java Intent serviceIntent = new Intent(context, MyForegroundService.class); context.startForegroundService(serviceIntent); ``` 在 Service 的 `onStartCommand()` 方法中: ```java Notification notification = new Notification.Builder(this, "channel_id") .setContentTitle("Foreground Service") .setSmallIcon(R.drawable.ic_notification) .build(); startForeground(1, notification); ``` ### 注意事项 - **资源管理**:由于 Service 在后台运行,需要注意资源的合理使用,避免过度消耗系统资源。 - **生命周期管理**:使用 `bindService()` 启动的 Service 必须通过 `unbindService()` 解除绑定,否则可能导致内存泄漏。 - **权限声明**:某些特殊用途的 Service(例如前台服务)可能需要在清单文件中声明额外的权限。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值