安卓开发service
如果把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的更多相关文章
- 安卓开发,Service 服务
Service 服务 是一种应用组件,可长时间后台运行,不提供用户界面.如音乐播放器/下载程序.不能自己运行. 使用Service的方式: (一)startService(): 调用者和服务之间没有联 ...
- C#编写WINNT服务,随便解决安卓开发遇到的5037被众多程序无节操占用的问题
需求分析: 最近重新开始学习安卓开发,好久不用的ADT集成开发环境频繁遇到不能在仿真机和真机上调试的问题,也就是本人另一篇博文描述的ADB(Android Debug Bridge)监控的5037被金 ...
- monkeyrunner之安卓开发环境搭建(二)
在上一篇文章-安卓开发环境搭建中,我们创建并启动了eclipse自带的安卓模拟器,该模拟器不仅启动慢,而且在使用过程中的反应速度也是出奇的差,经常出现卡机现象.为了解决这种现象,因此,我们又寻找到了更 ...
- 安卓开发开发规范手册V1.0
安卓开发开发规范手册V1.0 之前发布过一份Web安全开发规范手册V1.0,看到收藏文章的读者挺多,发现整理这些文档还挺有意义. 最近周末抽了些时间把之前收集关于安卓安全开发的资料也整理了一下,整理出 ...
- 安卓开发(3)—1— Activity
安卓开发(3)-1- Activity 3.1 Activity是什么: 在前面安卓概述中有提到,Activity是Android开发中的四大组件,所有在app里可以看到的东西都是Activity里面 ...
- 基于eclipse-java的平台上搭建安卓开发环境
首先感谢好人的分享!http://www.mamicode.com/info-detail-516839.html 系统:windows 7 若想直接安装eclipse—android的,请启动如下传 ...
- 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题
刚接触安卓开发,试着写个小程序熟悉下,就写了天气预报的小程序,通过httpUrlConnection读流的方式来获取网络公共接口提供的天气XML信息.但在建立http连接时一直报java.io.EOF ...
- Android Studio 1.0.1 + Genymotion安卓模拟器打造高效安卓开发环境
我们开发安卓大多是使用Eclipse和安卓SDK中自带的安卓模拟器.当然,Google早就推出了自己的安卓开发环境——Android studio,在不久前,Google发布了Android Stud ...
- 安卓开发第一步:Android Studio安装配置
虽然本人是JAVA开发工程师平时主要开发Web App,但因为项目需求需要开发对应的移动端.一时又找不到合适的安卓开发人员,兄弟我只好被项目经理"抓来当壮丁了".俗话说好" ...
随机推荐
- poj3294
首先后缀数组预处理然后二分答案len很显然,然后考虑怎么判定我们用左右指针顺着名次扫描一下,初始左右指针为1根据LCP(i,j)=min(height[rank[i]+1]~height[rank[j ...
- 【转】在Ubuntu下编译Android源码并运行Emulator
原文网址:http://www.mcuos.com/thread-4553-1-1.html 建立编译环境 1.在VirtualBox上安装Ubuntu 2.安装JDK $ sudo apt-ge ...
- ExecuteScalar的学习日志
一:今天写关于调用sqlhelper类的时候出现了一个异常,我仔细观察没有错误啊,怎么回事:看图 二:出现错误时id的结果是0,也就是说ExcuteScalar的结果是null,明明数据库里有多行数据 ...
- 10.8 OSInstall破解
解决 无法完成您的购买,不兼容的问题 http://pan.baidu.com/share/link?shareid=2396730945&uk=1764629622
- HDU-4272 LianLianKan
http://acm.hdu.edu.cn/showproblem.php?pid=4272 据说是状态压缩,+dfs什么什么的,可我这样也过了,什么算法都是浮云 ,暴力才是王道.我也归类为状态压缩, ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- [转载]JVM性能调优--JVM参数配置
http://www.cnblogs.com/chen77716/archive/2010/06/26/2130807.html
- 爬虫技术实战 | WooYun知识库
爬虫技术实战 | WooYun知识库 爬虫技术实战 大数据分析与机器学习领域Python兵器谱-大数据邦-微头条(wtoutiao.com) 大数据分析与机器学习领域Python兵器谱
- 用js判断一个复选框是否被选中
<html> <head> <title> 复选框全选.全不选.反选.必选一个 </title> ...
- linux —— shell 编程(文本处理)
导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 基本文本处理 流编辑器sed aw ...