امکان بروزرسانی برنامه
با سلام
میخوام به برنامم امکان بروزرسانی رو بدم که هر وقت نسخه جدیدی رو منتشر کردم اون دست از کاربرانی که نسخه قبلی رو نصب دارن بتونن بروزرسانی کنن و اطلاعتشون هم پاک نشه..
یه نکته من نمیخوام برنامم رو به صورت عمومی منتشر کنم بلکه این برنامه مختص یه گروه خاصه.
حالا میخوام در برنامم این امکان باشه که به صورت خودکار بروزرسانی بشه.
توی انجمن گشتم ولی چیزی دستگیرم شد!!!!!
کسی هست راهنماییم کنه و توضیح بده چکار باید انجام بدم
ممنون میشم
من یه کد پیدا کردم
کسی میتونه یه توضیح مختصری بده!؟
public class UpdateApp extends AsyncTask<String,Void,Void>{
private Context context;
public void setContext(Context contextf){
context = contextf;
}
@Override
protected Void doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = "/mnt/sdcard/Download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "update.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/Download/update.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
context.startActivity(intent);
} catch (Exception e) {
Log.e("UpdateAPP", "Update error! " + e.getMessage());
}
return null;
}}
اینم کدی که درون اکتیویتی اصلی کلاس بالایی رو صدا میزنیم
UpdateApp atualizaApp = new UpdateApp();
atualizaApp.setContext(getApplicationContext());
atualizaApp.execute("http://serverurl/appfile.apk");
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("file:///path/to/your.apk"),
"application/vnd.android.package-archive");
startActivity(promptInstall);
کسی هست توضیح بده؟
ممنون میشم
چیزی که من برای برنامه های خودم استفاده میکنم
سرور به این صورت میتونه باشه :
<?
header('Content-Type: text/html; charset=utf-8');
//status=0 : 1 = download , 2 = diactive
$array['status'] = "0";
$array['vcod'] = "1";
$array['url'] = "http://file.apk";
$array['descmain'] = "لطفا آخرین نسخه برنامه را نصب کنید";
$array['descdl'] = "دانلود";
echo json_encode($array);
?>
سمت اندروید هم به این شکل :
try {
JSONObject jsonObject = new JSONObject(doc.body().text());
status = jsonObject.getString("status");
vCod = jsonObject.getInt("vcod");
url = jsonObject.getString("url");
descMain = jsonObject.getString("descmain");
descDl = jsonObject.getString("descdl");
}
catch (JSONException e) {
e.printStackTrace();
}
if (status.equals("0")) {
lunckApp(1000);
} else if (status.equals("1") && appVCode < vCod) {
newApDialog();
} else if (status.equals("2")) {
newApDialog();
} else {
lunckApp(1000);
}
مربوط به دیالوگ آپدیت :
layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (status.equals("1")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.browser");
try {
getApplicationContext().startActivity(intent);
}
catch (ActivityNotFoundException ex) {
intent.setPackage(null);
getApplicationContext().startActivity(intent);
}
} else if (status.equals("2")) {
System.exit(0);
}
}
});
و یا میتونید این کار رو با استفاده از دانلود منجر هم انجام بدید :
private void startDownload(String path, String title) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path));
request.setDescription("در حال دانلود" + title);
request.setTitle(title);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, title);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) G.context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .