先放個gif。。最終效果如果:

主要演示了Android從服務器下載文件,調用Notification顯示下載進度,并且在下載完畢以后點擊通知會跳轉到安裝APK的界面,演示是在真實的網絡環境中使用真實的URL進行演示,來看看代碼:
MainActivity代碼非常簡單,就是啟動一個Service:
public class MainActivity extends AppCompatActivity { String download_url="http://shouji.360tpcdn.com/160329/a9037075b8d3aa98fbf6115c54a5b895/com.alensw.PicFolder_4722404.apk"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void bt_start_service(View view){ Intent intent=new Intent(this,DownLoadService.class); intent.putExtra("download_url",download_url); startService(intent); }}DownLoadService里面,在onStartCommand方法里面是關鍵代碼,調用NotifyUtil這個工具類的“notify_progress”方法去顯示一個通知,與此同時開始下載APK文件,DownLoadService代碼如下:
public class DownLoadService extends Service { String download_url; String savePath= Environment.getExternalStorageDirectory()+"/liulan.apk"; private int requestCode = (int) SystemClock.uptimeMillis(); private NotifyUtil currentNotify; File mFile; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { mFile=new File(savePath); download_url=intent.getStringExtra("download_url"); Log.e("test","執行onStartCommand"); //設置想要展示的數據內容 Intent intent_noti = new Intent(); intent_noti.setAction(Intent.ACTION_VIEW); //文件的類型,從tomcat里面找 intent_noti.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive"); PendingIntent rightPendIntent = PendingIntent.getActivity(this, requestCode, intent_noti, PendingIntent.FLAG_UPDATE_CURRENT); int smallIcon = R.drawable.xc_smaillicon; String ticker = "正在更新快圖瀏覽"; //實例化工具類,并且調用接口 NotifyUtil notify7 = new NotifyUtil(this, 7); notify7.notify_progress(rightPendIntent, smallIcon, ticker, "快圖瀏覽升級程序", "正在下載中", false, false, false, download_url, savePath, new NotifyUtil.DownLoadListener() { @Override public void OnSuccess(File file) { mFile=file; DownLoadService.this.stopSelf(); } @Override public void onFailure(Throwable t, int errorNo, String strMsg) { } }); currentNotify = notify7; return super.onStartCommand(intent, flags, startId); }}在調用“notify_progress”方法的時候,已經開始下載文件了,那么下載的代碼是什么呢?如下:
public void notify_progress(PendingIntent pendingIntent, int smallIcon, String ticker, String title, String content, boolean sound, boolean vibrate, boolean lights, String download_url, String savePath, final DownLoadListener listener) { setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights); /* * 因為進度條要實時更新通知欄也就說要不斷的發送新的提示,所以這里不建議開啟通知聲音。 * 這里是作為范例,給大家講解下原理。所以發送通知后會聽到多次的通知聲音。 */ FinalHttp fh = new FinalHttp(); HttpHandler<File> httpHandler=fh.download(download_url, savePath, new AjaxCallBack<File>() { @Override public void onLoading(long count, long current) { super.onLoading(count, current); double a=count; double b=current; double currentPro=(double)((b/a)*100); cBuilder.setProgress(100, (int)currentPro, false); sent(); } @Override public void onSuccess(File file) { super.onSuccess(file); cBuilder.setContentText("下載完成").setProgress(0, 0, false); sent(); listener.OnSuccess(file); } @Override public void onFailure(Throwable t, int errorNo, String strMsg) { super.onFailure(t, errorNo, strMsg); listener.onFailure(t,errorNo,strMsg); } }); }這里用到了afinal.jar
這個jar已經封裝好下載的工具類,我們直接拿來用就行。下載成功之后會通過DownLoadListener這個接口回調到DownLoadService里面,最終運行效果就如最上面那個gif動態圖運行效果一樣。
項目下載地址:點擊下載
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
|
新聞熱點
疑難解答