日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android icon在线更新,Android在线更新下载方案

發布時間:2024/1/23 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android icon在线更新,Android在线更新下载方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目的

App的在線更新是每一個項目必有的功能,但在過程當中會遇到一些問題,在此記錄下心得。

步驟

1、將最新版本號和本地版本號進行對比;

2、如需更新,則彈出更新提示對話框;

3、下載更新APK文件,并顯示進度條和通知欄;

4、安裝APK

具體流程

一、通過網絡接口獲取到線上最新版本號,將最新版本號和本地版本號進行對比

首先從網絡回調接口中獲取最新版本號,再將本地版本號與最新版本號進行對比,如果需要更新則彈出對話框

/**

* 更新提示對話框

*/

public void appUpdateDialog(final Context context, final UpdateCheckRetBean bean) {

new MaterialDialog.Builder(context)

.title("金米米") //標題內容

.titleColor(Color.parseColor("#FFC736")) //標題顏色

.iconRes(R.drawable.ic_logo_share) //圖標

.content(bean.getDetail()) //內容

.positiveText("立即更新") //選擇更新

.canceledOnTouchOutside(false) //觸摸窗口邊界以外是否關閉窗口,設置 false

.onPositive(new MaterialDialog.SingleButtonCallback() {

@Override

public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

dialog.dismiss();

downloadDialog(context, bean.getUrl()); //應用下載

setNotification(context, bean.getDetail()); //顯示通知欄

}

})

.negativeText("取消") //選擇取消

.onNegative(new MaterialDialog.SingleButtonCallback() {

@Override

public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

dialog.dismiss();

}

})

.show();

}

三、下載更新APK文件,并顯示進度條和通知欄

這部分是重點,分三部分解決:

1、顯示進度條:

/**

* 應用下載對話框

*

* @param downloadUrl APK下載鏈接

*/

private void downloadDialog(final Context mContext, String downloadUrl) {

downLoadDialog = new MaterialDialog.Builder(mContext)

.customView(R.layout.layout_app_update, false) //自定義View

.title("更新中...")

.iconRes(R.drawable.ic_logo_share)

.titleColor(Color.parseColor("#FFC736"))

.negativeText("取消下載") //選擇取消

.canceledOnTouchOutside(false) // 觸摸窗口邊界以外是否關閉窗口,設置 false

.onNegative(new MaterialDialog.SingleButtonCallback() {

@Override

public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

ToastUtils.showShortToast(mContext, "取消更新");

dialog.dismiss();

notificationManager.cancel(NOTIFICATION_ID); //取消通知欄

OkHttpClientUtil.getInstance().cancelOkHttp("download"); //取消下載更新

}

})

.build();

//攔截手機返回鍵

downLoadDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

@Override

public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

ToastUtils.showShortToast(mContext, "進入后臺下載模式");

return keyCode != KeyEvent.KEYCODE_BACK &&

downLoadDialog != null && downLoadDialog.isShowing();

}

});

npbDownload = downLoadDialog.getCustomView().findViewById(R.id.npb_download); //設置進度條

npbDownload.setProgressTextSize(45); //設置進度條字體大小

downLoadDialog.show();

downloadApk(mContext, downloadUrl); //下載更新APK

}

2、下載更新APK文件:

/**

* 下載更新APK

*

* @param downloadUrl APK下載鏈接

*/

private void downloadApk(final Context mContext, String downloadUrl) {

//下載APK

OkHttpClientUtil.getInstance().downloadFile("download", downloadUrl, new RequestParms(), Environment.getExternalStorageDirectory().getAbsolutePath() + "/lzt.apk", new OkHttpResponseListener() {

@Override

public void onSuccess(Object object) {

downLoadDialog.dismiss();

Intent intent = new Intent(Intent.ACTION_VIEW);

//判斷是否是AndroidN以及更高的版本

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileProvider", (File) object);

intent.setDataAndType(contentUri, "application/vnd.android.package-archive");

} else {

intent.setDataAndType(Uri.fromFile((File) object), "application/vnd.android.package-archive");

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

}

mContext.startActivity(intent);

}

@Override

public void onFailure(String errorMsg) {

ToastUtil.showShortToast(errorMsg);

}

@Override

public void onFileProgress(int progress) {

if (progress == 100) {

notificationManager.cancel(NOTIFICATION_ID);

}

npbDownload.setProgress(progress);

}

});

}

3、通知欄展示進度:

/**

* 創建通知欄

*/

private void setNotification(Context context, String detail) {

notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notification = new Notification.Builder(context);

notification.setSmallIcon(R.drawable.ic_16) //設置通知的圖標

.setTicker("正在加載更新包") //設置狀態欄的標題

.setContentTitle("正在加載更新包") //設置標題

.setContentText(detail) //設置內容

.setDefaults(Notification.FLAG_NO_CLEAR) //設置默認的提示音

.setPriority(Notification.PRIORITY_DEFAULT) //設置該通知的優先級

.setOngoing(true) //讓通知左右滑的時候不能取消通知

.setWhen(System.currentTimeMillis()) //設置通知時間,默認為系統發出通知的時間,通常不用設置

.setAutoCancel(true); //打開程序后圖標消失

//解決5.0系統通知欄白色Icon的問題

Drawable appIcon = getAppIcon(context);

Bitmap drawableToBitmap = null;

if (appIcon != null) {

drawableToBitmap = drawableToBitmap(appIcon);

}

if (drawableToBitmap != null) {

notification.setSmallIcon(R.drawable.ic_16);

notification.setLargeIcon(drawableToBitmap);

} else {

notification.setSmallIcon(context.getApplicationInfo().icon);

}

Notification notify = notification.build();

notify.flags |= FLAG_ONLY_ALERT_ONCE;

notificationManager.notify(NOTIFICATION_ID, notify);

}

/**

* 合成更新的Icon

*

* @param drawable

* @return

*/

public Bitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap = Bitmap.createBitmap(

drawable.getIntrinsicWidth(),

drawable.getIntrinsicHeight(),

drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

drawable.draw(canvas);

return bitmap;

}

/**

* 獲取App的Icon

*

* @param context

* @return

*/

public Drawable getAppIcon(Context context) {

try {

return context.getPackageManager().getApplicationIcon(context.getPackageName());

} catch (PackageManager.NameNotFoundException e) {

e.printStackTrace();

}

return null;

}

總結

以上是生活随笔為你收集整理的android icon在线更新,Android在线更新下载方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。