如果把Activity比喻为前台程序,那么service可以看做是一个后台程序。Service跟Activity一样也由Intent调用。

在工程里想要添加一个Service,先新建继承Service的类,然后到AndroidManifest.xml -> Application

中的Service标签中添加。

如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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">
<intent-filter>
<action android:name="" />
</intent-filter>
</service>
</application> </manifest>

其中,第一个name是service的位置,包括完整的包名和service名,如果包名就是你定义的程序包名,也就是和gen目录下那个包的名字一样的话,直接".service名"就可以了。第二个name是你调用service时intent.setAction();中的参数,这个可以自己随便定义。

代码如下:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnStartMyService"
android:text="StartMyService">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnStopMyService"
android:text="StopMyService">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnBindMyService"
android:text="BindMyService">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnUnbindMyService"
android:text="UnbindMyService">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnExit"
android:text="退出程序">
</Button>
</LinearLayout>

MyService.java:

package com.example.service;  

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; public class MyService extends Service {
static public String ServiceState="";
@Override
public IBinder onBind(Intent arg0) {
Log.e("Service", "onBind");
ServiceState="onBind";
return null;
}
@Override
public boolean onUnbind(Intent intent){
super.onUnbind(intent);
Log.e("Service", "onUnbind");
ServiceState="onUnbind";
return false; }
@Override
public void onCreate(){
super.onCreate();
Log.e("Service", "onCreate");
ServiceState="onCreate";
}
@Override
public void onDestroy(){
super.onDestroy();
Log.e("Service", "onDestroy");
ServiceState="onDestroy";
}
@Override
public void onStart(Intent intent,int startid){
super.onStart(intent, startid);
Log.e("Service", "onStart");
ServiceState="onStart";
} }

MainActivity.java:

package com.example.service;  

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity {
Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);
btnStartMyService.setOnClickListener(new ClickEvent()); btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);
btnStopMyService.setOnClickListener(new ClickEvent()); btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);
btnBindMyService.setOnClickListener(new ClickEvent()); btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);
btnUnbindMyService.setOnClickListener(new ClickEvent()); btnExit=(Button)this.findViewById(R.id.btnExit);
btnExit.setOnClickListener(new ClickEvent());
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e("Activity","onDestroy");
} private ServiceConnection _connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
class ClickEvent implements View.OnClickListener{ @Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MyService.class);
if(v==btnStartMyService){
MainActivity.this.startService(intent);
}
else if(v==btnStopMyService){
MainActivity.this.stopService(intent);
}
else if(v==btnBindMyService){
MainActivity.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE);
}
else if(v==btnUnbindMyService){
if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑
MainActivity.this.unbindService(_connection);
}
else if(v==btnExit)
{
MainActivity.this.finish();
} } }
}

安卓开发service的更多相关文章

  1. 安卓开发,Service 服务

    Service 服务 是一种应用组件,可长时间后台运行,不提供用户界面.如音乐播放器/下载程序.不能自己运行. 使用Service的方式: (一)startService(): 调用者和服务之间没有联 ...

  2. C#编写WINNT服务,随便解决安卓开发遇到的5037被众多程序无节操占用的问题

    需求分析: 最近重新开始学习安卓开发,好久不用的ADT集成开发环境频繁遇到不能在仿真机和真机上调试的问题,也就是本人另一篇博文描述的ADB(Android Debug Bridge)监控的5037被金 ...

  3. monkeyrunner之安卓开发环境搭建(二)

    在上一篇文章-安卓开发环境搭建中,我们创建并启动了eclipse自带的安卓模拟器,该模拟器不仅启动慢,而且在使用过程中的反应速度也是出奇的差,经常出现卡机现象.为了解决这种现象,因此,我们又寻找到了更 ...

  4. 安卓开发开发规范手册V1.0

    安卓开发开发规范手册V1.0 之前发布过一份Web安全开发规范手册V1.0,看到收藏文章的读者挺多,发现整理这些文档还挺有意义. 最近周末抽了些时间把之前收集关于安卓安全开发的资料也整理了一下,整理出 ...

  5. 安卓开发(3)—1— Activity

    安卓开发(3)-1- Activity 3.1 Activity是什么: 在前面安卓概述中有提到,Activity是Android开发中的四大组件,所有在app里可以看到的东西都是Activity里面 ...

  6. 基于eclipse-java的平台上搭建安卓开发环境

    首先感谢好人的分享!http://www.mamicode.com/info-detail-516839.html 系统:windows 7 若想直接安装eclipse—android的,请启动如下传 ...

  7. 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题

    刚接触安卓开发,试着写个小程序熟悉下,就写了天气预报的小程序,通过httpUrlConnection读流的方式来获取网络公共接口提供的天气XML信息.但在建立http连接时一直报java.io.EOF ...

  8. Android Studio 1.0.1 + Genymotion安卓模拟器打造高效安卓开发环境

    我们开发安卓大多是使用Eclipse和安卓SDK中自带的安卓模拟器.当然,Google早就推出了自己的安卓开发环境——Android studio,在不久前,Google发布了Android Stud ...

  9. 安卓开发第一步:Android Studio安装配置

    虽然本人是JAVA开发工程师平时主要开发Web App,但因为项目需求需要开发对应的移动端.一时又找不到合适的安卓开发人员,兄弟我只好被项目经理"抓来当壮丁了".俗话说好" ...

随机推荐

  1. 硬盘安装RedHat Enterprise Linux 6(转载)

    准备条件: (1)RedHat Enterprise Linux 6安装镜像            下载见文章末尾 (2)grub文件(用于引导安装)                         ...

  2. 关于android屏幕适配

    好吧 我承认被美工虐的够呛,而且美工他么是个男的!一点也不美, 废话不多说 急着赶路, 之前不怎么重视 直到遇见这个美工给我一套1080x1920的 图,没错 就一套 1dp=3px没错的啊 问题是就 ...

  3. XML文档部署到Tomcat服务器上总是加载出错

    config.xnl 起初文档路径是在src/Dao/config.xml 在Dao目录下BaseDao类中,解析config.xml文件路径 path="/Dao/config.xml&q ...

  4. AMBA总线介绍

    The Advanced Microcontroller Bus Architecture (AMBA) specification defines an on- chip communication ...

  5. 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数

    找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...

  6. hdoj 1342 Lotto【dfs】

    Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  7. XML文件操作指南

    一.XML简介 XML的全名是eXtensible Markup Language(可以扩展的标记语言),它的语法类似HTML,都是用标签来描述数据.HTML的标签是固定的,我们只能使用.不能修改: ...

  8. java 实现 DES加密 解密算法

    DES算法的入口参数有三个:Key.Data.Mode.其中Key为8个字节共64位,是DES算法的工作密钥:Data也为8个字节64位,是要被加密或被解密的数据:Mode为DES的工作方式,有两种: ...

  9. 【Android - 框架】之MVP模式的使用

    提起MVP架构模式,大家可能首先想到的是它的"前辈"MVC模式.MVC由Model.View.Controller组成,请求从Controller进入后进行业务判断,然后交给Mod ...

  10. Java Web中资源的访问路径

    在web应用中,以“/”开头的是绝对路径,不以“/”开头的是相对路径.   在服务器端,通常都使用绝对路径.例如web.xml.struts.xml.servlet等的访问路径都是以“/”开始. 服务 ...