服务
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