IntentService----意图服务
意图服务是异步进行的 执行完操作后就会自己消毁(onDestroy方法)
本例为点击按钮下载三张图片相当于连续执行三次意图服务中的onStartcommand方法
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout; public class MainActivity extends Activity { private ImageView img1View,img2View,img3View;
private RelativeLayout mainLayout; private ImgReceiver imgReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mainLayout=(RelativeLayout) findViewById(R.id.mainLayoutId); img1View=(ImageView) findViewById(R.id.img1Id);
img2View=(ImageView) findViewById(R.id.img2Id);
img3View=(ImageView) findViewById(R.id.img3Id); img1View.setTag(Config.URL1);
img2View.setTag(Config.URL2);
img3View.setTag(Config.URL3); imgReceiver=new ImgReceiver();
registerReceiver(imgReceiver,
new IntentFilter(Config.ACTION_DOWNLOAD_COMPLETED)); } public void startDownload(View view){
Intent intent=new Intent(getApplicationContext(),DownloadService.class);
intent.putExtra("path", Config.URL1);
startService(intent); //涓嬭浇绗竴涓浘鐗�
intent.putExtra("path", Config.URL2);
startService(intent); //涓嬭浇绗簩涓浘鐗�
intent.putExtra("path", Config.URL3);
startService(intent); //涓嬭浇绗笁涓浘鐗�
} @Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(imgReceiver); //鍙栨秷娉ㄥ唽骞挎挱鎺ユ敹鍣�
} class ImgReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO 鎺ユ敹鍥剧墖涓嬭浇瀹屾垚鐨勫箍鎾�
String url=intent.getStringExtra(Config.EXTRA_URL);
Bitmap bitmap=intent.getParcelableExtra(Config.EXTRA_BITMAP); //鏍规嵁Url浣滀负鐨勫浘鐗囨帶浠剁殑鏍囩鏌ユ壘鍥剧墖鎺т欢
ImageView imgView=(ImageView) mainLayout.findViewWithTag(url);
imgView.setImageBitmap(bitmap);
}
} }
MainActivity.java
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import android.app.IntentService;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log; /**
* IntentService鏄甫鏈夊瓙绾跨▼鐨勬湇鍔$粍浠讹紝鍏跺唴閮ㄤ娇鐢ㄤ簡鍗曠嚎绋嬫睜妯″紡锛� * 褰撴墍鏈夌殑浠诲姟鎵ц瀹屾垚鍚庯紝浼氳嚜鍔ㄥ叧闂湰鏈嶅姟
* 鍏剁敓鍛藉懆鏈熸柟娉曪細
* onCreate()
* onStartCommand()
* onHandleIntent() 鍦ㄥ瓙绾跨▼涓墽琛岀殑鏂规硶
* onDestroy()
*
* @author apple
*
*/
public class DownloadService extends IntentService {
public DownloadService(){
super(null);//鍙傛暟锛氭槸璁剧疆瀛愮嚎绋嬬殑鍚嶇О
} @Override
public void onCreate() {
super.onCreate();
Log.i("debug", "--onCreate---");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("debug", "--onStartCommand---");
return super.onStartCommand(intent, flags, startId);
} @Override
protected void onHandleIntent(Intent intent) {
// TODO 鍦ㄥ瓙绾跨▼涓墽琛岀殑鏂规硶
Log.i("debug", "--onHandleIntent---");
//鑾峰彇涓嬭浇鍥剧墖鐨勫湴鍧�
String path=intent.getStringExtra("path");
try{
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
InputStream is=conn.getInputStream();
byte[] buffer=new byte[10*1024];//姣忔璇诲彇瀛楄妭鐨勬渶澶ф暟
int len=-1; ByteArrayOutputStream baos=new ByteArrayOutputStream();
if(conn.getResponseCode()==200){
while((len=is.read(buffer))!=-1){
baos.write(buffer, 0, len);
} byte[] bytes=baos.toByteArray();
//灏嗕笅杞藉畬鎴愮殑瀛楄妭鏁扮粍杞垚鍥剧墖瀵硅薄
Bitmap bitmap=BitmapFactory.decodeByteArray(bytes, 0, bytes.length); //灏嗗浘鐗囧璞″彂閫佺粰Activity杩涜鏄剧ず
Intent bitmapIntent=new Intent(Config.ACTION_DOWNLOAD_COMPLETED);
bitmapIntent.putExtra(Config.EXTRA_BITMAP,bitmap);
bitmapIntent.putExtra(Config.EXTRA_URL, path); sendBroadcast(bitmapIntent); Thread.sleep(2000);//浠呮祴璇曟椂浣跨敤
} }catch(Exception e){
e.printStackTrace();
} } @Override
public void onDestroy() {
super.onDestroy();
Log.i("debug", "--onDestroy---");
} }
DownLoadService.java
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/mainLayoutId"> <Button
android:id="@+id/btnId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startDownload"
android:text="开始下载" /> <ImageView
android:id="@+id/img1Id"
android:layout_width="100dp"
android:layout_height="90dp"
android:scaleType="centerCrop"
android:layout_margin="5dp"
android:layout_below="@id/btnId"/> <ImageView
android:id="@+id/img2Id"
android:layout_width="100dp"
android:layout_height="90dp"
android:scaleType="centerCrop"
android:layout_margin="5dp"
android:layout_below="@id/btnId"
android:layout_toRightOf="@id/img1Id"/> <ImageView
android:id="@+id/img3Id"
android:layout_width="100dp"
android:layout_height="90dp"
android:scaleType="centerCrop"
android:layout_margin="5dp"
android:layout_below="@id/img1Id"/> </RelativeLayout>
activity_main.xml
至于服务的类都需要注册 这里就不写了
IntentService----意图服务的更多相关文章
- 服务 IntentService 前台服务 定时后台服务
Activity public class MainActivity extends ListActivity { private int intentNumber = 0; @Ove ...
- IntentService用于服务中开启子线程的自动关闭
package com.pingyijinren.test; import android.app.IntentService; import android.content.Intent; impo ...
- [Android] Service服务详解以及如何使service服务不被杀死
排版上的细节有些不好看,主要是我用的MarkDown编辑器预览和这里的不一样,在那个上面的样式很舒服.这里要改的地方太多就不想改了,将就看吧.下次写的时候注意.还有看到错误给我提啊. 本文链接:htt ...
- Android 服务入门
前言:硬着头皮把数据库SQLite看完了,接下来就是android服务了,因为自己本身就是菜鸟,所以呢,也只是做做笔记,技术上的东西就别指望我了. 1.什么是服务呢?举个例子,百度地图,美团外卖,OF ...
- Android 服务类Service 的详细学习
http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建 目录(?)[+] 什么是服务 服务有 ...
- Android(java)学习笔记266:Android线程形态之 IntentService
1. IntentService原理 IntentService是一种特殊的Service,既然是Service,使用的时候记得在AndroidManifest清单文件中注册. 并且它是一个抽象类,因 ...
- Android 服务类Service 的具体学习
上一篇说到了通知栏Notification,提起通知栏,不得让人想到Service以及BroadcastReceive,作为android的4大组建的2个重要成员,我们没少和它们打交道.它们能够在无形 ...
- Android HandlerThread和IntentService
HandlerThreadHandlerThread继承了Thread,它是一种可以使用Handler的Thread,它实现也很简单,就是在run中通过Looper.prepare()来创建消息队列, ...
- Android四大组件-服务
Android服务 android 的服务有点类似windows的服务,没有界面,在后台长时间运行,如果我们这种需求的话我们就可以使用服务来实现. 服务的典型应用场景: 播放音乐,下载等,如果要在一个 ...
随机推荐
- MySQL ALTER讲解
当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本章教程前让我们先创建一张表,表名为:testalter_tbl. root@host# mysql -u r ...
- ie6 双边距问题
div 设置float和margin margin在float方向上会变成设置值的两倍 解决方法,加上css _display:inline
- mysql Date查询当天、本周,本月,上一个月的数据
出自:http://www.cnblogs.com/benefitworld/p/5832897.html 今天 select * from 表名 where to_days(时间字段名) = t ...
- 关于VS+ImageWatch在线调试问题
1.使用VS肯定离不开在线调试 2.使用Opencv在VS下进行图像处理,那肯定少不了Image Watch 这两个软件在线调试都存在大坑,弄得精疲力尽才找到解决办法!!! 以下问题都可以通过这个设置 ...
- Windows系统日常运维
WINDOWS系统日常运维 http://www.docin.com/p-677263438.html
- 制作keil5的pack
[原创出品§转载请注明出处] 出处:http://www.cnblogs.com/libra13179/p/6273415.html 我在这里就交大家怎样制作自己的Pack,(这里是我制作好的http ...
- 前端-jQuery的ajax方法
https://www.cnblogs.com/majj/p/9134922.html 0.什么是ajax AJAX = 异步的javascript和XML(Asynchronous Javascri ...
- 八月(The Summer is Gone)观后感
第一次看到这部电影时觉得很亲近,黑白画面,国企改革的背景,浓浓的儿时画面感,原谅我只是一个三十不到的人,可能我比较早熟,对八九十年代还有些记忆,更早以前也通过电视.音乐.书籍等了解过一些,而那些听过又 ...
- ansible随记
先来看一下ansible架构图: 一.官网的语法简单介绍 #选择的主机组 - hosts: webservers #这个是变量 vars: http_port: 80 max_cl ...
- How To Pronounce 3-Syllable Phrases
How To Pronounce 3-Syllable Phrases Share Tweet Share Tagged With: 3-Syllable Learn about the stress ...