- 浏览: 518736 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
tangyunliang:
大哥你太历害了谢谢
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
u013015029:
LZ,请问下,在// 添加消息到聊天窗口 , 这里获取Ed ...
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
endual:
怎么保持会话,我搞不懂啊
Android基于XMPP Smack Openfire开发IM【一】登录openfire服务器 -
donala_zq:
显示:[2013-11-30 11:50:36 - Andro ...
android-----------新浪微博 -
donala_zq:
哥,运行不了啊
android-----------新浪微博
apk通过Notification显示下载进度,现在完成后-->安装
- 博客分类:
- Android源码
运行效果图:
工程结构图:
xml:
AndroidManifest.xml
项目源码:
工程结构图:
package com.cn.do1.downloadtest; import com.cn.do1.downloadtest.service.DownloadService; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; public class DownloadTest extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.down_file); Intent intent = new Intent(this,DownloadService.class); startService(intent); bindListener(); } void bindListener(){ findViewById(R.id.btn_load_btn_one).setOnClickListener(this); findViewById(R.id.btn_load_btn_two).setOnClickListener(this); findViewById(R.id.btn_load_btn_three).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_load_btn_one: DownloadService.downNewFile("http://m.appchina.com/market/e/882602/0/16/44DFFDF6E89D0CC2F5CB41CE041E9BB7/packagename.apk?refererPage=m.cherry.soft_list", 351, "百度知道"); break; case R.id.btn_load_btn_two: DownloadService.downNewFile("http://m.appchina.com/market/e/886657/feature/1/360F5D5A76A3E0E397451CE9EA503DE6/com.sogou.novel?refererPage=m.cherry.soft_main", 1071, "搜狗小说"); break; case R.id.btn_load_btn_three: DownloadService.downNewFile("http://m.appchina.com/market/e/885495/feature/2/360F5D5A76A3E0E397451CE9EA503DE6/com.king2.yyh?refererPage=m.cherry.soft_main", 1072, "君王2(新服开启)"); break; default: break; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode==KeyEvent.KEYCODE_BACK) { System.exit(0); return false; } return super.onKeyDown(keyCode, event); } }
package com.cn.do1.downloadtest.service; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import com.cn.do1.downloadtest.DownloadTest; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.os.Message; import android.widget.Toast; public class DownloadService extends Service{ private static NotificationManager nm; private static Notification notification; private static boolean cancelUpdate = false; private static MyHandler myHandler; private static ExecutorService executorService = Executors.newFixedThreadPool(5); // 固定五个线程来执行任务 public static Map<Integer,Integer> download = new HashMap<Integer, Integer>(); public static Context context; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); } @Override public void onCreate() { super.onCreate(); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); myHandler = new MyHandler(Looper.myLooper(), DownloadService.this); context = this; } @Override public void onDestroy() { super.onDestroy(); } public static void downNewFile(final String url,final int notificationId,final String name){ if(download.containsKey(notificationId)) return; notification = new Notification(); notification.icon = android.R.drawable.stat_sys_download; // notification.icon=android.R.drawable.stat_sys_download_done; notification.tickerText = name + "开始下载"; notification.when = System.currentTimeMillis(); notification.defaults = Notification.DEFAULT_LIGHTS; //显示在“正在进行中” notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; PendingIntent contentIntent = PendingIntent.getActivity(context, notificationId,new Intent(context, DownloadTest.class), 0); notification.setLatestEventInfo(context, name, "0%", contentIntent); download.put(notificationId, 0); // 将下载任务添加到任务栏中 nm.notify(notificationId, notification); // 启动线程开始执行下载任务 downFile(url,notificationId,name); } // 下载更新文件 private static void downFile(final String url,final int notificationId,final String name) { executorService.execute(new Runnable() { @Override public void run() { // TODO Auto-generated method stub File tempFile = null; try { HttpClient client = new DefaultHttpClient(); // params[0]代表连接的url HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); long length = entity.getContentLength(); InputStream is = entity.getContent(); if (is != null) { File rootFile = new File(Environment.getExternalStorageDirectory(),"/zhtrade"); if (!rootFile.exists() && !rootFile.isDirectory()) rootFile.mkdir(); tempFile = new File(Environment.getExternalStorageDirectory(),"/zhtrade/"+ url.substring(url.lastIndexOf("/"),url.indexOf("?"))+"_"+notificationId+".apk"); if (tempFile.exists()) tempFile.delete(); tempFile.createNewFile(); // 已读出流作为参数创建一个带有缓冲的输出流 BufferedInputStream bis = new BufferedInputStream(is); // 创建一个新的写入流,讲读取到的图像数据写入到文件中 FileOutputStream fos = new FileOutputStream(tempFile); // 已写入流作为参数创建一个带有缓冲的写入流 BufferedOutputStream bos = new BufferedOutputStream(fos); int read; long count = 0; int precent = 0; byte[] buffer = new byte[1024]; while ((read = bis.read(buffer)) != -1 && !cancelUpdate) { bos.write(buffer, 0, read); count += read; precent = (int) (((double) count / length) * 100); // 每下载完成1%就通知任务栏进行修改下载进度 if (precent - download.get(notificationId) >= 1) { download.put(notificationId, precent); Message message = myHandler.obtainMessage(3,precent); Bundle bundle = new Bundle(); bundle.putString("name", name); message.setData(bundle); message.arg1 = notificationId; myHandler.sendMessage(message); } } bos.flush(); bos.close(); fos.flush(); fos.close(); is.close(); bis.close(); } if (!cancelUpdate) { Message message = myHandler.obtainMessage(2, tempFile); message.arg1 = notificationId; Bundle bundle = new Bundle(); bundle.putString("name", name); message.setData(bundle); myHandler.sendMessage(message); } else { tempFile.delete(); } } catch (ClientProtocolException e) { if (tempFile.exists()) tempFile.delete(); Message message = myHandler.obtainMessage(4, name+"下载失败:网络异常!"); message.arg1 = notificationId; myHandler.sendMessage(message); } catch (IOException e) { if (tempFile.exists()) tempFile.delete(); Message message = myHandler.obtainMessage(4, name+"下载失败:文件传输异常"); message.arg1 = notificationId; myHandler.sendMessage(message); } catch (Exception e) { if (tempFile.exists()) tempFile.delete(); Message message = myHandler.obtainMessage(4, name+"下载失败,"+e.getMessage()); message.arg1 = notificationId; myHandler.sendMessage(message); } } }); } // 安装下载后的apk文件 private void Instanll(File file, Context context) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive"); context.startActivity(intent); } /* 事件处理类 */ class MyHandler extends Handler { private Context context; public MyHandler(Looper looper, Context c) { super(looper); this.context = c; } @Override public void handleMessage(Message msg) { PendingIntent contentIntent = null; super.handleMessage(msg); if (msg != null) { switch (msg.what) { case 0: Toast.makeText(context, msg.obj.toString(),Toast.LENGTH_SHORT).show(); download.remove(msg.arg1); break; case 1: break; case 2: contentIntent = PendingIntent.getActivity(DownloadService.this, msg.arg1,new Intent(DownloadService.this, DownloadTest.class), 0); notification.setLatestEventInfo(DownloadService.this, msg.getData().getString("name")+"下载完成", "100%",contentIntent); nm.notify(msg.arg1, notification); // 下载完成后清除所有下载信息,执行安装提示 download.remove(msg.arg1); nm.cancel(msg.arg1); Instanll((File) msg.obj, context); break; case 3: contentIntent = PendingIntent.getActivity(DownloadService.this, msg.arg1,new Intent(DownloadService.this, DownloadTest.class), 0); notification.setLatestEventInfo(DownloadService.this, msg.getData().getString("name")+"正在下载", download.get(msg.arg1) + "%",contentIntent); nm.notify(msg.arg1, notification); break; case 4: Toast.makeText(context, msg.obj.toString(),Toast.LENGTH_SHORT).show(); download.remove(msg.arg1); nm.cancel(msg.arg1); break; } } } } }
xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="3dp" android:paddingRight="3dp" > <Button android:id="@+id/btn_load_btn_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="开始一" /> <Button android:id="@+id/btn_load_btn_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_load_btn_one" android:layout_centerHorizontal="true" android:layout_marginTop="20dip" android:text="开始二" /> <Button android:id="@+id/btn_load_btn_three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@+id/btn_load_btn_two" android:layout_marginTop="20dip" android:text="开始三" /> </RelativeLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn.do1.downloadtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" /> <uses-permission android:name="androd.permission.INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".DownloadTest"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.cn.do1.downloadtest.service.DownloadService" android:label="DownloadService" ></service> </application> </manifest>
项目源码:
发表评论
-
android刮开效果
2013-12-04 14:58 0效果图: [img] [/img] package c ... -
火力篮球源码
2013-12-02 10:49 0效果图: [img] [/img] 源码: -
捏死它源码
2013-12-02 10:46 0捏死他源码: [img] [/img] 源码: -
Login的一些demo
2013-11-29 17:26 0login demo jar包混淆用的三个文件夹 -
圆形的进度条
2013-11-26 10:48 0效果图: [img] [/img] 源码: -
高仿丁丁优惠客户端
2013-11-26 10:45 0效果图: [img] [/img] [img] [/im ... -
仿腾讯新闻之内容定制
2013-11-19 13:51 0效果图: [img] [/img] [img] [/img ... -
客户端与服务端交互在线订餐app,功能流程仿俏江南app
2013-11-19 13:42 0知识点如下: 购物车模块 百度地图模块 ... -
视频聊天软件源码
2013-11-13 09:40 0效果图: [img] [/img] 源码: ... -
代码收藏:旋转木马效果
2013-10-21 09:41 0效果图: 源码: -
仿360悬浮窗效果
2013-10-11 16:40 0demo运行效果: 源码: -
Activity的一些进入退出动画
2013-09-26 09:11 0效果图: -
动画:图片移动到右下角
2013-09-25 15:17 0图片移动到右下角动画,简单学习一下。 -
仿360悬浮窗效果
2013-09-17 15:32 0效果不错 注释清楚 悬浮窗 -
android上的软件提示导航界面
2013-07-24 11:44 0... -
悬浮view(全部内存与使用内存)
2013-07-22 11:32 843http://daikainan.iteye. ... -
Android应用开发——Tencent微博(抽屉UI类型)
2013-06-13 09:17 0源码: -
android底部菜单栏demo
2013-06-20 09:26 824效果图: 源码: -
android二级listview列表
2013-06-09 10:18 0实现大众点评客户端的横向listview二级列表,先看一下样式 ... -
android书架效果
2013-06-09 10:09 0效果图: 源码:
相关推荐
Service下载apk通过notification显示下载进度 下载完成自动安装
DownAPKServiceDemo Service下载apk通过notification显示下载进度 下载完成自动安装
功能:从网上下载apk存储到手机指定目录,并在通知栏里显示其下载进度,如果下载错误,或者不想要该apk可以删除 涉及知识: 文件流、网络下载链接协议、读写权限、Handler、Notification、跑马灯
主要演示了Android从服务器下载文件,调用Notification显示下载进度,并且在下载完毕以后点击通知会跳转到安装APK的界面,演示是在真实的网络环境中使用真实的URL进行演示,来看看代码: MainActivity代码非常简单...
有2个下载案例 大家可酌情考虑运用...1. 服务1 okhttp + notification 通知栏带进度的下载apk 实现自动安装 点击通知栏安装 自定义通知栏样式 静默下载等 提供忽略当前版本 2. 服务2 运用downloadManager实现相同的效果
通过开源看DownloadManager多线程来下载网络资源,可自定义下载路径,同时通过notification通知栏的控制来展示下载进度,下载完成之后自动安装apk
通过开源看DownloadManager多线程来下载网络资源,可自定义下载路径,同时通过notification通知栏的控制来展示下载进度,下载完成之后自动安装apk,使用IntentService来后台下载
下载完成,接收回调(onDownLoadSuccess),显示Notification和Dialog 用户点击DownloadSuccessDialog或Notification即跳转到安装界面 二:流量的情况【用户自行操作】 显示更新app的UpdateDialog,用户点击更新后,开始...
类似于QQ音乐更新,绑定服务,AsyncTask HttpURLConnection下载xx.apk,同步用notification更新进度条。
5.29 模拟文件下载Notification进度变化与关闭 5.30 取得已安装应用程序列表与安装日期信息 5.31 造假的Toast画面 5.32 剪贴簿管理器 第6章 手机自动服务纪实 6.1 您有一条短信pop up提醒 6.2 手机电池计量还剩多少 ...
5.29 模拟文件下载Notification进度变化与关闭 5.30 取得已安装应用程序列表与安装日期信息 5.31 造假的Toast画面 5.32 剪贴簿管理器 第6章 手机自动服务纪实 6.1 您有一条短信pop up提醒 6.2 手机电池计量还剩多少 ...
5.29 模拟文件下载Notification进度变化与关闭 5.30 取得已安装应用程序列表与安装日期信息 5.31 造假的Toast画面 5.32 剪贴簿管理器 第6章 手机自动服务纪实 6.1 您有一条短信pop up提醒 6.2 手机电池计量还剩多少 ...
通常在使用service更新应用时最常出现的问题就是Notification进度的更新问题、service在什么时间关闭以及需要我们自己在Service中创建新的线程处理耗时操作,当然这种也是可以实现的但是会显得略微繁琐 经过对比...
5.29 模拟文件下载notification进度变化与关闭 285 5.30 取得已安装应用程序列表与安装日期信息 291 5.31 造假的toast画面 294 5.32 剪贴簿管理器 298 第6章 手机自动服务纪实 302 6.1 您有一条短信...