意图服务是异步进行的  执行完操作后就会自己消毁(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----意图服务的更多相关文章

  1. 服务 IntentService 前台服务 定时后台服务

    Activity public class MainActivity extends ListActivity {     private int intentNumber = 0;     @Ove ...

  2. IntentService用于服务中开启子线程的自动关闭

    package com.pingyijinren.test; import android.app.IntentService; import android.content.Intent; impo ...

  3. [Android] Service服务详解以及如何使service服务不被杀死

    排版上的细节有些不好看,主要是我用的MarkDown编辑器预览和这里的不一样,在那个上面的样式很舒服.这里要改的地方太多就不想改了,将就看吧.下次写的时候注意.还有看到错误给我提啊. 本文链接:htt ...

  4. Android 服务入门

    前言:硬着头皮把数据库SQLite看完了,接下来就是android服务了,因为自己本身就是菜鸟,所以呢,也只是做做笔记,技术上的东西就别指望我了. 1.什么是服务呢?举个例子,百度地图,美团外卖,OF ...

  5. Android 服务类Service 的详细学习

    http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建   目录(?)[+] 什么是服务 服务有 ...

  6. Android(java)学习笔记266:Android线程形态之 IntentService

    1. IntentService原理 IntentService是一种特殊的Service,既然是Service,使用的时候记得在AndroidManifest清单文件中注册. 并且它是一个抽象类,因 ...

  7. Android 服务类Service 的具体学习

    上一篇说到了通知栏Notification,提起通知栏,不得让人想到Service以及BroadcastReceive,作为android的4大组建的2个重要成员,我们没少和它们打交道.它们能够在无形 ...

  8. Android HandlerThread和IntentService

    HandlerThreadHandlerThread继承了Thread,它是一种可以使用Handler的Thread,它实现也很简单,就是在run中通过Looper.prepare()来创建消息队列, ...

  9. Android四大组件-服务

    Android服务 android 的服务有点类似windows的服务,没有界面,在后台长时间运行,如果我们这种需求的话我们就可以使用服务来实现. 服务的典型应用场景: 播放音乐,下载等,如果要在一个 ...

随机推荐

  1. 深入理解yield(二):yield与协程

    转自:http://blog.beginman.cn/blog/133/ 协程概念 1.并发编程的种类:多进程,多线程,异步,协程 2.进程,线程,协程的概念区别: 进程.线程和协程的理解 进程:拥有 ...

  2. asp.net webapi 自托管插件式服务(转)

    asp.net webapi 自托管插件式服务   webapi问世已久,稀里糊涂的人哪它都当mvc来使,毕竟已mvc使用级别的经验就可以应对webapi. webapi和mvc在asp.net5时代 ...

  3. storm的定时任务

    应用场景: 第一种方法 参考代码StormTopologyTimer1.java package yehua.storm; import java.util.Map; import org.apach ...

  4. 库、教程、论文实现,这是一份超全的PyTorch资源列表(Github 2.2K星)

    项目地址:https://github.com/bharathgs/Awesome-pytorch-list 列表结构: NLP 与语音处理 计算机视觉 概率/生成库 其他库 教程与示例 论文实现 P ...

  5. ECCV 2018 | UBC&腾讯AI Lab提出首个模块化GAN架构,搞定任意图像PS组合

    通常的图像转换模型(如 StarGAN.CycleGAN.IcGAN)无法实现同时训练,不同的转换配对也不能组合.在本文中,英属哥伦比亚大学(UBC)与腾讯 AI Lab 共同提出了一种新型的模块化多 ...

  6. js常见兼容

    滚动条的兼容写法(谷歌chrome) document.documentElement.scrollTop || document.body.scrollTop   阻止浏览器默认行为的兼容写法  e ...

  7. python+selenium+requests爬取我的博客粉丝的名称

    爬取目标 1.本次代码是在python2上运行通过的,python3的最需改2行代码,用到其它python模块 selenium 2.53.6 +firefox 44 BeautifulSoup re ...

  8. 配置WDS支持使用UEFI模式启动

    使用WDS通过Legacy+MBR方式部署操作系统不难,网上文章也有很多,本文就不赘述了,主要记录一下通过UEFI+GPT方式部署. 网上文章虽然也有介绍通过UEFI+GPT方式部署,但大多数说的比较 ...

  9. HTML5 Canvas ( 贝塞尔曲线, 一片星空加绿地 ) quadraticCurveTo, bezierCurveTo

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. apache配置修改

    1.如何设置请求等待时间   在httpd.conf里面设置:   TimeOut n   其中n为整数,单位是秒.     2. 3.如何使得apache监听在特定的端口   修改httpd.con ...