From 9bba3cd899d400aadfee511a0216183b34257944 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Wed, 18 Jan 2017 11:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0notification=E4=BE=8B?= =?UTF-8?q?=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/arialyy/simple/MainActivity.java | 5 + .../notification/SimpleNotification.java | 98 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 9 ++ 3 files changed, 112 insertions(+) create mode 100644 app/src/main/java/com/arialyy/simple/notification/SimpleNotification.java diff --git a/app/src/main/java/com/arialyy/simple/MainActivity.java b/app/src/main/java/com/arialyy/simple/MainActivity.java index 33a10ee8..0df80bbc 100644 --- a/app/src/main/java/com/arialyy/simple/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/MainActivity.java @@ -33,6 +33,7 @@ import com.arialyy.simple.databinding.ActivityMainBinding; import com.arialyy.simple.dialog_task.DownloadDialog; import com.arialyy.simple.fragment_task.FragmentActivity; import com.arialyy.simple.multi_task.MultiTaskActivity; +import com.arialyy.simple.notification.SimpleNotification; import com.arialyy.simple.pop_task.DownloadPopupWindow; import com.arialyy.simple.single_task.SingleTaskActivity; @@ -104,6 +105,10 @@ public class MainActivity extends BaseActivity { case R.id.fragment_task: startActivity(new Intent(this, FragmentActivity.class)); break; + case R.id.notification: + SimpleNotification notification = new SimpleNotification(this); + notification.start(); + break; } } } \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/notification/SimpleNotification.java b/app/src/main/java/com/arialyy/simple/notification/SimpleNotification.java new file mode 100644 index 00000000..3d11fd69 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/notification/SimpleNotification.java @@ -0,0 +1,98 @@ +package com.arialyy.simple.notification; + +import android.app.NotificationManager; +import android.content.Context; +import android.os.Environment; +import android.support.v4.app.NotificationCompat; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.task.Task; +import com.arialyy.frame.util.show.L; +import com.arialyy.frame.util.show.T; +import com.arialyy.simple.R; + +/** + * Created by Aria.Lao on 2017/1/18. + */ + +public class SimpleNotification { + private static final String DOWNLOAD_URL = + "http://static.gaoshouyou.com/d/6e/e5/ff6ecaaf45e532e6d07747af82357472.apk"; + + private NotificationManager mManager; + private Context mContext; + private NotificationCompat.Builder mBuilder; + private static final int mNotifiyId = 0; + + public SimpleNotification(Context context) { + mContext = context; + init(); + } + + private void init() { + mManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + mBuilder = new NotificationCompat.Builder(mContext); + mBuilder.setContentTitle("Aria Download Test") + .setContentText("进度条") + .setProgress(100, 0, false) + .setSmallIcon(R.mipmap.ic_launcher); + mManager.notify(mNotifiyId, mBuilder.build()); + Aria.whit(mContext).addSchedulerListener(new DownloadCallback(mBuilder, mManager)); + } + + public void start() { + Aria.whit(mContext) + .load(DOWNLOAD_URL) + .setDownloadName("notification_test.apk") + .setDownloadPath( + Environment.getExternalStorageDirectory() + "/Download/notification_test.apk") + .start(); + } + + public void stop() { + Aria.whit(mContext).load(DOWNLOAD_URL).stop(); + } + + private static class DownloadCallback extends Aria.SimpleSchedulerListener { + NotificationCompat.Builder mBuilder; + NotificationManager mManager; + + private DownloadCallback(NotificationCompat.Builder builder, NotificationManager manager) { + mBuilder = builder; + mManager = manager; + } + + @Override public void onTaskStart(Task task) { + super.onTaskStart(task); + } + + @Override public void onTaskPre(Task task) { + super.onTaskPre(task); + } + + @Override public void onTaskStop(Task task) { + super.onTaskStop(task); + } + + @Override public void onTaskRunning(Task task) { + super.onTaskRunning(task); + long len = task.getFileSize(); + int p = (int) (task.getCurrentProgress() * 100 / len); + if (mBuilder != null) { + mBuilder.setProgress(100, p, false); + mManager.notify(mNotifiyId, mBuilder.build()); + } + } + + @Override public void onTaskComplete(Task task) { + super.onTaskComplete(task); + if (mBuilder != null) { + mBuilder.setProgress(100, 100, false); + mManager.notify(mNotifiyId, mBuilder.build()); + } + } + + @Override public void onTaskCancel(Task task) { + super.onTaskCancel(task); + } + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 464bb3a5..08ccd012 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -56,5 +56,14 @@ style="?buttonBarButtonStyle" /> +