添加下载状态通知功能

This commit is contained in:
huangzhuanghua
2016-10-11 10:07:04 +08:00
parent 4262ec7386
commit 896c4e980c
12 changed files with 311 additions and 189 deletions

View File

@ -1,9 +1,6 @@
package com.gh.gamecenter;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@ -17,7 +14,6 @@ import android.os.SystemClock;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NotificationCompat;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
@ -47,7 +43,6 @@ import com.gh.common.util.NetworkUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.common.util.RandomUtils;
import com.gh.common.util.RunningUtils;
import com.gh.common.util.SpeedUtils;
import com.gh.common.util.TokenUtils;
import com.gh.common.util.TrafficUtils;
@ -154,6 +149,7 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
return;
} else if (DownloadStatus.neterror.equals(downloadEntity.getStatus())
|| DownloadStatus.timeout.equals(downloadEntity.getStatus())) {
toast("网络不稳定,下载任务已暂停");
uploadNeterrorLog(downloadEntity);
}
if (downloadEntity.getName().contains("光环助手") && isShowDownload) {
@ -169,60 +165,32 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
} else {
statDoneEvent(downloadEntity);
if (RunningUtils.isApplicationBroughtToBackground(getApplicationContext())) {
// 应用程序在后台如果是插件化下载则弹出notification提示用户
String platform = PlatformUtils.getInstance(getApplicationContext())
.getPlatformName(downloadEntity.getPlatform());
if (platform != null) {
if (downloadEntity.isPluggable()) {
String path = downloadEntity.getPath();
String platform = PlatformUtils.getInstance(getApplicationContext())
.getPlatformName(downloadEntity.getPlatform());
String title = downloadEntity.getName() + " - " + platform;
Intent intent = new Intent();
intent.putExtra("path", path);
intent.setAction("com.gh.gamecenter.UNINSTALL");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.logo)
.setTicker(title)
.setContentTitle(title)
.setContentText("下载完成,点击继续插件化。")
.setContentIntent(pendingIntent)
.build();
notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音
notification.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时通知将被清除。
NotificationManager notificationManager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(((int) System.currentTimeMillis() / 1000), notification);
}
} else {
String platform = PlatformUtils.getInstance(getApplicationContext())
.getPlatformName(downloadEntity.getPlatform());
if (platform != null) {
if (downloadEntity.isPluggable()) {
// 弹出插件化提示框
EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath()));
} else if (platform.equals("官方版")) {
toast(downloadEntity.getName() + " - 下载完成");
} else {
toast(downloadEntity.getName() + " - " + platform + " - 下载完成");
}
// 弹出插件化提示框
EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath()));
} else if (downloadEntity.isPlugin()) {
toast(downloadEntity.getName() + " - " + platform + " - 下载完成");
} else {
toast(downloadEntity.getName() + " - 下载完成");
}
if (!downloadEntity.isPluggable()) {
// 是否是自动安装
if (sp.getBoolean("autoinstall", true)) {
if (FileUtils.isEmptyFile(downloadEntity.getPath())) {
toast("解析包出错(可能被误删了),请重新下载");
DownloadManager.getInstance(MainActivity.this).cancel(downloadEntity.getUrl());
} else {
toast(downloadEntity.getName() + " - 下载完成");
}
if (!downloadEntity.isPluggable()) {
// 是否是自动安装
if (sp.getBoolean("autoinstall", true)) {
if (FileUtils.isEmptyFile(downloadEntity.getPath())) {
toast("解析包出错(可能被误删了),请重新下载");
DownloadManager.getInstance(MainActivity.this).cancel(downloadEntity.getUrl());
} else {
if (PackageUtils.isCanLaunchSetup(getApplicationContext(), downloadEntity.getPath())) {
startActivity(PackageUtils.getInstallIntent(downloadEntity.getPath()));
} else {
if (PackageUtils.isCanLaunchSetup(getApplicationContext(), downloadEntity.getPath())) {
startActivity(PackageUtils.getInstallIntent(downloadEntity.getPath()));
} else {
// 弹出卸载提示框
EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath()));
}
// 弹出卸载提示框
EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath()));
}
}
}
@ -462,9 +430,6 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
// 初始化PlatformUtils
PlatformUtils.getInstance(getApplicationContext());
// 解决助手奔溃后导致的下载状态保留问题
DownloadManager.getInstance(this).checkAll();
// 添加观察者
DownloadManager.getInstance(this).addObserver(dataWatcher);
@ -1019,8 +984,11 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene
toIntent.putExtra("newsId", getIntent().getExtras().getString("newsId"));
toIntent.putExtra("entrance", getIntent().getExtras().getString("entrance"));
} else if("DownloadManagerActivity".equals(to)) {
toIntent.putExtra("packageName" , getIntent().getExtras().getString("packageName"));
toIntent.putExtra("currentItem" , 1);
String packageName = getIntent().getExtras().getString("packageName");
if (packageName != null) {
toIntent.putExtra("packageName" , getIntent().getExtras().getString("packageName"));
toIntent.putExtra("currentItem" , 1);
}
} else if ("GameDetailsActivity".equals(to) || "GameDetailActivity".equals(to)) {
toIntent.putExtra("gameId", getIntent().getExtras().getString("gameId"));
toIntent.putExtra("entrance", getIntent().getExtras().getString("entrance"));