意图服务是异步进行的  执行完操作后就会自己消毁(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. ThinkPHP框架学习摘要

    框架在linux与win下区别 1.文件权限设置: 2.大小写不规范: 学习框架的基本思路 : 1.如何收入并配置框架: 2.Controller的命名规范与书写规范: 3.Model的命名规范与书写 ...

  2. Game of War - Fire Age 有何特别之处?

    作者:福克斯007 链接:https://www.zhihu.com/question/21611550/answer/52458767来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...

  3. ffmpeg 播放音频

    播放音频,设置好SDL_AudioSpec播放参数,然后由SDL回调函数进行解码和数据的拷贝,解码播放音频无需设置延迟,因为声卡播放音频是阻塞的 int audio_decode_frame(AVCo ...

  4. AJAX相关总结

    AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 J ...

  5. html 更新

    HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...

  6. 技术思维VS管理思维

    以下为技术思维与管理思维的不同 在日常的工作中,会出现身兼两职 开发和项目经理 的情况,在此就要学会游刃有余的切换角色,方能一人分身二角 角色转换本质上是思维转换.思维决定一个人的行为,项目经理不像项 ...

  7. 阻止form提交数据,通过ajax等上传数据

    btn.click(function (event) { event.preventDefault(); // 组织发送 $.ajax({ ...}) })

  8. C语言键盘按键无阻塞侦测:kbhit()

    http://www.360doc.com/content/12/0414/09/1317564_203474440.shtml kbhit in c kbhit in c: kbhit functi ...

  9. 最近开始研究php的缓存技术,来个系统自带的OPcache

    最近开始研究php的缓存技术,来个系统自带的OPcache php5.5以上版本  系统自带 PHP5.2-5.4 可通过扩展来安装 OPcache是 zend出品  比apc的优势在于  长期更新 ...

  10. Xeon Phi 编程备忘

    ▶ 闲鱼的 Xeon Phi 3120A 配办公室的新 Xeon 服务器,记录一下环境安装过程. ● 原本尝试搭 Ubuntu 服务器,参考[https://software.intel.com/en ...