因为多数启动服务不必同时处理多个请求(在多线程情景下会很危险),所以使用IntentService类实现服务是很好的选择。本经验将通过继承IntentService输出当前时间教大家如何使用IntentService。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.basillee.asus.demo.MainActivity5">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前时间"
android:id="@+id/button_current_time"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

  然后我们在编写一个CurrentTimeService类,继承IntentService

package com.basillee.asus.demo;
import android.app.IntentService;
import android.content.Intent;
import android.text.format.Time;
import android.util.Log;
public class CurrentTimeService extends IntentService {
public CurrentTimeService(){
super("CurrentTimeService");
}
@Override
protected void onHandleIntent(Intent intent) {
Time time=new Time();
time.setToNow();
String currentTime=time.format("%Y-%m-%d %H:%M:%S");
Log.i("CurrentTimeService",currentTime);
}
}

  然后我们在Oncreate方法里面编写如下代码为button增加监听事件

package com.basillee.asus.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity5 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity5);
Button button= (Button) findViewById(R.id.button_current_time);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity5.this, CurrentTimeService.class));
}
});
}
}

  更多细节请看: http://jingyan.baidu.com/season/48891

Android IntentService使用的更多相关文章

  1. Android IntentService完全解析 当Service遇到Handler

    一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...

  2. Android IntentService 与Alarm开启任务关闭任务

    1:MyService public class MyService extends IntentService{ AlarmManager alarmManager = null; PendingI ...

  3. Android IntentService使用介绍以及源码解析

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.IntentService概述及使用举例 IntentService内部实现机制用到了HandlerThread,如果对HandlerThrea ...

  4. Android IntentService的使用和源码分析

    引言 Service服务是Android四大组件之一,在Android中有着举足重轻的作用.Service服务是工作的UI线程中,当你的应用需要下载一个文件或者播放音乐等长期处于后台工作而有没有UI界 ...

  5. Android IntentService

    IntentService简要分析 IntentService 继承自 android.app.Service.内部实现极其简单. 首先在 onCreate()中去开启了一个 HandlerThrea ...

  6. Android IntentService分析

    IntentService其实是一个很通用的知识点,最近看了下阿里巴巴Android开发手册,再次记录下 阿里巴巴Android开发手册 [强制]避免在 BroadcastReceiver#onRec ...

  7. android IntentService和ResultReceiver的异步处理

    IntentService和ResultReceiver的异步处理 1.在下载手机上从网络下载东西的时候会用到AsyncTask来方便处理,这里可以在用IntentService和ResultRece ...

  8. Android IntentService 的使用

    1.service 执行耗时任务的步骤 2.IntentService (1)介绍 (2)使用方法 (3)优点 (4)在AndroidManifest.xml文件中添加service设置 <se ...

  9. Android IntentService全然解析 当Service遇到Handler

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚.在Andro ...

随机推荐

  1. 使用SVN服务器管理源码

    最近在学习使用SVN管理自己的项目文件,正好有好的文章就拿来标记一下,正所谓: 站在巨人的肩膀上   天下知识为我所用 转载两篇关于使用SVN管理源码的文章. 使用SVN进行源码管理(上):http: ...

  2. MySQL 字段类型详解

    一.非数字类型 类型 范围 说明   Char(N) [ binary] N=1~255 个字元 binary :分辨大小写 固定长度 std_name cahr(32) not null VarCh ...

  3. yum cdh4

    cdh4 install for Centos6那个最美的年代,最好的时光,一路梦想,一路流泪,流的不会是懦弱的泪,而是对奋斗的寄于;1,repo配置>>>/etc/yum.repo ...

  4. IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)

    进程:正在进行中的程序被称为进程,负责程序运行的内存分配;每一个进程都有自己独立的虚拟内存空间 线程:线程是进程中一个独立的执行路径(控制单元);一个进程中至少包含一条线程,即主线程 队列 dispa ...

  5. 转载 基于Selenium WebDriver的Web应用自动化测试

    转载原地址:  https://www.ibm.com/developerworks/cn/web/1306_chenlei_webdriver/ 对于 Web 应用,软件测试人员在日常的测试工作中, ...

  6. opencv 通过摄像头捕捉头部

    code: #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\cxcore.h> ...

  7. SQL 索引

    1.http://www.cnblogs.com/AK2012/archive/2013/01/04/2844283.html 2 .聚簇索引和非聚簇索引的区别 3.聚集索引:只能有一个  (相当于字 ...

  8. React中的Statics对象

    statics 对象允许你定义静态的方法,这些静态的方法可以在组件类上调用.例如 var MyComponent = React.createClass({ statics: { customMeth ...

  9. C#-获取datagriview选中行中某一列的值

    获取选中行中某一列的值: int index = dg_Product.CurrentRow.Index; //取得选中行的索引 txt_ProductId.Text = dg_Product.Row ...

  10. Codeforces Round #324 (Div. 2) C. Marina and Vasya 贪心

    C. Marina and Vasya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pr ...