首页 编程 软件学院 查看内容

Android之Service

2014-11-18 11:59 1193 0

摘要: 服务是运行在后台的一段代码。不是进程,也不是线程,但它运行在进程和线程中。Android中的服务与Activity不同,不能与用户交互,不能自己启动。媒体播放器的服务,当用户退出媒体选择用户界面,仍然...
关键词: nbsp 服务 运行 不同 用户界面 线程 媒体播放器 plaincopy 后台 进程

服务是运行在后台的一段代码。不是进程,也不是线程,但它运行在进程和线程中。Android中的服务与Activity不同,不能与用户交互,不能自己启动。媒体播放器的服务,当用户退出媒体选择用户界面,仍然希望音乐依然可以继续播放,这就是由服务Service来保证当用户界面关闭时音乐继续播放。当我们某个应用的数据是通过网络获取的,不同时间的数据是不同的,这时我们可以用Service在后台定时更新,而不用每次打开应用的时候去获取。实例:ServiceDemo运行效果:代码清单:AndroidManifest.xml[html] view plaincopy<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="com.rainsong.servicedemo"        android:versionCode="1"        android:versionName="1.0">        <uses-sdk          android:minSdkVersion="11"          android:targetSdkVersion="19" />        <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">          <activity android:name="MainActivity"                    android:label="@string/app_name">              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>          <service android:name=".MyService" />      </application>  </manifest>  布局文件:main.xml[html] view plaincopy<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"      android:layout_width="match_parent"      android:layout_height="match_parent"      >      <Button android:id 声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部