发布:2021/10/26 10:04:08作者:管理员 来源:本站 浏览次数:1071
//这里采用了asyncTask 异步任务
}
//activity调用
public class SystemInfoActivity : Activity
{
private Context context;
private bool isHasNew = false;
private RelativeLayout checkNewBtn;//检查更新
private CustomProgressDialog barDialog;
private AlertDialog.Builder builder;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layo ut.SystemInfo);
//。。。。。获取页面布局元素
context = this;
checkNewBtn = FindViewById<RelativeLayout>(Resource.Id.checkNewBtn);
checkNewBtn.Click -= CheckNewBtn_Click;
checkNewBtn.Click += CheckNewBtn_Click;
//先查询服务器上面最新的app版本
CheckNew();
}
//请求api,检测最新版本的信息
private void CheckNew()
{
//请求接口的代码省略。。。。
//返回的结果
if (result.Content.AppVersion > AppInfo.VersionCode)
{
isHasNew = true;
}
}
private void CheckNewBtn_Click(object sender, EventArgs e)
{
if (isHasNew)
{
OpenUpDialog();
}
else
{
MyDialog.showMessage(context, "当前已是最新版本无需更新");
}
}
//
private void OpenUpDialog()
{
builder = new AlertDialog.Builder(mContext);
builder.SetTitle("有新版本啦").SetMessage("我更新再更新").SetNeutralButton("暂不更新",
(s,e)=> { }).setPositiveButton("立即更新",(s,e)=>{
//先检测一遍是否有访问权限
if (CheckFilePermission())
{
DownloadApk(content.AppVersionCode, content.DownloadURL);
}
else
{
MyDialog.showMessage(context, "需要获得访问存储卡的权限,您可以在设置->应用->权限
进行设置!");
}
}).Show();
}
//查看是否打开了使用存储的读写权限
private bool CheckFilePermission()
{
var result = false;
// 需要在Android里面找到你要开的权限
string readPermission = Manifest.Permission.ReadExternalStorage;
string writerPermission = Manifest.Permission.WriteExternalStorage;
if (PermissionChecker.CheckSelfPermission(context, readPermission) ==
PermissionChecker.PermissionGranted &&
PermissionChecker.CheckSelfPermission(context, writerPermission) ==
PermissionChecker.PermissionGranted)
{
result = true;
}
return result;
}
//下载
private void DownloadApk(string versionName, string url)
{
//自定义的进度条弹窗
barDialog = new CustomProgressDialog(context);
barDialog.show(0, "0 / 0");
DownLoadTask loadtask = new DownLoadTask("appName", versionName, ".apk", url);
loadtask.Execute();
loadtask.OnUpdateProgress += Loadtask_OnUpdateProgress;
loadtask.Done += Loadtask_Done;
}
//更新进度条的值
private void Loadtask_OnUpdateProgress(int progress, decimal percent, long currentLength, long maxLength)
{
var stringCurrentLen = StringHelper.ToByteString(currentLength);
var stringmaxLen = StringHelper.ToByteString(maxLength);
barDialog.update(progress, stringCurrentLen + " / " + stringmaxLen);
}
//下载完成的时候
private void Loadtask_Done(bool isLoaded, string savePath)
{
barDialog.dismiss();
if (isLoaded)
{
InstallApk(savePath);
}
else
{
MyDialog.showToast(context, "下载失败", 1);
}
}
//开始安装
private void InstallApk(string savePath)
{
if (System.IO.File.Exists(savePath))
{
try
{
Intent i = new Intent(Intent.ActionView);
i.SetDataAndType(Uri.FromFile(new File(savePath)), "application/vnd.android.package-archive");
i.SetFlags(ActivityFlags.NewTask);
tartActivity(i);
}
catch (Exception)
{
MyDialog.showToast(context, "安装失败", 1);
}
}
else
{
MyDialog.showToast(context, "未找到安装包的文件路径", 1);
}
}
}
//自定义进度条弹窗布局
<?xml version="1.0" encoding="utf-8"?>
<!--更新新版版本下载时的进度条-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:paddingLeft="15dp"
android:text="正在下载" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:minHeight="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:progress="0" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="15dp"
android:text="zz" />
</LinearLayout>
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4