如果要检查服务器上某处的应用程序,则必须每 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"/>
祝你好运。