Android Service服务(有例子)

服务(Service)是Android四大组件之一,无界面且能在后台长时间运行。适合执行后台任务,如数据下载、音乐播放。创建Service通过右键新建,输入名称。示例包括activity_main.xml, MainActivity.java, MusicService.java和AndroidManifest.xml的配置,运行时注意添加相关权限。" 106316962,8498940,git教程:将本地分支推送到远程同名分支,['git'],"['git', 'push', '本地分支', '远程分支', '同步']
摘要由CSDN通过智能技术生成

服务

1.服务简介
服务(Service)是Android中的四大组件之一,是一个长期运行在后台的用户组件,没有用户界面。即使切换到另一个应用程序,服务也可以在后台运行,因此服务更适合执行一段时间而又不需要显示界面的后台操作,例如下载数据,播放音乐等。

2.服务的创建
在程序包上右键,选择【new】>>【Service】>>【Service】选项,在弹出窗口上输入名称即可。
在这里插入图片描述
那两个一般都选上。

3.具体例子

activity_main.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:background="@drawable/bg"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_inputPath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Music/a.mp3"
        android:textColor="#09C9EB"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_play"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/play"
            android:drawablePadding="3dp"
            android:gravity="center"
            android:textSize="30sp"
            android:text
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值