Sameer Z...

16

使用像我从服务器下载文件并放入SD卡的服务下载它的最佳方式也使用通知.它是相当长的代码,但我认为完美的一个,如果不明白任何事情,那么请去android开发者博客的服务.

public class DownloadService extends Service{

SharedPreferences preferences;

private static final String DOCUMENT_VIEW_STATE_PREFERENCES = "DjvuDocumentViewState";

private Looper mServiceLooper;

private ServiceHandler mServiceHandler;

private NotificationManager mNM;

String downloadUrl;

public static boolean serviceState=false;

// Handler that receives messages from the thread

private final class ServiceHandler extends Handler {

public ServiceHandler(Looper looper) {

super(looper);

}

@Override

public void handleMessage(Message msg) {

downloadFile();

showNotification(getResources().getString(R.string.notification_catalog_downloaded),"VVS");

stopSelf(msg.arg1);

}

}

@Override

public void onCreate() {

serviceState=true;

mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

HandlerThread thread = new HandlerThread("ServiceStartArguments",1);

thread.start();

// Get the HandlerThread's Looper and use it for our Handler

mServiceLooper = thread.getLooper();

mServiceHandler = new ServiceHandler(mServiceLooper);

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.d("SERVICE-ONCOMMAND","onStartCommand");

Bundle extra = intent.getExtras();

if(extra != null){

String downloadUrl = extra.getString("downloadUrl");

Log.d("URL",downloadUrl);

this.downloadUrl=downloadUrl;

}

Message msg = mServiceHandler.obtainMessage();

msg.arg1 = startId;

mServiceHandler.sendMessage(msg);

// If we get killed, after returning from here, restart

return START_STICKY;

}

@Override

public void onDestroy() {

Log.d("SERVICE-DESTROY","DESTORY");

serviceState=false;

//Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();

}

@Override

public IBinder onBind(Intent intent) {

// We don't provide binding, so return null

return null;

}

public void downloadFile(){

downloadFile(this.downloadUrl,fileName);

}

void showNotification(String message,String title) {

// In this sample, we'll use the same text for the ticker and the expanded notification

CharSequence text = message;

// Set the icon, scrolling text and timestamp

Notification notification = new Notification(R.drawable.icon, "vvs",

System.currentTimeMillis());

notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(this, HomeScreenActivity.class);

intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);

//The PendingIntent to launch our activity if the user selects this notification

PendingIntent contentIntent = PendingIntent.getActivity(this.getBaseContext(), 0,

intent, PendingIntent.FLAG_CANCEL_CURRENT);

// Set the info for the views that show in the notification panel.

notification.setLatestEventInfo(this, title,

text, contentIntent);

// Send the notification.

// We use a layout id because it is a unique number. We use it later to cancel.

mNM.notify(R.string.app_name, notification);

}

