Mono For Android 這個對不少人來講彷佛很陌生,由於在國內還不是很流行,因此國內資料也不多關於mono for Android,甚至國外也不多資料。 java
好了言歸正傳,直奔主題 android
最近爲了完成項目最後的一個大功能,那就是若是軟件有更新怎麼辦?那確定是要作到自動提示更新了。 服務器
若是是Android的話這個網上資料一堆,直接copy,可是mono for android壓根就沒有,因此我只能參考java代碼用C#寫了。下面是個人資料:
1.在AndroidManifest.xml文件中加入這個 app
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" package="com.myapp" android:versionName="3.0.0.0"> ui
每次只須要修改versionName就好。(package 的名字不須要修改,不然會出錯,我試過了) this
2.下面是定義一個Global.cs文件,用於讀取當前app的版本號。 url
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.和服務器版本的對比。
spa
這個隨意你怎麼對比了,我是直接把版本號發到服務器上,服務器有存儲當前最新版本的版本號,若是相等就無論,不然就提示更新。 orm
4.下載文件,更新app(這個我用了三天才搞定,斷斷續續)。 xml
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的簡單了。但願對你有所幫助吧,不明白的能夠留言哦!
謝謝瀏覽,轉載請註明出處!