Android IntentService使用
因为多数启动服务不必同时处理多个请求(在多线程情景下会很危险),所以使用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使用的更多相关文章
- Android IntentService完全解析 当Service遇到Handler
一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能 ...
- Android IntentService 与Alarm开启任务关闭任务
1:MyService public class MyService extends IntentService{ AlarmManager alarmManager = null; PendingI ...
- Android IntentService使用介绍以及源码解析
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.IntentService概述及使用举例 IntentService内部实现机制用到了HandlerThread,如果对HandlerThrea ...
- Android IntentService的使用和源码分析
引言 Service服务是Android四大组件之一,在Android中有着举足重轻的作用.Service服务是工作的UI线程中,当你的应用需要下载一个文件或者播放音乐等长期处于后台工作而有没有UI界 ...
- Android IntentService
IntentService简要分析 IntentService 继承自 android.app.Service.内部实现极其简单. 首先在 onCreate()中去开启了一个 HandlerThrea ...
- Android IntentService分析
IntentService其实是一个很通用的知识点,最近看了下阿里巴巴Android开发手册,再次记录下 阿里巴巴Android开发手册 [强制]避免在 BroadcastReceiver#onRec ...
- android IntentService和ResultReceiver的异步处理
IntentService和ResultReceiver的异步处理 1.在下载手机上从网络下载东西的时候会用到AsyncTask来方便处理,这里可以在用IntentService和ResultRece ...
- Android IntentService 的使用
1.service 执行耗时任务的步骤 2.IntentService (1)介绍 (2)使用方法 (3)优点 (4)在AndroidManifest.xml文件中添加service设置 <se ...
- Android IntentService全然解析 当Service遇到Handler
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚.在Andro ...
随机推荐
- CDH ecosystem components
1,Mahout ASF(Apache Software Foundation)开源项目,提供可扩展的`机器学习`--(ML,Machine Learning多领域交叉学科,涉及概率,统计,逼近,凸分 ...
- MSSQLSERVER数据库- 一条代码搞定单表备份表结构和表数据
在百度上找到的,很实用这个容易操作,不就两张表,我的建议就是备份表带上日期,以便以后要恢复数据的时候,可以快速找到他,这样备份是表结构和数据一起处理.select * into share_20090 ...
- eclipse 护眼色
eclipse windows 窗口背景颜色 保护视力 博客分类: 工具使用 EclipseWindows eclipse 窗口背景颜色 windows 窗口背景颜色
- 推荐eclipse velocity一款插件 --- veloeclipse
vm文件在eclipse展示很丑,关键字没有颜色之差.这里,推荐一款极其好用的velocity插件 -- veloeclipse 在 Eclipse 版本 4.5.0, 离线安装 Veloeclip ...
- iOS开发-控件设置
一.用户界面控件共有三种基本模式:动态.静态(非动态)和被动 动态:点击它们时会发生事情——通常是出发一段自己编写的时间代码. 被动:仅用于存储用户输入的值,以备后续使用.这些控件不会触发任何操作方法 ...
- android studio简易了解第一部分
1.如果还没下载 jdk,先把jdk下载,然后下载android studio 安装 百度 android studio ,百度软件中心可以下载. Android官网可以下载: 网址: htt ...
- Tomcat服务器常用配置和HTTP简介
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- SpringMVC 参考文档
原文地址:http://blog.csdn.net/lufeng20/article/details/7598801
- 安装Intel HAXM为Android 模拟器加速,30秒内启动完成
要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); Android Studio 1.1.0;JDK 1.7.0_75(64位);android-sdk_r24 ...
- python 闭包(closure)
闭包的定义: 闭包就是一个函数,这个函数可以记住封闭作用域里的值,而不管封闭作用域是否还在内存中. 来一个例子: def happy_add(a): print 'id(a): %x' % id(a) ...