Last active
March 29, 2019 06:50
-
-
Save miao1007/d7cf00ef6878e964e368 to your computer and use it in GitHub Desktop.
Fir.im check for update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by leon on 2/16/15. | |
* Download update apk with SystemService. | |
* require: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | |
*/ | |
public class DownloadUtils { | |
public static String MINETYPE_APPLCATION = "application/vnd.android.package-archive"; | |
public static long DownloadApkWithProgress(Context context, String url) { | |
DownloadManager downloadManager = | |
(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); | |
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); | |
request.setDestinationInExternalPublicDir("/appupdate", "update.apk"); | |
request.setTitle("Updating" + context.getPackageName()); | |
request.setMimeType(MINETYPE_APPLCATION); | |
long downloadId = 0; | |
//fix crash on SecurityException. | |
try { | |
downloadId = downloadManager.enqueue(request); | |
//this may cause memory leak ,but never it will occur because the app will be killed before install | |
context.registerReceiver(new DownloadCompleteReceiver(), | |
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); | |
} catch (SecurityException e) { | |
Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); | |
} | |
return downloadId; | |
} | |
/** | |
* Created by leon on 2/17/15. | |
* Called when download update apk complete. | |
*/ | |
public static class DownloadCompleteReceiver extends BroadcastReceiver { | |
public static String TAG = LogUtils.makeLogTag(DownloadManager.class); | |
@Override public void onReceive(Context context, Intent intent) { | |
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { | |
long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); | |
Log.d(TAG, "onReceive" + completeDownloadId); | |
DownloadManager downloadManager = | |
(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); | |
Intent installIntent = new Intent(Intent.ACTION_VIEW); | |
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
installIntent.setDataAndType(downloadManager.getUriForDownloadedFile(completeDownloadId), | |
MINETYPE_APPLCATION); | |
try { | |
context.startActivity(installIntent); | |
} catch (ActivityNotFoundException e) { | |
Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); | |
} finally { | |
context.unregisterReceiver(this); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by leon on 15/5/16. | |
* Check for update via Fir.im or wandoujia.You need to use a Theme.AppCompat theme (or descendant) | |
* with this activity | |
*/ | |
public class FIRUtils { | |
public final static void checkForUpdate(Context context, boolean isShowToast) { | |
if (BuildConfig.DEBUG){ | |
FIR.checkForUpdateInFIR(context, FIR.GENERAL_KEY, callback(context, isShowToast)); | |
} else { | |
FIR.checkForUpdateInAppStore(context, callback(context, isShowToast)); | |
} | |
} | |
static VersionCheckCallback callback(final Context context, final boolean isShowToast) { | |
return new VersionCheckCallback() { | |
@Override public void onSuccess(final AppVersion appVersion, boolean b) { | |
if (appVersion.getVersionCode() == BuildConfig.VERSION_CODE) { | |
if (isShowToast) Toast.makeText(context, "你已经是最新版本", Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
try { | |
new AlertDialog.Builder(context).setTitle("最新版本 " + appVersion.getVersionName()) | |
.setMessage(appVersion.getChangeLog()) | |
.setPositiveButton("立即更新", new DialogInterface.OnClickListener() { | |
@Override public void onClick(DialogInterface dialogInterface, int i) { | |
//when download complete, broadcast will be sent to receiver | |
DownloadUtils.DownloadApkWithProgress(context.getApplicationContext(), | |
appVersion.getUpdateUrl()); | |
} | |
}) | |
.create() | |
.show(); | |
//IllegalStateException when using appcompAlertDialog or NullException when windows leak | |
} catch (Exception e) { | |
GlobalContext.showToast(e.getMessage()); | |
} | |
} | |
@Override public void onFail(String s, int i) { | |
if (isShowToast) Toast.makeText(context, "检查更新失败", Toast.LENGTH_SHORT).show(); | |
} | |
@Override public void onError(Exception e) { | |
if (isShowToast) Toast.makeText(context, "检查更新失败", Toast.LENGTH_SHORT).show(); | |
} | |
@Override public void onStart() { | |
if (isShowToast) Toast.makeText(context, "开始检查更新", Toast.LENGTH_SHORT).show(); | |
} | |
@Override public void onFinish() { | |
} | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//使用Fir.im进行在线更新的一个封装(对话框显示,后台下载,广播接受收后自动安装) | |
@OnClick(R.id.about_version) void about_version() { | |
FIRUtils.checkForUpdate(getActivity()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment