Mono For Android 这个对很多人来说似乎很陌生,因为在国内还不是很流行,所以国内资料也很少关于mono for Android,甚至国外也很少资料。
好了言归正传,直奔主题
最近为了完成项目最后的一个大功能,那就是如果软件有更新怎么办?那肯定是要做到自动提示更新了。
如果是Android的话这个网上资料一堆,直接copy,但是mono for android压根就没有,所以我只能参考java代码用C#写了。下面是我的资料:
1.在AndroidManifest.xml文件中加入这个<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" package="com.myapp" android:versionName="3.0.0.0">
每次只需要修改versionName就好。(package 的名字不需要修改,否则会出错,我试过了)
2.下面是定义一个Global.cs文件,用于读取当前app的版本号。
public class Global { //Vision information public static string getVersion(Context context) { string version = ""; try { version = context.PackageManager.GetPackageInfo("com.myapp", 0).VersionName; } catch (Exception exception) { MessageBox.Show(context,"CONFIRM",exception.Message);//这是本人写的一个messagebox } return version; } } 3.和服务器版本的对比。
这个随意你怎么对比了,我是直接把版本号发到服务器上,服务器有存储当前最新版本的版本号,如果相等就不管,否则就提示更新。
4.下载文件,更新app(这个我用了三天才搞定,断断续续)。
public class UpdateManager { private Context mContext; //返回的安装包url private string apkUrl = "http://localhost/msoft/mars.apk"; private Dialog downloadDialog; /* 下载包安装路径 */ private static string savePath = "/sdcard/Download/"; private static string saveFileName = savePath + "mars.apk"; /* 进度条与通知ui刷新的handler和msg常量 */ private ProgressBar mProgress; private int progress; private bool interceptFlag = false; public UpdateManager(Context context) { this.mContext = context; } public void showDownloadDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.SetTitle("Upgrade sofware"); LayoutInflater inflater = LayoutInflater.From(mContext); View v = inflater.Inflate(Resource.Layout.progress,null); mProgress = (ProgressBar)v.FindViewById(Resource.Id.progress); builder.SetView(v); builder.SetNegativeButton("Cancel",new MyClickListener()); interceptFlag = MyClickListener.interceptFlag; downloadDialog = builder.Create(); downloadDialog.Show(); Task.Factory.StartNew(() => downloadApk()); } private void downloadApk() { try { URL url = new URL(apkUrl); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.SetRequestProperty("User-Agent","PacificHttpClient"); conn.Connect(); conn.ConnectTimeout = 10000; conn.ReadTimeout = 20000; int length = conn.ContentLength; System.IO.Stream stream = conn.InputStream; File file = new File(savePath); if (!file.Exists()) { file.Mkdir(); } string apkFile = saveFileName; File ApkFile = new File(apkFile); FileOutputStream fos = new FileOutputStream(ApkFile); int count = 0; int intBufferSize = 16384 * 8;//这个Size随意你喜欢。 byte[] bytBuffer = new byte[intBufferSize]; interceptFlag = false; int numread = 0; while ((numread = stream.Read(bytBuffer, 0, intBufferSize)) > 0) { count += numread; progress = (int)(((float)count / length) * 100); mProgress.Progress = progress; fos.Write(bytBuffer,0,numread); } if (numread == 0) Task.Factory.StartNew(()=> installApk()); fos.Close(); stream.Close(); } catch (MalformedURLException exception) { exception.PrintStackTrace(); } } private void installApk() { downloadDialog.Dispose(); File apkfile = new File(saveFileName); if (!apkfile.Exists()) { return; } var targetUri = Android.Net.Uri.FromFile(apkfile); Intent i = new Intent(Intent.ActionView); i.SetDataAndType(targetUri, "application/vnd.android.package-archive"); mContext.StartActivity(i); } } class MyClickListener : Java.Lang.Object, IDialogInterfaceOnClickListener { public static bool interceptFlag = false; public void OnClick(IDialogInterface dialog,int which) { dialog.Dismiss(); interceptFlag = true; } }
好了,到此为止,更新app功能完成了。我觉得比java的简单了。希望对你有所帮助吧,不明白的可以留言哦!
谢谢浏览,转载请注明出处!