- 浏览: 1058419 次
- 性别:
- 来自: 南昌
文章分类
- 全部博客 (276)
- 生活 (1)
- 代码之美 (22)
- Media (7)
- Android Widget (3)
- Android Intent (1)
- Android Activity (4)
- UI event handle--UI事件处理机制 (2)
- Java基础知识 (12)
- android Databases (5)
- Android 系统知识 (70)
- 平常遇到的问题与解决方法 (38)
- Android TextView/EditView (2)
- Thinking Java (1)
- android webkit (6)
- JSON (1)
- XML (4)
- HTTP (1)
- Google Weather API (1)
- android 2.3 NFC (10)
- android app (20)
- android framework (7)
- C++ (2)
- android System (5)
- Pthread (1)
- Wifi (8)
- Unix/Linux C (8)
- Android 4.0 (1)
- Mail (1)
- Smack 源码学习 (4)
- iOS (4)
- Android (1)
- git (1)
- Gallery3d (2)
- React-Natice (1)
最新评论
-
dd18349182956:
你是用的smack哪个版本?我用的smack4.1.3和sma ...
关于socket长连接的心跳包 -
xukaiyin:
全英文
getApplicationContext()与this,getBaseContext() -
裂风矢:
...
<category android:name="android.intent.category.DEFAULT" /> 惹的祸 -
xanthodont:
mark一下
XMPP——Smack -
Evilover3:
mark一下,学习了
XMPP——Smack
看android的源码可以发现很多很多有趣有用的代码,今天在看关于NFC 的Tag源码的时候,发现了IntentService.做应用程序真的可以很快就完成,因为已经有了很多很多现成的API用,帮我们做了很多的事情。就拿IntentService来说:
Class Overview
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
IntentService是Service的子类,可以通过处理异步的请求(用Intent的方式)。Service可以在需要的时候启动,并且用单独的thread处理每个Intent,但处理完之后就会取消自己。
这种“工作队列的处理器”方式是普遍用于从应用程序的主线程分出来的任务。
所有的请求都会在一个单独的线程处理,但不会阻塞应用程序的主线程。并且一次只处理一个请求。
我们看Tag应用程序里面用IntentService的例子:
如果我们不用IntentService,我们就自己得用AsyncTask,Looper, Handler来处理。其实若去看IntentService的源码就知道,IntentSerice已经帮我们做了这些事情。所以我们用起来就方便快捷了。^_^
Class Overview
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
IntentService是Service的子类,可以通过处理异步的请求(用Intent的方式)。Service可以在需要的时候启动,并且用单独的thread处理每个Intent,但处理完之后就会取消自己。
这种“工作队列的处理器”方式是普遍用于从应用程序的主线程分出来的任务。
所有的请求都会在一个单独的线程处理,但不会阻塞应用程序的主线程。并且一次只处理一个请求。
我们看Tag应用程序里面用IntentService的例子:
/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.android.apps.tag; import com.android.apps.tag.provider.TagContract.NdefMessages; import android.app.IntentService; import android.app.PendingIntent; import android.app.PendingIntent.CanceledException; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.nfc.NdefMessage; import android.os.Parcelable; import android.util.Log; public class TagService extends IntentService { private static final String TAG = "TagService"; private static final String EXTRA_SAVE_MSGS = "msgs"; private static final String EXTRA_DELETE_URI = "delete"; private static final String EXTRA_STAR_URI = "set_star"; private static final String EXTRA_UNSTAR_URI = "remove_star"; private static final String EXTRA_STARRED = "starred"; private static final String EXTRA_PENDING_INTENT = "pending"; private static final boolean DEBUG = true; public TagService() { super("SaveTagService"); } @Override public void onHandleIntent(Intent intent) { if (intent.hasExtra(EXTRA_SAVE_MSGS)) { Parcelable[] msgs = intent.getParcelableArrayExtra(EXTRA_SAVE_MSGS); NdefMessage msg = (NdefMessage) msgs[0]; ContentValues values = NdefMessages.toValues(this, msg, false, System.currentTimeMillis()); Uri uri = getContentResolver().insert(NdefMessages.CONTENT_URI, values); if (intent.hasExtra(EXTRA_PENDING_INTENT)) { Intent result = new Intent(); result.setData(uri); PendingIntent pending = (PendingIntent) intent.getParcelableExtra(EXTRA_PENDING_INTENT); try { pending.send(this, 0, result); } catch (CanceledException e) { if (DEBUG) Log.d(TAG, "Pending intent was canceled."); } } return; } if (intent.hasExtra(EXTRA_DELETE_URI)) { Uri uri = (Uri) intent.getParcelableExtra(EXTRA_DELETE_URI); getContentResolver().delete(uri, null, null); return; } if (intent.hasExtra(EXTRA_STAR_URI)) { Uri uri = (Uri) intent.getParcelableExtra(EXTRA_STAR_URI); ContentValues values = new ContentValues(); values.put(NdefMessages.STARRED, 1); getContentResolver().update(uri, values, null, null); } if (intent.hasExtra(EXTRA_UNSTAR_URI)) { Uri uri = (Uri) intent.getParcelableExtra(EXTRA_UNSTAR_URI); ContentValues values = new ContentValues(); values.put(NdefMessages.STARRED, 0); getContentResolver().update(uri, values, null, null); } } public static void saveMessages(Context context, NdefMessage[] msgs, boolean starred, PendingIntent pending) { Intent intent = new Intent(context, TagService.class); intent.putExtra(TagService.EXTRA_SAVE_MSGS, msgs); intent.putExtra(TagService.EXTRA_STARRED, starred); intent.putExtra(TagService.EXTRA_PENDING_INTENT, pending); context.startService(intent); } public static void delete(Context context, Uri uri) { Intent intent = new Intent(context, TagService.class); intent.putExtra(TagService.EXTRA_DELETE_URI, uri); context.startService(intent); } public static void setStar(Context context, Uri message, boolean star) { Intent intent = new Intent(context, TagService.class); if (star) { intent.putExtra(EXTRA_STAR_URI, message); } else { intent.putExtra(EXTRA_UNSTAR_URI, message); } context.startService(intent); } }
如果我们不用IntentService,我们就自己得用AsyncTask,Looper, Handler来处理。其实若去看IntentService的源码就知道,IntentSerice已经帮我们做了这些事情。所以我们用起来就方便快捷了。^_^
发表评论
-
ACTIVITY的LAUNCH MODE详解 SINGLETASK正解
2012-05-30 08:58 1084转自:http://www.cnblogs.com/xiaoQ ... -
android的一些开源项目
2011-12-06 14:08 1164转自:http://www.iteye.com/problem ... -
修改StatusBar
2011-10-10 10:27 1237转自:http://iserveandroid.blogspo ... -
单独一个应用程序换语言
2011-09-29 15:16 1423转自http://blog.csdn.net/sodino/a ... -
通过代码设置live wall paper
2011-09-02 09:17 24921. The code is: private Wall ... -
Android camera 默认显示黑白的问题
2011-06-08 16:18 2490转自:http://tassardge.blog. ... -
关于Activity切换动画——overridePendingTransition
2011-05-25 14:33 5744Activity的切换动画指的是从一个activity跳转到另 ... -
sendBroadcast和sendStickyBroadcast的区别
2011-04-28 13:39 9674我们平时最经常使用的是sendBroadcast,就是把一个I ... -
Settings.System.getInt获取Setting里面的一些设置
2011-04-28 10:29 7004好久没有更新博客了,其实这期间我都在研究android如何换皮 ... -
Service 启动Activity
2011-03-11 11:06 25139我想我们一般在Service里想启动Activity一定会这样 ... -
Android Windows
2011-03-11 09:45 2027转自:http://elsila.blog.163.com/b ... -
AsyncQueryHandler
2011-03-02 15:10 6771在QuickContactBadge里面我 ... -
QuickContactBadge如何实现
2011-03-02 11:52 4892从前一篇,我们知道了如何使用了QuikcContactBadg ... -
如何使用QuickContactBadge
2011-03-02 09:34 4022参考文章:http://mobile.tutsplus.com ... -
QuickSearcheBox---SearchManager获取search列表
2011-02-25 14:05 1845在android 2.2之后加入了SearchManager, ... -
QuickSearcheBox---SearchWidgetConfigActivity
2011-02-24 10:48 1611再把QuickSearchBox放到桌面前,会先触发它的Con ... -
RemoteView总结
2011-02-23 11:15 2304我最初认识RemoteView是在AppWidget里面的,但 ... -
Google Search Widget, Google Search ap, Globe Search ap
2011-02-23 10:39 1261http://blog.csdn.net/Judy889/ar ... -
调用android system Search UI须注意的问题
2011-02-23 10:38 1521http://blog.csdn.net/Judy889/ar ...
相关推荐
service中1需要手动开启子线程2服务开启之后会一直运行,需要手动调用stopService();或者stopSelf(); intentService是一种异步(子线程)、自动停止的服务,这个例子测试IntentService的特征
一个IntentService的简单使用
介绍service和intentservice的使用和其中应该注意的部分
对于有写工作,我们需要在后台进行,这里主要介绍了使用IntentService轻松实现模拟上传图片的例子。对于IntentServixce和Service扩展类的使用区别,大家可以自行去了解研究一下。
Android线程,线程池,AsyncTask,HandlerThread和IntentService的用法demo
IntentService简单demo,对应博文 http://blog.csdn.net/tianmi1988/article/details/50520339
实现连接IntentService,打印输出日志
Android 中IntentService的简单应用
NULL 博文链接:https://zhanglimijavaeye.iteye.com/blog/1881599
再开始之前我们还是先介绍下service吧:此处用的是IntentService,至于和常规的service有什么区别呢? 有了Service为什么还要有个IntentService呢? 原因如下: 1)Service默认运行在主线程中,IntentService运行在一个...
自己写的一个android IntentService 的应用的例子,希望对大家有帮助。
Android IntentService详解 一、IntentService简介 IntentService是Service的子类,比普通的Service增加了额外的功能。先看Service本身存在两个问题: Service不会专门启动一条单独的进程,Service与它所在...
demo对应博客地址:http://blog.csdn.net/u013100574/article/details/53129554
博客地址:...IntentService是继承于Service并处理异步请求的一个类,在 IntentService 内有一个工作线程来处理耗时操作,启动 IntentService 的方式和启动传统 Service 一样
为什么我们需要IntentService ? Android中的IntentService是继承自Service类的,在我们讨论IntentService之前,我们先想一下Service的特点: Service的回调方法(onCreate、onStartCommand、onBind、onDestroy)都是...
Android中的IntentService简介.pdf 学习资料 复习资料 教学资源
android IntentService实现原理及内部代码分享,需要的朋友可以参考一下