使用Notification更新多任务下载进度。大牛请绕路!!!
在网上真心没找到实现这样的代码,工作需要,只能苦X 自己实现了。
转载请注明:http://cn23snyga.iteye.com/blog/1902071
如图:
启动页面NotificationActivity06:(从这文件名,就可以看出我苦X 了多少个版本)
package com.example.notification06; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import com.example.services.DownloadServices; public class NotificationActivity06 extends Activity { private Context mContext = NotificationActivity06.this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void downloadNP(View view) { startDownloadService(0, "http://61.50.254.57:8088/nature-person/mobilenature/download/NaturalSaler_common.apk"); } public void downloadYUN(View view) { startDownloadService(1, "http://mix.911860.com/resources/apkupdate/7/common/mix-common.apk"); } public void downloadTNews(View view) { startDownloadService(2, "http://mix.911860.com/resources/mix/custom-apks/01-TencentNews/TencentNews.apk"); } public void startDownloadService(int notifyId, String url) { Intent i = new Intent(mContext, DownloadServices.class); i.putExtra("url", url); i.putExtra("notifyId", notifyId); mContext.startService(i); } }
其中的下载链接,换成自己需要的就好了。
下载服务DownloadServices:
package com.example.services; import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Timer; import java.util.TimerTask; import android.app.Notification; import android.app.NotificationManager; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.widget.Toast; import com.example.bean.DownloadTask; import com.example.bean.NotificationBean; import com.example.notification06.R; import com.example.utils.DownloadUtil; import com.example.utils.DownloadUtil.IOnDownloadListener; public class DownloadServices extends Service { private Context mContext = DownloadServices.this; /** 正在下载 */ private final int DOWN_LOADING = 0; /** 下载完成 */ private final int DOWN_COMPLETE = 1; /** 下载失败 */ private final int DOWN_ERR = 2; /** Timer 执行时间间隔 */ private final int TIMER_PERIOD = 1500; protected Timer mTimer; protected NotificationManager mNotificationManager; /** 下载任务管理 */ protected Map<String, DownloadTask> map_downloadtask; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { super.onDestroy(); } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); mTimer = new Timer(); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); map_downloadtask = new HashMap<String, DownloadTask>(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { String mUrl = intent.getExtras().getString("url"); int mNotifyId = intent.getExtras().getInt("notifyId"); Notification mNotification = new NotificationBean(this, R.drawable.ic_launcher, "开始下载", System.currentTimeMillis()); System.out.println("NotifyId = " + mNotifyId); if(map_downloadtask.containsKey(mUrl)){ Toast.makeText(mContext, "已存在此下载任务", Toast.LENGTH_SHORT).show(); } else{ DownloadTask mDownloadTask = new DownloadTask(); mDownloadTask.setUrl(mUrl); mDownloadTask.setNotifyID(mNotifyId); mDownloadTask.setNotification(mNotification); map_downloadtask.put(mUrl, mDownloadTask); Runnable mRunnable = new MyRunnable(mDownloadTask); new Thread(mRunnable).start(); } return super.onStartCommand(intent, flags, startId); } class MyRunnable implements Runnable { private DownloadUtil mDownUtil = new DownloadUtil(); private DownloadTask mDownTask; private Handler mHandler; private TimerTask mTimerTask; public MyRunnable(DownloadTask downTask) { super(); this.mDownTask = downTask; this.mHandler = new MyHandler(mDownUtil); this.mTimerTask = new MyTimerTask(mDownUtil, mHandler, mDownTask); } @Override public void run() { mTimer.schedule(mTimerTask, 0, TIMER_PERIOD); mDownUtil.downloadFile(mDownTask.getUrl()); } } class MyTimerTask extends TimerTask { private Handler mHandler; private DownloadUtil mDownUtil; private DownloadTask mDownTask; private IOnDownloadListener mListener; public MyTimerTask(DownloadUtil downUtil, Handler handler, DownloadTask downTask) { super(); this.mHandler = handler; this.mDownUtil = downUtil; this.mDownTask = downTask; this.mListener = new IOnDownloadListener() { @Override public void updateNotification(int progress, int totalSize, File downFile) { // TODO Auto-generated method stub int rate = 0; // 计算百分比 if (totalSize > 0) { rate = progress * 100 / totalSize; mHandler.obtainMessage(DOWN_LOADING, rate, mDownTask.getNotifyID(), mDownTask.getNotification()).sendToTarget(); } else if (totalSize == 0) { mHandler.obtainMessage(DOWN_LOADING, 0, mDownTask.getNotifyID(), mDownTask.getNotification()).sendToTarget(); } else { cancel(); mHandler.obtainMessage(DOWN_ERR, mDownTask).sendToTarget(); } // 是否下载结束 if (totalSize > 0 && null != downFile && totalSize == (int) downFile.length()) { cancel(); mHandler.obtainMessage(DOWN_COMPLETE, downFile).sendToTarget(); map_downloadtask.remove(mDownTask.getUrl());// 移除已完成任务 System.out.println("DOWN_COMPLETE ==> totalSize ==> " + totalSize); } } }; } @Override public void run() { mDownUtil.setOnDownloadListener(mListener); } } class MyHandler extends Handler { private DownloadUtil mDownUtil; public MyHandler(DownloadUtil downUtil) { super(); this.mDownUtil = downUtil; } @Override public void handleMessage(Message msg) { switch (msg.what) { case DOWN_LOADING: ((Notification)msg.obj).contentView.setProgressBar(R.id.pb, 100, msg.arg1, false); ((Notification)msg.obj).contentView.setTextViewText(R.id.tv, "下载" + msg.arg1 + "%"); mNotificationManager.notify(msg.arg2, ((Notification)msg.obj)); System.out.println("DOWN_LOADING --> mNotifyId --> " + msg.arg2 + " --> " + msg.arg1 + "%"); break; case DOWN_COMPLETE: // mNotificationManager.cancel(mNotifyId); removeMessages(DOWN_LOADING); Toast.makeText(mContext, "下载完成", Toast.LENGTH_SHORT).show(); mDownUtil.installApk(mContext, (File)msg.obj); System.out.println("======================DOWN_COMPLETE================================"); stopService(); break; case DOWN_ERR: removeMessages(DOWN_LOADING); map_downloadtask.remove(((DownloadTask)msg.obj).getUrl()); Toast.makeText(mContext, "下载失败", Toast.LENGTH_SHORT).show(); stopService(); break; default: break; } } /** * 如果无下载任务,关闭服务 */ private void stopService(){ if(map_downloadtask.isEmpty()){ stopSelf(-1); } } } }
封装Notification:
package com.example.bean; import android.app.Notification; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; import com.example.notification06.NotificationActivity06; import com.example.notification06.R; public class NotificationBean extends Notification { private Context mContext; public NotificationBean(Context context, int icon, CharSequence tickerText, long when) { super(icon, tickerText, when); this.mContext = context; this.flags = Notification.FLAG_AUTO_CANCEL; // |= // this.flags = Notification.FLAG_ONGOING_EVENT; RemoteViews mRemoteView = new RemoteViews(mContext.getPackageName(), R.layout.remote_view); this.contentView = mRemoteView; Intent intent = new Intent(mContext, NotificationActivity06.class); // 点击安装APK 未实现 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); this.contentIntent = pIntent; } }
封装下载任务DownloadTask:
package com.example.bean; import android.app.Notification; public class DownloadTask { private String url; private int notifyID; private Notification notification; public DownloadTask() { // TODO Auto-generated constructor stub } public Notification getNotification() { return notification; } public void setNotification(Notification notification) { this.notification = notification; } public int getNotifyID() { return notifyID; } public void setNotifyID(int notifyID) { this.notifyID = notifyID; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
完毕。
相关推荐
android实现Service后台下载Notification进度条
NULL 博文链接:https://l540151663.iteye.com/blog/2022729
Android notification进度条 demo,欢迎下载!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NULL 博文链接:https://wosyingjun.iteye.com/blog/2241628
类似于QQ音乐更新,绑定服务,AsyncTask HttpURLConnection下载xx.apk,同步用notification更新进度条。
Android 实现自定义通知栏,并不断更新通知栏的进度条,模拟下载效果。 注:只是模拟下载进度,没有下载功能。
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
本文实例讲述了Android实现为Notification加上一个进度条的方法。分享给大家供大家参考,具体如下: package com.notification; import android.app.Activity; import android.app.Notification; import android....
android 利用notification 通知栏 实现动态下载.
这个demo主要介绍了带有进度条的APP的下载更新,点击notification就可以安装了
带进度条(ProgressBar)的Notification
android自动更新异步线程和NOTIFICATION的方式
这个例子演示Android 在状态栏添加Notification信息图标及提示,相信大家对这个功能已经不陌生了,手机中安装的APP,一般都会在后台运行,时不时会在手机顶部的状态栏中显示应用的图标,滑出状态栏会看到详细的信息...
Android NOtification 使用Android NOtification 使用
这几天做一个小软件在API28(Android 9.0)的模拟器上测试时,发现通知栏无效,经过一番查询,了解到:API26(Android 8.0)以后,引入了**通知渠道(Notification Channels)**这么一个东西来帮助用户管理通知。...
android notification 软件更新
使用android push notification service 实现即时通知