意图服务是异步进行的 执行完操作后就会自己消毁(onDestroy方法)
本例为点击按钮下载三张图片相当于连续执行三次意图服务中的onStartcommand方法
1 import android.app.Activity; 2 import android.content.BroadcastReceiver; 3 import android.content.Context; 4 import android.content.Intent; 5 import android.content.IntentFilter; 6 import android.graphics.Bitmap; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.ImageView; 10 import android.widget.RelativeLayout; 11 12 public class MainActivity extends Activity { 13 14 private ImageView img1View,img2View,img3View; 15 private RelativeLayout mainLayout; 16 17 private ImgReceiver imgReceiver; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 23 mainLayout=(RelativeLayout) findViewById(R.id.mainLayoutId); 24 25 img1View=(ImageView) findViewById(R.id.img1Id); 26 img2View=(ImageView) findViewById(R.id.img2Id); 27 img3View=(ImageView) findViewById(R.id.img3Id); 28 29 img1View.setTag(Config.URL1); 30 img2View.setTag(Config.URL2); 31 img3View.setTag(Config.URL3); 32 33 imgReceiver=new ImgReceiver(); 34 registerReceiver(imgReceiver, 35 new IntentFilter(Config.ACTION_DOWNLOAD_COMPLETED)); 36 37 } 38 39 public void startDownload(View view){ 40 Intent intent=new Intent(getApplicationContext(),DownloadService.class); 41 intent.putExtra("path", Config.URL1); 42 startService(intent); //涓嬭浇绗竴涓浘鐗? 43 intent.putExtra("path", Config.URL2); 44 startService(intent); //涓嬭浇绗簩涓浘鐗? 45 intent.putExtra("path", Config.URL3); 46 startService(intent); //涓嬭浇绗笁涓浘鐗? 47 } 48 49 @Override 50 protected void onDestroy() { 51 super.onDestroy(); 52 unregisterReceiver(imgReceiver); //鍙栨秷娉ㄥ唽骞挎挱鎺ユ敹鍣? 53 } 54 55 class ImgReceiver extends BroadcastReceiver{ 56 @Override 57 public void onReceive(Context context, Intent intent) { 58 // TODO 鎺ユ敹鍥剧墖涓嬭浇瀹屾垚鐨勫箍鎾? 59 String url=intent.getStringExtra(Config.EXTRA_URL); 60 Bitmap bitmap=intent.getParcelableExtra(Config.EXTRA_BITMAP); 61 62 //鏍规嵁Url浣滀负鐨勫浘鐗囨帶浠剁殑鏍囩鏌ユ壘鍥剧墖鎺т欢 63 ImageView imgView=(ImageView) mainLayout.findViewWithTag(url); 64 imgView.setImageBitmap(bitmap); 65 } 66 } 67 68 }
MainActivity.java
1 import java.io.ByteArrayOutputStream; 2 import java.io.InputStream; 3 import java.net.HttpURLConnection; 4 import java.net.URL; 5 6 import android.app.IntentService; 7 import android.content.Intent; 8 import android.graphics.Bitmap; 9 import android.graphics.BitmapFactory; 10 import android.util.Log; 11 12 /** 13 * IntentService鏄甫鏈夊瓙绾跨▼鐨勬湇鍔$粍浠讹紝鍏跺唴閮ㄤ娇鐢ㄤ簡鍗曠嚎绋嬫睜妯″紡锛? * 褰撴墍鏈夌殑浠诲姟鎵ц瀹屾垚鍚庯紝浼氳嚜鍔ㄥ叧闂湰鏈嶅姟 14 * 鍏剁敓鍛藉懆鏈熸柟娉曪細 15 * onCreate() 16 * onStartCommand() 17 * onHandleIntent() 鍦ㄥ瓙绾跨▼涓墽琛岀殑鏂规硶 18 * onDestroy() 19 * 20 * @author apple 21 * 22 */ 23 public class DownloadService extends IntentService { 24 public DownloadService(){ 25 super(null);//鍙傛暟锛氭槸璁剧疆瀛愮嚎绋嬬殑鍚嶇О 26 } 27 28 @Override 29 public void onCreate() { 30 super.onCreate(); 31 Log.i("debug", "--onCreate---"); 32 } 33 @Override 34 public int onStartCommand(Intent intent, int flags, int startId) { 35 Log.i("debug", "--onStartCommand---"); 36 return super.onStartCommand(intent, flags, startId); 37 } 38 39 @Override 40 protected void onHandleIntent(Intent intent) { 41 // TODO 鍦ㄥ瓙绾跨▼涓墽琛岀殑鏂规硶 42 Log.i("debug", "--onHandleIntent---"); 43 //鑾峰彇涓嬭浇鍥剧墖鐨勫湴鍧? 44 String path=intent.getStringExtra("path"); 45 try{ 46 URL url=new URL(path); 47 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 48 InputStream is=conn.getInputStream(); 49 byte[] buffer=new byte[10*1024];//姣忔璇诲彇瀛楄妭鐨勬渶澶ф暟 50 int len=-1; 51 52 ByteArrayOutputStream baos=new ByteArrayOutputStream(); 53 if(conn.getResponseCode()==200){ 54 while((len=is.read(buffer))!=-1){ 55 baos.write(buffer, 0, len); 56 } 57 58 byte[] bytes=baos.toByteArray(); 59 //灏嗕笅杞藉畬鎴愮殑瀛楄妭鏁扮粍杞垚鍥剧墖瀵硅薄 60 Bitmap bitmap=BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 61 62 //灏嗗浘鐗囧璞″彂閫佺粰Activity杩涜鏄剧ず 63 Intent bitmapIntent=new Intent(Config.ACTION_DOWNLOAD_COMPLETED); 64 bitmapIntent.putExtra(Config.EXTRA_BITMAP,bitmap); 65 bitmapIntent.putExtra(Config.EXTRA_URL, path); 66 67 sendBroadcast(bitmapIntent); 68 69 Thread.sleep(2000);//浠呮祴璇曟椂浣跨敤 70 } 71 72 }catch(Exception e){ 73 e.printStackTrace(); 74 } 75 76 } 77 78 @Override 79 public void onDestroy() { 80 super.onDestroy(); 81 Log.i("debug", "--onDestroy---"); 82 } 83 84 }
DownLoadService.java
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" 10 android:id="@+id/mainLayoutId"> 11 12 <Button 13 android:id="@+id/btnId" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:onClick="startDownload" 17 android:text="开始下载" /> 18 19 <ImageView 20 android:id="@+id/img1Id" 21 android:layout_width="100dp" 22 android:layout_height="90dp" 23 android:scaleType="centerCrop" 24 android:layout_margin="5dp" 25 android:layout_below="@id/btnId"/> 26 27 <ImageView 28 android:id="@+id/img2Id" 29 android:layout_width="100dp" 30 android:layout_height="90dp" 31 android:scaleType="centerCrop" 32 android:layout_margin="5dp" 33 android:layout_below="@id/btnId" 34 android:layout_toRightOf="@id/img1Id"/> 35 36 <ImageView 37 android:id="@+id/img3Id" 38 android:layout_width="100dp" 39 android:layout_height="90dp" 40 android:scaleType="centerCrop" 41 android:layout_margin="5dp" 42 android:layout_below="@id/img1Id"/> 43 44 </RelativeLayout>
activity_main.xml
至于服务的类都需要注册 这里就不写了
时间: 2024-12-21 14:04:41