下载更新并安装托管在私人网络服务器中的APK



实际上,我正在为我的公司开发一个私人Android应用程序,它仅适用于我们的员工。 我目前正在为下载和更新活动而苦苦挣扎。 请发布一些示例或分享链接,从中我可以获得参考。

我已经像一周前一样这样做了,这里有一个活动示例,该活动在检查更新时显示背景图像。 活动

FileProvider.Java

您需要将其放在应用程序标记内的清单中

onpostexecute

@Override

protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
bar.dismiss();
if (result) {
Toast.makeText(getApplicationContext(), "Download Done",
Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(testactivity.this);
builder.setMessage("DO you want to Install Updates?")
.setCancelable(false).setTitle("Install Update").setIcon(R.drawable.ic_action_logoutred)
.setPositiveButton("Install", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
OpenNewVersion();
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

} else {
Toast.makeText(getApplicationContext(), "Error: Try Again",
Toast.LENGTH_SHORT).show();
}
}

并打开新版本

MANSOOR RAZA:

void OpenNewVersion(( {

Intent downloadIntent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String PATH = Environment.getExternalStorageDirectory() + "/Download/";
File fileLocation = new File(PATH, "app-release.apk");
Uri apkUri = FileProvider.getUriForFile(this,  SHARED_PROVIDER_AUTHORITY, fileLocation);
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(apkUri, "application/vnd.android.package-archive");
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(downloadIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.grantUriPermission(packageName, apkUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}else {
File fileLocation = new File(this.getFilesDir(), "app-release.apk");
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(Uri.fromFile(fileLocation), "application/vnd.android.package-archive");
}
this.startActivity(downloadIntent);
Toast.makeText(getApplicationContext(), "update Done",
Toast.LENGTH_SHORT).show()

}

@Override

protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
bar.dismiss();
if (result) {
Toast.makeText(getApplicationContext(), "Download Done",
Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(testactivity.this);
builder.setMessage("DO you want to Install Updates?")
.setCancelable(false).setTitle("Install Update").setIcon(R.drawable.ic_action_logoutred)
.setPositiveButton("Install", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
OpenNewVersion();
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

} else {
Toast.makeText(getApplicationContext(), "Error: Try Again",
Toast.LENGTH_SHORT).show();
}
}

和我的打开新版本

MANSOOR RAZA:

void OpenNewVersion(( {

Intent downloadIntent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String PATH = Environment.getExternalStorageDirectory() + "/Download/";
File fileLocation = new File(PATH, "app-release.apk");
Uri apkUri = FileProvider.getUriForFile(this,  SHARED_PROVIDER_AUTHORITY, fileLocation);
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(apkUri, "application/vnd.android.package-archive");
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(downloadIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.grantUriPermission(packageName, apkUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}else {
File fileLocation = new File(this.getFilesDir(), "app-release.apk");
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(Uri.fromFile(fileLocation), "application/vnd.android.package-archive");
}
this.startActivity(downloadIntent);
Toast.makeText(getApplicationContext(), "update Done",
Toast.LENGTH_SHORT).show();
}

最新更新