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 ...
随机推荐
- DTD - Attributes
In a DTD, attributes are declared with an ATTLIST declaration. Declaring Attributes An attribute dec ...
- prestashop二次开发 笔记(支付插件)
//主函数 public function __construct() { $this->name = 'CilPay'; //模块名称 $this->display ...
- function和感叹号,运算符号的转化
1.下面的程序经过运算之后,a为true,这个很好理解,但是函数怎么会运行呢? var a = !function(){ alert('message'); }(); console.log(a); ...
- 对<< ubuntu 12.04编译安装linux-3.6.10内核笔记>>的修正
前题: 在前几个月的时候,写了一篇笔记,说的是kernel compile的事情,当时经验不足,虽说编译过了,但有些地方写的有错误--因为当时的理解是有错误的.今天一一更正,记录如下: 前文笔记链接: ...
- iOS- iPad UIPopoverController
在IPAD开发中,有一个很有趣的视图控制器,UIPopoverControllr,它的初始化必须要设置一个"内容视图",相当于它本身只是作为一个“容器”,而显示的内容还需要另外一个 ...
- yarn 集群部署,遇到的问题小结
版本号信息: hadoop 2.3.0 hive 0.11.0 1. Application Master 无法訪问 点击application mater 链接,出现 http 500 错 ...
- Android 屏幕画笔实现
Tuya.rar
- [Server Running] [Node.js, PM2] Using PM2 To Keep Your Node Apps Alive
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allow ...
- android学习日记13--数据存储之File存储
4.文件存储File File即传统的I/O 流存储文件,Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过程与在J2SE环境中保存数据到文件中是一样的 ...
- Linux PAM&&PAM后门
Linux PAM&&PAM后门 我是壮丁 · 2014/03/24 11:08 0x00 PAM简介 PAM (Pluggable Authentication Modules )是 ...