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(背景程序),通过背 ...
随机推荐
- python环境搭建
Python下载 Python最新源码,二进制文档,新闻资讯等可以在Python的官网查看到: Python官网:http://www.python.org/ 你可以在一下链接中下载Python的文档 ...
- HBase的架构以及各个模块的功能
一:整体架构 1.体系结构 2.物理模型 3.存储体系 regionserver->region->多个store(列簇)->一个memstore和多个storefile 4.HDF ...
- C# 时间与时间戳互转 13位
/// <summary> /// 将c# DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="t ...
- [LeetCode]题解(python):103 Binary Tree Zigzag Level Order Traversal
题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, re ...
- Flash Socket简单调试工具
写了一个简单的Flash Socket调试工具,可用来简单调试本地或者外部socket服务器,使用的时候注意Flash socket的安全策略问题,有问题请联系sky-wang@qq.com.
- 树莓派连接wifi
使用树莓派,通过无线网卡连接wifi,再通过远程桌面或者ssh的连接树莓派比较方便,本文记录树莓派wifi如何设置. 参考链接: http://www.jianshu.com/p/b42e8d3df4 ...
- OceanBase架构浅析(二)
单点性能 OceanBase架构的优势在于既支持跨行跨表事务,又支持存储服务器线性扩展.当然,这个架构也有一个明显的缺陷:UpdateServer单点,这个问题限制了OceanBase集群的整体读写性 ...
- T4模版生成多个实体文件时,提示找不到 Host
T4模版生成多个实体文件时,提示找不到 Host 使用以下方法,把hostspecific改为true就可以了 hostspecific:有效值true.false,默认为false.如果将此特性的值 ...
- 面向对象编程(八)——this关键字
很多人一提起它,就说“当前对象”,可到底什么是当前对象,是什么当前对象,他自己也不清楚. this(隐式参数) 普通方法中,this总是指向当前调用该方法的对象. 构造方法中,this总是指向正要初始 ...
- Janus WinForms Controls
http://blog.sina.com.cn/s/blog_68eb92020101kpw8.html