Android 利用Service实现下载网络图片至sdk卡
package com.example.myapp5; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Android 利用service实现下载图片功能
* @author shaobn
* @date 2015-9-14
* @packege com.example.myapp5.MyApp5
*/
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
});
}
}
package com.example.myapp5; import java.io.File;
import java.io.FileOutputStream; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast; public class MyService extends Service {
private final static String IMAGEPAH = "https://www.baidu.com/img/bd_logo1.png";
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
final Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) {
if(msg.what==1){
Toast.makeText(getApplicationContext(), "关闭了", 1).show();
stopSelf();
}
}; };
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(IMAGEPAH);
HttpResponse httpResponse = null;
File file = Environment.getExternalStorageDirectory();
File file2 = null;
FileOutputStream fos = null;
try {
httpResponse =httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode()==200) {
byte[] num = EntityUtils.toByteArray(httpResponse.getEntity());
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
file2 = new File(file, "/Movies/a.jpg");
file2.createNewFile();
fos = new FileOutputStream(file2);
fos.write(num);
Message message = Message.obtain();
message.what = 1;
handler.sendMessage(message);
}
} } catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}finally{
if(fos!=null){
try {
fos.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
} } }
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
Android 利用Service实现下载网络图片至sdk卡的更多相关文章
- Android 利用Service BroadcastReceiver实现小例子
Activity: package com.example.test; import android.app.Activity; import android.content.Context; imp ...
- Android 利用 AsyncTask 异步读取网络图片
1.新建Android工程AsyncLoadPicture 新建布局文件activity_main.xml主界面为一个GridView,还有其子项布局文件gridview_item.xml 2.功能主 ...
- Android开发利用Volley框架下载和缓存网络图片
2013年Google I/O大会上推出了一种新的网络通信框架——Volley,Volley可是说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可 ...
- Android利用Http下载文件
Android利用Http下载文件 一.场景 下载存文本文件和下载如mp3等大容量的文件 界面 二.代码编写 1.AndroidMainfest.xml中配置 主要是解决网络权限和写SDCard的权限 ...
- android下载网络图片并缓存
异步下载网络图片,并提供是否缓存至内存或外部文件的功能 异步加载类AsyncImageLoader public void downloadImage(final String url, final ...
- Android开发-下载网络图片并显示到本地
Android下载网络图片的流程是: 发送网络请求->将图片以流的形式下载下来->将流转换为Bitmap并赋给ImageView控件. 注意点 最新的Android系统不可以在主线程上请求 ...
- android 下载网络图片并缓存
异步下载网络图片,并提供是否缓存至内存或外部文件的功能 异步加载类AsyncImageLoader public void downloadImage(final String url, final ...
- Android多媒体学习六:利用Service实现背景音乐的播放
Android同意我们使用Service组件来完毕后台任务.这些任务的同意不会影响到用户其它的交互. 1.Activity类 [java] view plaincopy package demo.ca ...
- Android(java)学习笔记181:利用Service在后台播放背景音乐
1.在android应用程序里,有一种没有UI的类(android.app.Service)——Service.简单来说,Service是一个 background process(背景程序),通过背 ...
随机推荐
- LinQ总结
不管是在Mvc还是在别的架构中的项目LinQ和Lambda总是经常会遇到的. 而有些LinQ的语法并不是很长用(我大部分用的是Lambda),所以有必要记录一下万一用到的时候我能很方便的找到我想找到的 ...
- C++字符数字的编码(Encode)与解码(Decode)
在日常应用中,我们常用结构体或者类来存储一条信息,这种方式很方便,但是不利于数据的传输.例如在网络编程中,我们需要将结构中的数据转化为字节流才能进行传输,我们可以利用memcpy强行将结构化的数据转化 ...
- HD 1003 Max Sum 的递归解法
#include <STDIO.H> typedef struct SU_tag{ SU_tag(){} SU_tag(int a,int b,int c):max_sum(a),left ...
- Spark Programming--Actions
first def first(): T first返回RDD中的第一个元素,不排序. 例子: count def count(): Long count返回RDD中的元素数量 例子: reduce ...
- 关于lnmp下搭thinkPHP无法找到指定静态页面
我在lnmp 下架了一个thinkPHP框架,非常奇怪,在环境都配置好后,我在url里输入localhost:10007/index.php/member/login,正常来说应该显示login.ht ...
- oracle视图V$BH && X$BH的使用列子
1创建一个测试表,test,并且插入10000行数据: SQL> create table test (id int); SQL> begin 2 for i in 1 ...
- iOS:crash崩溃日志分析
一.前言: 作为一个合格的iOS开发者,除了具有规范强悍的编码能力外,还应该具有过硬的查错纠错能力.在项目运行时,程序崩溃是不可避免的,遇到这个问题,有时会出现一大堆的crash日志,艹,貌似看不懂呀 ...
- Mysql---DML 语句与DCL语句
DML 语句: DML 操作是指对数据库中表记录的操作,主要包括表记录的插入(insert).更新(update).删除(delete)和查(select),是开发人员日常使用最频繁的操作.下面将依次 ...
- Android --固定底部
相对布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
- python多进程(三种方法)
#!/usr/bin/env python # -*- coding:utf- -*- from multiprocessing import Pool import time def f(x): t ...