意图服务是异步进行的  执行完操作后就会自己消毁(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. bash下. : () {} [] [[]] (())的解释

    bash下有很多像{}.[]等一些符号命令,下面是我对一些常用的符号命令的学习笔记,若有错误或纰漏望各位兄弟指正. 一..(source).(点)与source命令一样,从文件中读取并执行命令,无论该 ...

  2. solr 7+tomcat 8 + mysql实现solr 7基本使用(安装、集成中文分词器、定时同步数据库数据以及项目集成)

    基本说明 Solr是一个开源项目,基于Lucene的搜索服务器,一般用于高级的搜索功能: solr还支持各种插件(如中文分词器等),便于做多样化功能的集成: 提供页面操作,查看日志和配置信息,功能全面 ...

  3. noip 2011 选择客栈

    题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...

  4. java基础:关于java流与文件操作

    1.描述:流是字节数据或字符数据序列.Java采用输入流对象和输出流对象来支持程序对数据的输入和输出.输入流对象提供了数据从源点流向程序的管道,程序可以从输入流对象读取数据:输出流对象提供了数据从程序 ...

  5. ubuntu16.04安装python3,numpy,pandas等量化计算库

    ubunt安装python3 sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install ...

  6. etcd查看key-value

    get/set key-value etcdctl get/set /key-path etcdctl watch --recursive /test/sm/default/apps 查看所有key- ...

  7. (15/24) 为webpack增加babel支持

    Babel是什么? Babel是一个编译JavaScript的平台,它的强大之处表现在可以通过编译达到以下目的: 使用下一代的javaScript代码(ES6,ES7-.),即使这些标准目前并未被当前 ...

  8. Spring MVC 自定义视图

    实现View import org.springframework.stereotype.Component; import org.springframework.web.servlet.View; ...

  9. css 学习网址

    http://www.divcss5.com/ http://www.divcss5.com/css3/  css3手册 http://www.divcss5.com/shouce/ css手册 ht ...

  10. eclipse 断点找到同名的其它类

    转载自Eclipse断点进入另一个项目的同名Java文件中(http://tunps.com/p/11789.html) eclipse 断点找到同名的其它类 A和B是两个相同的项目,A一直本地,B是 ...