如果你想檢查你的服務器上的某個地方的應用程序,你必須每24小時檢查一次更新,如果有任何更新可用,那麼它將導航到異步任務其中,更新後的版本版本將得到安裝
public void checkforUpdate() {
/* Get Last Update Time from Preferences */
SharedPreferences prefs = getPreferences(0);
lastUpdateTime = prefs.getLong("lastUpdateTime", 0);
if ((lastUpdateTime + CommonString.timeCheckDuration) < System.currentTimeMillis() && System.currentTimeMillis()>lastUpdateTime) {
// Asynch task
new VersionCheckTask().execute();
}
else{
// do nothing
}
}
現在將導航到:
private class VersionCheckTask extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
progressDialog = new ProgressDialog(Login.this, android.R.style.Theme_Holo_Light_Dialog);
//progressDialog.setTitle("AppName");
progressDialog.setMessage("Checking for updates...");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected Void doInBackground(Void... params) {
/**
* Simulates a background job.
*/
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
HashMap<String, String> map = new HashMap<String, String>();
map.put("build",CommonString.buildVersion);
map.put("en", CommonString.en);
responce = CommonFunction.PostRequest("updateCheck", map);
return null;
}
@Override
protected void onPostExecute(Void result) {
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
if(!CommonFunction.isNetworkAvailable()){
Toast.makeText(ClaimColonyApplication.getAppContext(), CommonString.NO_NETWORK, Toast.LENGTH_SHORT).show();
return;
}
ParseUpdateResponse(responce);
if(rCodeUpdate == 100 && ApkLink.length() >0){
new AlertDialog.Builder(Login.this,android.R.style.Theme_Holo_Light_Dialog)
.setIcon(R.drawable.ic_launcher)
.setTitle("Update Available")
.setMessage(""+UpdateMessage)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//User clicked OK so do some stuff
new VersionCheckTaskDialog().execute();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//User clicked Cancel
finish();
}
})
.show();
}else{
if(rCodeUpdate == 100){
lastUpdateTime = System.currentTimeMillis();
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putLong("lastUpdateTime", lastUpdateTime);
editor.commit();
}
}
super.onPostExecute(result);
}
}
private class VersionCheckTaskDialog extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialogUpdate;
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
progressDialogUpdate = new ProgressDialog(Login.this, android.R.style.Theme_Holo_Light_Dialog);
//progressDialog.setTitle("AppName");
progressDialogUpdate.setMessage("Fetching updates...");
progressDialogUpdate.setCancelable(false);
progressDialogUpdate.setIndeterminate(true);
progressDialogUpdate.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected Void doInBackground(Void... params) {
/**
* Simulates a background job.
*/
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "pdf");
folder.mkdir();
File file = new File(folder, "AppName."+"apk");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
/**
* replace url to ApkLink
*/
//DownloadFile(ApkLink, file);
DownloadFile("URL", file);
return null;
}
@Override
protected void onPostExecute(Void result) {
if (progressDialogUpdate != null && progressDialogUpdate.isShowing())
progressDialogUpdate.dismiss();
if(!CommonFunction.isNetworkAvailable()){
Toast.makeText(ClaimColonyApplication.getAppContext(), CommonString.NO_NETWORK, Toast.LENGTH_SHORT).show();
return;
}
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/pdf/" + "AppName.apk")), "application/vnd.android.package-archive");
startActivity(intent);
lastUpdateTime = System.currentTimeMillis();
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putLong("lastUpdateTime", lastUpdateTime);
editor.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Exception in start intent for launch app-------: "+e.toString());
e.printStackTrace();
}
super.onPostExecute(result);
}
}
我在24小時一次檢查更新,如果有可用,那麼它會顯示彈出任何更新給你升級您的應用程序,否則會將您上次的檢查時間保存在首選項中 現在,這將允許您更新和安裝您的應用程序,這將檢查24小時後的下一次更新,您可能需要根據條件來檢查更新。請更改.apk文件和URL的名稱。
您將需要以下權限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
好運。
感謝您的代碼。這可能不是我真正想問的。我的意思是...查詢應用程序更新是非常簡單的任務。 VersionCheckTaskDialog的onPostExecute是最有趣的部分。我需要做的是觸發**未經監督的安裝。你的代碼*應該*要求用戶同意,但由於我沒有測試過它,因爲它應該是一個「紙上」概念證明我想問你是否從系統應用程序觸發ACTION_VIEW意圖可以觸發未被監督的安裝。謝謝 –
我也這樣做,但我在這裏迫使用戶更新他的版本,直到他不會更新他將無法登錄應用程序,當有任何更新可用在我的服務器上時,我會得到我的一些網址新建和我顯示彈出,如果用戶允許更新將自動下載應用程序apk在設備中,並要求安裝,安裝後用戶將不得不再次啓動應用程序, – Puneet
+1是一種可行的方式來執行最新版本規則 –