淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(四)
DEMO1:在Activity里声明一个回调方法,当service完成任务后,调用这个回调方法。
首先,我们先继承service,来创建服务,代码如下:
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Button;
public class MyService extends Service {
public static final String TAG = "MYSERVICE";
@Override
public void onCreate() {
Log.i(TAG, "MyService-->onCreate");
super.onCreate();
}
@Override
public void onDestroy() {
Log.i(TAG, "MyService-->onDestroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "MyService-->onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder implements ICalculator
{
public MyService getService()
{
return MyService.this;
}
@Override
public int add(int x, int y) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return x + y;
}
}
@Override
public IBinder onBind(Intent arg0) {
return binder;
}
}
其中,我定义了一个公共的接口
package com.example.service;
public interface ICalculator {
int add(int x,int y);
}
主页面:MainActivity.java
package com.example.servicedemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.example.service.ICalculator;
import com.example.service.MyService;
/*
* bindService(intent,conn,flag)
* Service:onCreate()
* Service:onBind()
* Activity:onServiceConnected()
*/
public class MainActivity extends Activity implements OnClickListener {
private Button btnStartSrv,btnStopSrv,btnBindSrv;
private ICalculator ical;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartSrv = (Button)findViewById(R.id.btnStartSrv);
btnStopSrv = (Button)findViewById(R.id.btnStopSrv);
btnBindSrv = (Button)findViewById(R.id.btnBindSrv);
btnStartSrv.setOnClickListener(this);
btnStopSrv.setOnClickListener(this);
btnBindSrv.setOnClickListener(this);
Intent intent=new Intent(MainActivity.this,MyService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
unbindService(conn);
super.onDestroy();
}
@Override
public void onClick(View view) {
Intent service = new Intent(this, MyService.class);
switch(view.getId())
{
case R.id.btnStartSrv:
{
startService(service);
break;
}
case R.id.btnStopSrv:
{
stopService(service);
break;
}
case R.id.btnBindSrv:
{
testSrv();
Toast.makeText(this, "服务调用完了", 1).show();
break;
}
default:
break;
}
}
ServiceConnection conn=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
ical = (ICalculator) service;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
public void testSrv()
{
if(ical != null)
{
int x = ical.add(3, 5);
Toast.makeText(this, String.valueOf(x), 1).show();
}
}
}
我故意在service里的方法,做了休眠10秒钟,当我们点击的BindSrv按钮时,过了10秒钟才弹出对话框,得到服务的运行结果。

所以,如果我在service中处理相对耗时的,就得在服务中另开一个线程。
转载请注明http://www.cnblogs.com/yushengbo,否则将追究版权责任!
淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(四)的更多相关文章
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(六)
Service和Thread的关系 不少初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread? 答案是Service和T ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(五)
我们现在对上一节中的DEMO进行改进,在服务中开启线程来执行. package com.example.service; import android.app.Service; import andr ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(三)
主题:Service与Activity交互通信 问题的引出:现在有个需求,如果我们有一个下载任务,下载时间耗时比较长,并且当下载完毕后,需要更新UI的内容,这时,service中的bindServic ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(二)
DEMO1:当我们点击启动服务时和点击停止服务的时候,观察服务的运行状态,布局由于简单,只是两个普通的Button按钮,在此我只上截图. java代码部分 第一步:我们需要实现一个服务类,继承自ser ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(一)
public abstract class Service; [API文档关于Service类的介绍] A Service is an application component representi ...
- 淘宝(阿里百川)手机客户端开发日记第七篇 Service,Handler和Thread
现在我们已经已经知道android有Service,Handler和Thread这些内容了,但是我想应该还有很多人对此并不是很清楚他们之间的区别! (1)Service 是运行在后端的程序,不与UI直 ...
- 淘宝(阿里百川)手机客户端开发日记第十一篇 JSP+Servlet
由于本人从事.net开发已有多年经验,今天由于工作需要,我只能学习下JSP+Servlet,至于java web提供了更好的开发框架MVC,现在由于时间关系,我只好用JSP+Servlet来搭建服务器 ...
- 淘宝(阿里百川)手机客户端开发日记第五篇 SharedPreferences使用详解
我们知道,Android中数据存储技术由于如下几种 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用ContentProvider存储数据 ...
- 淘宝(阿里百川)手机客户端开发日记第十篇 阿里百川服务器环境介绍之API文档的快速链接(四)
个人感觉比较重要的快速链接: http://open.taobao.com/doc/detail.htm?id=102513 http://open.taobao.com/doc/detail.htm ...
随机推荐
- Android--自动搜索提示
一. 效果图 在Google或者百度搜索的时候,在输入关键词都会出现自动搜索的提示内容,类似如下的效果,输入b 则出现包含b的相关词条 二. 布局代码 <?xml version="1 ...
- Sql Server 附加没有日志文件的数据库(.mdf)文件方法
附加数据库,附加的时候会提醒找不到log文件 针对以上现象有两个写法的语句能解决: 写法一: USE MASTER; EXEC sp_detach_db @dbname = 'TestDB'; EXE ...
- [codevs 1503]愚蠢的宠物(特殊的LCA)
题目:http://codevs.cn/problem/1503/ 分析:一道裸的LCA,但是由于询问只有一次,所以可以简单打打……可以从a,b向父节点推直到1节点,然后比较两个序列,后面一段肯定相同 ...
- 大型网站系统架构实践(五)深入探讨web应用高可用方案
从上篇文章到这篇文章,中间用了一段时间准备,主要是想把东西讲透,同时希望大家给与一些批评和建议,这样我才能有所进步,也希望喜欢我文章的朋友,给个赞,这样我才能更有激情,呵呵. 由于本篇要写的内容有点多 ...
- 《TCP/IP详解卷1:协议》第5章 RARP:逆地址解析协议-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- [AaronYang]C#人爱学不学[5]
这世上有三样东西是别人抢不走的:一是吃进胃里的食物,二是藏在心中的梦想,三是读进大脑的书 --Aaronyang的博客(www.ayjs.net) 1. 数组-的疑惑? 1.1 多维数组 ...
- Apache CXFjar包目录(转)
文件目录结构及相关文件的详细说明:|-bin|-docs|-etc|-lib|-licenses|-modules|-samples bin(目录) bin 目录中是 CXF 框架中所提供的代码生成. ...
- beta发布排序结果
排序结果 序号 组名 组长 项目简称 1 飞天小女警 沈柏杉 选礼物 1 奋斗吧兄弟 黄兴 食物链 3 天天向上 王森 连连看 4 金洲勇士 尹良亮 考试 5 新蜂 武志远 俄罗斯 6 nice! 李 ...
- .net架构设计读书笔记--第三章 第10节 命令职责分离(CQRS)简介(Introducing CQRS)
一.分离查询命令 Separating commands from queries 早期的面向DDD设计方法的难点是如何设计一个类,这个类要包含域的方方面面.通常来说,任务软件系统方法调用可以 ...
- Spring 常用注解
用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册 <context:component-scan base-package=”pagkage1[, ...