public void downloadFile(String fileURL, String fileName) {

StatFs stat_fs = new StatFs(Environment.getExternalStorageDirectory().getPath());

double avail_sd_space = (double)stat_fs.getAvailableBlocks() *(double)stat_fs.getBlockSize();

//double GB_Available = (avail_sd_space / 1073741824);

double MB_Available = (avail_sd_space / 10485783);

//System.out.println("Available MB : " + MB_Available);

Log.d("MB",""+MB_Available);

try {

File root =new File(Environment.getExternalStorageDirectory()+"/vvveksperten");

if(root.exists() && root.isDirectory()) {

}else{

root.mkdir();

}

Log.d("CURRENT PATH",root.getPath());

URL u = new URL(fileURL);

HttpURLConnection c = (HttpURLConnection) u.openConnection();

c.setRequestMethod("GET");

c.setDoOutput(true);

c.connect();

int fileSize = c.getContentLength()/1048576;

Log.d("FILESIZE",""+fileSize);

if(MB_Available <= fileSize ){

this.showNotification(getResources().getString(R.string.notification_no_memory),getResources().getString(R.string.notification_error));

c.disconnect();

return;

}

FileOutputStream f = new FileOutputStream(new File(root.getPath(), fileName));

InputStream in = c.getInputStream();

byte[] buffer = new byte[1024];

int len1 = 0;

while ((len1 = in.read(buffer)) > 0) {

f.write(buffer, 0, len1);

}

f.close();

File file = new File(root.getAbsolutePath() + "/" + "some.pdf");

if(file.exists()){

file.delete();

Log.d("FILE-DELETE","YES");

}else{

Log.d("FILE-DELETE","NO");

}

File from =new File(root.getAbsolutePath() + "/" + fileName);

File to = new File(root.getAbsolutePath() + "/" + "some.pdf");

} catch (Exception e) {

Log.d("Downloader", e.getMessage());

}

应用程序被杀后它是否有效? (3认同)

android下载图片服务,Android中的背景图片下载需要帮助吗?相关推荐

  1. java图片覆盖_java中插入背景图片为什么总是覆盖掉按钮啊

    主函数: public static void main(String[] args) { MsClient client=new MsClient("聊天进击~",410,100 ...

  2. 怎样在android中添加背景图片?

    怎样在android中添加背景图片? 1.打开左侧的工程文件夹.打开res中的layout,双击main.xml. 2.一般的图片是jpg格式的,要把格式转化为png格式的. 3.把PNG格式的图片拖 ...

  3. Android实现系统相册选择APP全局背景图片

    最近项目需要实现一个功能:用户在系统相册里选择并更换APP全部界面的背景图片. 基本思路: 1,用户点击"选择背景图片"按钮后,跳转到系统相册中选择图片.APP把选择的图片压缩后单 ...

  4. table vue 背景图片_table中background背景图片自动拉伸

    解决android:background背景图片被拉伸问题 ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而s ...

  5. echarts热力背景图_Echarts 图表中设置背景图片

    在项目开发过程中,遇到在Echarts图表上添加背景图的需求,通过查找Echarts官网的配置项,发现 graphic 能够实现在图表中设置背景图片. image 在Echarts的实例中,找到一个实 ...

  6. Vue中添加背景图片

    往一个div中添加背景图片 bgImg:为图片地址 希望能帮到你!!

  7. html语言设置网页背景,HTML+CSS入门 设置网页中的背景图片的5个属性

    本篇教程介绍了HTML+CSS入门 设置网页中的背景图片的5个属性,希望阅读本篇文章以后大家有所收获,帮助大家HTML+CSS入门. < 在css代码中设置背景图片的方法,包括背景图片.背景重复 ...

  8. 在vue中设置背景图片

    在vue中设置背景图片 在App.vue中将样式中默认的margin-top 改为0,使得页面顶部不留白 #app {font-family: 'Avenir', Helvetica, Arial, ...

  9. Vue项目中设置背景图片

    在Vue项目开发中我们经常要向页面中添加背景图片,可是当我们在样式中添加了背景图片后,编译打包后,配置到服务器上时,由于路径解析的问题,图片并不能够正确的显示出来,如下CSS样式: backgroun ...

  10. 简历背景图片设置html代码,怎么在简历中添加背景图片

    在简历中添加背景图片的方法二 步骤1.在菜单栏选择图片->自来文件->选择图片后->确定; 步骤2.选图片->右键-->设置图片格式->版式->衬于文字下方; ...

最新文章

  1. 解决Android5.0以后DatePicker选择时间无效的bug。
  2. 深入理解RunLoop(转载)
  3. 文计笔记2: 计算机硬件知识
  4. 格力干的?顾客京东上买到二手奥克斯空调 京东、奥克斯回应...
  5. 计算机算法设计与分析 单峰序列
  6. Fragment的知识总结
  7. Git 常用命令 和 安装
  8. JavaScript常用算法 1
  9. Atitit 学习方法 -------体系化学习方法 Excel 科目,分类,专业 三级分类。。 知识点。。 课程就是每一个知识点的详细化。。 比如经济学 类别 专业 xx概论知识点 3、金
  10. 线性链表的建立与插入-----数据结构与算法笔记
  11. linux中为什么要分区,为什么要分区
  12. synctoy 远程同步_使用SyncToy 2.1同步计算机和驱动器之间的文件夹
  13. 值得您收藏的png图标第二辑
  14. html里的图片和文字并排显示
  15. 数学建模 多属性决策模型
  16. 分布式数据库查询机制
  17. pythoniter 2_python [iter(list)] * 2是什么意思?
  18. C++层Binder——Bn、Bp
  19. 【听】人性的弱点,人际关系交往的鸡汤宝典
  20. 自己写的将数组中的“null“字符串转成null的小工具类,报java.lang.NullPointerException

热门文章

  1. 调试寄存器:Debug Register
  2. 2018.12.11-21:45
  3. Django模板层:DTL模板渲染-变量
  4. python绘制星空图_【Python】基于某些列删除数据框中的重复值
  5. mybatis mysql merge_使用Mybatis和druid连接池报错 merge sql error, dbType mysql, sql
  6. 计算机汉字的输入和编辑教案,计算机汉字录入教案
  7. 基于matlab的捕食算法,运行捕食者算法(OpenTLD-master)
  8. 18100出多少取整_关于JavaScript数据类型,你知道多少?
  9. linux shell 版本信息,查看各种Linux系统版本信息的Shell命令
  10. java websocket原理_Java WebSocket基本原理