Skip to content

Commit

Permalink
更新添加通知栏
Browse files Browse the repository at this point in the history
  • Loading branch information
maning committed Apr 5, 2017
1 parent 1e0c13e commit 1831f12
Show file tree
Hide file tree
Showing 3 changed files with 477 additions and 6 deletions.
50 changes: 46 additions & 4 deletions app/src/main/java/com/maning/gankmm/ui/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.maning.gankmm.ui.activity;

import android.Manifest;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
Expand All @@ -18,6 +20,7 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.sdk.android.feedback.impl.FeedbackAPI;
import com.amap.api.location.AMapLocation;
Expand All @@ -43,6 +46,7 @@
import com.maning.gankmm.utils.IntentUtils;
import com.maning.gankmm.utils.MySnackbar;
import com.maning.gankmm.utils.NetUtils;
import com.maning.gankmm.utils.NotifyUtil;
import com.maning.gankmm.utils.SharePreUtil;
import com.socks.library.KLog;
import com.umeng.analytics.MobclickAgent;
Expand Down Expand Up @@ -97,6 +101,8 @@ public class MainActivity extends BaseActivity implements IMainView, View.OnClic
private MaterialDialog dialogCloseWarn;
private static final int citysChooseRequestCode = 10001;

private NotifyUtil notifyUtils;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -461,41 +467,57 @@ public void onCancel() {
});
}


private void showDownloadDialog(AppUpdateInfo appUpdateInfo) {
dialogUpdate = new MaterialDialog.Builder(MainActivity.this)
.title("正在下载最新版本")
.content("请稍等")
.canceledOnTouchOutside(false)
.cancelable(false)
.progress(false, 100, false)
.negativeText("后台下载")
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
startNotifyProgress();
}
})
.show();

new InstallUtils(context, appUpdateInfo.getInstall_url(), Constants.UpdateAPKPath, "GankMM_" + appUpdateInfo.getVersionShort(), new InstallUtils.DownloadCallBack() {
@Override
public void onStart() {
KLog.i("onStart");
KLog.i("installAPK-----onStart");
if (dialogUpdate != null) {
dialogUpdate.setProgress(0);
}
}

@Override
public void onComplete(String path) {
KLog.i("onComplete:" + path);
KLog.i("installAPK----onComplete:" + path);
InstallUtils.installAPK(context, path);
if (dialogCloseWarn != null && dialogCloseWarn.isShowing()) {
dialogCloseWarn.dismiss();
}
if (dialogUpdate != null && dialogUpdate.isShowing()) {
dialogUpdate.dismiss();
}
if (notifyUtils != null) {
notifyUtils.setNotifyProgressComplete();
notifyUtils.clear();
}
}

@Override
public void onLoading(long total, long current) {
KLog.i("onLoading:-----total:" + total + ",current:" + current);
KLog.i("installAPK-----onLoading:-----total:" + total + ",current:" + current);
int currentProgress = (int) (current * 100 / total);
if (dialogUpdate != null) {
dialogUpdate.setProgress((int) (current * 100 / total));
dialogUpdate.setProgress(currentProgress);
}
if (notifyUtils != null) {
notifyUtils.setNotifyProgress(100, currentProgress, false);
}
}

Expand All @@ -507,10 +529,30 @@ public void onFail(Exception e) {
if (dialogUpdate != null && dialogUpdate.isShowing()) {
dialogUpdate.dismiss();
}
if (notifyUtils != null) {
notifyUtils.clear();
}
}
}).downloadAPK();
}


/**
* 开启通知栏
*/
private void startNotifyProgress() {
//设置想要展示的数据内容
Intent intent = new Intent(this, SettingActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent rightPendIntent = PendingIntent.getActivity(this,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
int smallIcon = R.mipmap.ic_launcher;
String ticker = "正在下载干货营更新包...";
//实例化工具类,并且调用接口
notifyUtils = new NotifyUtil(this, 0);
notifyUtils.notify_progress(rightPendIntent, smallIcon, ticker, "干货营 下载", "正在下载中...", false, false, false);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// 只需要调用这一句,其它的交给AndPermission吧,最后一个参数是PermissionListener。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.maning.gankmm.ui.activity;

import android.Manifest;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ImageView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.maning.gankmm.R;
import com.maning.gankmm.bean.AppUpdateInfo;
Expand All @@ -23,6 +26,7 @@
import com.maning.gankmm.utils.IntentUtils;
import com.maning.gankmm.utils.MySnackbar;
import com.maning.gankmm.utils.NetUtils;
import com.maning.gankmm.utils.NotifyUtil;
import com.maning.gankmm.utils.SharePreUtil;
import com.socks.library.KLog;
import com.umeng.analytics.MobclickAgent;
Expand Down Expand Up @@ -62,6 +66,7 @@ public class SettingActivity extends BaseActivity implements ISettingView {
private MaterialDialog dialogCloseWarn;

private AppUpdateInfo appUpdateInfo;
private NotifyUtil notifyUtils;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -270,8 +275,15 @@ private void showDownloadDialog(AppUpdateInfo appUpdateInfo) {
.title("正在下载最新版本")
.content("请稍等")
.canceledOnTouchOutside(false)
.cancelable(true)
.cancelable(false)
.progress(false, 100, false)
.negativeText("后台下载")
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
startNotifyProgress();
}
})
.show();

new InstallUtils(context, appUpdateInfo.getInstall_url(), Constants.UpdateAPKPath, "GankMM_" + appUpdateInfo.getVersionShort(), new InstallUtils.DownloadCallBack() {
Expand All @@ -293,13 +305,21 @@ public void onComplete(String path) {
if (dialogUpdate != null && dialogUpdate.isShowing()) {
dialogUpdate.dismiss();
}
if (notifyUtils != null) {
notifyUtils.setNotifyProgressComplete();
notifyUtils.clear();
}
}

@Override
public void onLoading(long total, long current) {
KLog.i("installAPK-----onLoading:-----total:" + total + ",current:" + current);
int currentProgress = (int) (current * 100 / total);
if (dialogUpdate != null) {
dialogUpdate.setProgress((int) (current * 100 / total));
dialogUpdate.setProgress(currentProgress);
}
if (notifyUtils != null) {
notifyUtils.setNotifyProgress(100, currentProgress, false);
}
}

Expand All @@ -311,10 +331,30 @@ public void onFail(Exception e) {
if (dialogUpdate != null && dialogUpdate.isShowing()) {
dialogUpdate.dismiss();
}
if (notifyUtils != null) {
notifyUtils.clear();
}
}
}).downloadAPK();
}


/**
* 开启通知栏
*/
private void startNotifyProgress() {
//设置想要展示的数据内容
Intent intent = new Intent(this, SettingActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent rightPendIntent = PendingIntent.getActivity(this,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
int smallIcon = R.mipmap.ic_launcher;
String ticker = "正在下载干货营更新包...";
//实例化工具类,并且调用接口
notifyUtils = new NotifyUtil(this, 0);
notifyUtils.notify_progress(rightPendIntent, smallIcon, ticker, "干货营 下载", "正在下载中...", false, false, false);
}

@Override
public void showBaseProgressDialog(String msg) {
showProgressDialog(msg);
Expand Down
Loading

0 comments on commit 1831f12

Please sign in to comment.