Service的基本组成
Service与Activity的最大区别就是一有界面,一个没有界面。
如果某些程序操作很消耗时间,那么可以将这些程序定义在Service之中,这样就可以完成程序的后台运行,
其实Service就是一个没有界面的Activity,执行跨进程访问也可以使用Service完成。
Service是一个没有UI界面的操作组件,主要功能是为Activity程序提供一些必要的支持,例如:手机
中的Mp3播放软件,当回到桌面的时候这些组件依然在运行,这就属于Service的功能,Service也有
自己的生命周期方法。
Service的生命周期控制方法:
|
方法及常量 |
类型 |
描述 |
|
|
1 |
START_CONTINUATION_MASK |
常量 |
解析执行Service |
|
2 |
START_STICKY |
常量 |
用于显示启动和停止Service |
|
3 |
IBinder onBind(Intent intent) |
普通 |
设置Activity和Service之间的绑定 |
|
4 |
onCreate() |
普通 |
当一个Service创建时调用 |
|
5 |
onStartCommand(Intent intent,int flags,int startId) |
普通 |
启动Service,由startService()方法触发 |
|
6 |
onDestroy() |
普通 |
Service销毁时调用,由stopService()方法触发 |
Service基本组成
1、单击“启动Serivce”按钮得下图:
2、再单击“启动Serivce”按钮得下图:
3、单击“停止Serivce”按钮得下图:
如果不退出Service,按Home键回到桌面,再找到任务管理器,可以发现Service还在运行,
所以说Service是运行在后台的。
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal">
<Button
android:id="@+id/start"
android:layout_margin="30dp"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="启动Service" />
<Button
android:id="@+id/stop"
android:layout_margin="30dp"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="停止Service" />
</LinearLayout>
在MyServiceDemo.java中:
package com.li.service;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyServiceDemo extends Activity {
private Button start ;
private Button stop ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.start = (Button) super.findViewById(R.id.start) ;
this.stop = (Button) super.findViewById(R.id.stop) ;
this.start.setOnClickListener(new StartOnClickListenerImpl()) ;
this.stop.setOnClickListener(new StopOnClickListenerImpl()) ;
}
private class StartOnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MyServiceDemo.this.startService(new Intent(MyServiceDemo.this,MyServiceUtil.class)) ;
}
}
private class StopOnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MyServiceDemo.this.stopService(new Intent(MyServiceDemo.this,MyServiceUtil.class)) ;
}
}
}
在MyServiceUtil.java中:
package com.li.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyServiceUtil extends Service {
@Override
public IBinder onBind(Intent intent) {
return null; // 此处暂时不做任何的处理
}
@Override
public void onCreate() {
System.out.println("*** Service onCreate()") ;
}
@Override
public void onDestroy() {
System.out.println("*** Service onDestroy()") ;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("*** Service onStartCommand") ;
return Service.START_CONTINUATION_MASK; // 继续执行
}
}
修改AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyServiceDemo"
android:label="@string/title_activity_my_service_demo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyServiceUtil" />
</application>
</manifest>
Service的基本组成的更多相关文章
- 通过AngularJS实现前端与后台的数据对接(二)——服务(service,$http)篇
什么是服务? 服务提供了一种能在应用的整个生命周期内保持数据的方法,它能够在控制器之间进行通信,并且能保证数据的一致性. 服务是一个单例对象,在每个应用中只会被实例化一次(被$injector实例化) ...
- Azure Service Fabric 开发环境搭建
微服务体系结构是一种将服务器应用程序构建为一组小型服务的方法,每个服务都按自己的进程运行,并通过 HTTP 和 WebSocket 等协议相互通信.每个微服务都在特定的界定上下文(每服务)中实现特定的 ...
- 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查。。。
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Ser ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- java中Action层、Service层和Dao层的功能区分
Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...
- org.jboss.deployment.DeploymentException: Trying to install an already registered mbean: jboss.jca:service=LocalTxCM,name=egmasDS
17:34:37,235 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-0.0.0.0-8080 17:34:37,281 INFO [ ...
- Android—Service与Activity的交互
service-Android的四大组件之一.人称"后台服务"指其本身的运行并不依赖于用户可视的UI界面 实际开发中我们经常需要service和activity之间可以相互传递数据 ...
- angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)
common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...
- IIS启动失败,启动Windows Process Activation Service时,出现错误13:数据无效 ;HTTP 错误 401.2 - Unauthorized 由于身份验证头无效,您无权查看此页
因为修改过管理员账号的密码后重启服务器导致IIS无法启动,出现已下异常 1.解决:"启动Windows Process Activation Service时,出现错误13:数据无效&quo ...
- 如何利用mono把.net windows service程序迁移到linux上
How to migrate a .NET Windows Service application to Linux using mono? 写在最前:之所以用要把windows程序迁移到Linux上 ...
随机推荐
- 一道来自华为的C机试题目
题目是这样的 求一个字符串中连续字母的个数 比如I have a book. : 1 I have a dog. : 0 I haavee aa dogg : 4 #include <windo ...
- C#/winform 旅游管理信息系统
工具:Visual Studio 2015,sql server2014 1.系统概述 该旅游管理信息系统可以为游客和公司业务管理员提供服务.游客可以对旅游路线,旅游班次,旅游团,保险,导游,交通工具 ...
- Longtail Hedgehog(DP)
Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- To restore the database on a new host-将数据库恢复至一个新的主机上
To restore the database on a new host:1. Ensure that the backups of the target database are accessib ...
- 基于Mesos运行Spark
背景介绍 Spark有多种集群运行模式,例如:Standalone,Yarn,Mesos. 下面就说一下如何在Mesos上运行Spark,这也是官方推荐的一种运行方式. 在运行Sp ...
- sticker.js贴纸效果
http://stickerjs.cmiscm.com/ <div class="sticker gbtags"></div> <!-- 引用Java ...
- xcode中控件共有属性
Text,title 控件上的文字 backgroung 背景图片或颜色 alignment 布局(居中,..) mode显示模式(缩放,拉伸..) drawing 隐藏控件的可见性 alpha 透明 ...
- C 各种数据类型介绍
1.各种数据类型介绍: 基本数据类型最主要的特点是,其值不可以再分解为其它类型.也就是说,基本数据类型是自我说明的. 1.1整型 整形包括短整型.整形和长整形. 1.1.1短整形 short a=1; ...
- C#中List和数组之间的转换
一.List转数组 (从List<string>转到string[]) C# 代码 复制 List<string> listS=new List<string&g ...
- 【noip2012提高组】国王游戏
恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 成一排,国王站在队伍的最前面. ...