hello friends,
today we are going to learn about download and install android application form own serve with all version android. when we need to download an app and install an app from our own server in android nougat or API 24 grater below code is must used.
class DownloadNewVersionOfApp extends AsyncTask<String, Integer, Boolean> {
String apk_link, app_name;
public DownloadNewVersion(String apk_link, String app_name) {
this.apk_link = apk_link;
this.app_name = app_name;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(UpdateAppClass.this);
progressDialog.setCancelable(false);
progressDialog.setMessage("Downloading...");
progressDialog.setIndeterminate(true);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgress(progress[0]);
String msg = "";
if (progress[0] > 99) {
msg = "Finishing... ";
} else {
msg = "Downloading... " + progress[0] + "%";
}
progressDialog.setMessage(msg);
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progressDialog.dismiss();
if (result) {
Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Try Again later..", Toast.LENGTH_SHORT).show();
}
}
@Override
protected Boolean doInBackground(String... arg0) {
Boolean app_check = false;
try {
URL url = new URL(apk_link);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/Download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, app_name);
if (outputFile.exists()) {
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
int total_size = 1431692;
byte[] buffer = new byte[1024];
int len1 = 0;
int per = 0;
int downloaded = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
downloaded += len1;
per = (int) (downloaded * 100 / total_size);
publishProgress(per);
}
fos.close();
is.close();
checking(PATH,app_name,apk_link);
app_check = true;
} catch (Exception e) {
app_check = false;
}
return app_check;
}
}
above code download app and run the install method below.
void checking(String location,String appname,String apk_link)
{
File toInstall = new File(location, appname);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.INSTALL_PACKAGES) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.INSTALL_PACKAGES)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("message");
alertBuilder.setMessage("message");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(UpdateAppClass.this, new String[]{Manifest.permission.INSTALL_PACKAGES}, PERMISSION_REQUEST_CODE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
Log.e("", "permission denied, show dialog");
} else {
ActivityCompat.requestPermissions(UpdateAppClass.this, new String[]{Manifest.permission.INSTALL_PACKAGES}, PERMISSION_REQUEST_CODE);
}
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.REQUEST_INSTALL_PACKAGES) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.REQUEST_INSTALL_PACKAGES)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("message");
alertBuilder.setMessage("message");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(UpdateAppClass.this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, PERMISSION_REQUEST_CODE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
Log.e("", "permission denied, show dialog");
} else {
ActivityCompat.requestPermissions(UpdateAppClass.this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, PERMISSION_REQUEST_CODE);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(UpdateAppClass.this, "com.example.sys3.abtechsolution.provider",toInstall);
Intent intent_install = new Intent(Intent.ACTION_VIEW);
intent_install.setDataAndType(uri, "application/vnd.android.package-archive");
intent_install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
UpdateAppClass.this.startActivity(intent_install);
} else {
Uri apkUri = Uri.fromFile(toInstall);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
}
AndroidMainfest.xml
below code need to add in an android manifest file.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.sys3.abtechsolution.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
and one more thing needs to add in our current activity tag in android manifest.
android:exported="true"
today we are going to learn about download and install android application form own serve with all version android. when we need to download an app and install an app from our own server in android nougat or API 24 grater below code is must used.
class DownloadNewVersionOfApp extends AsyncTask<String, Integer, Boolean> {
String apk_link, app_name;
public DownloadNewVersion(String apk_link, String app_name) {
this.apk_link = apk_link;
this.app_name = app_name;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(UpdateAppClass.this);
progressDialog.setCancelable(false);
progressDialog.setMessage("Downloading...");
progressDialog.setIndeterminate(true);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgress(progress[0]);
String msg = "";
if (progress[0] > 99) {
msg = "Finishing... ";
} else {
msg = "Downloading... " + progress[0] + "%";
}
progressDialog.setMessage(msg);
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progressDialog.dismiss();
if (result) {
Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Try Again later..", Toast.LENGTH_SHORT).show();
}
}
@Override
protected Boolean doInBackground(String... arg0) {
Boolean app_check = false;
try {
URL url = new URL(apk_link);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/Download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, app_name);
if (outputFile.exists()) {
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
int total_size = 1431692;
byte[] buffer = new byte[1024];
int len1 = 0;
int per = 0;
int downloaded = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
downloaded += len1;
per = (int) (downloaded * 100 / total_size);
publishProgress(per);
}
fos.close();
is.close();
checking(PATH,app_name,apk_link);
app_check = true;
} catch (Exception e) {
app_check = false;
}
return app_check;
}
}
above code download app and run the install method below.
void checking(String location,String appname,String apk_link)
{
File toInstall = new File(location, appname);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.INSTALL_PACKAGES) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.INSTALL_PACKAGES)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("message");
alertBuilder.setMessage("message");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(UpdateAppClass.this, new String[]{Manifest.permission.INSTALL_PACKAGES}, PERMISSION_REQUEST_CODE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
Log.e("", "permission denied, show dialog");
} else {
ActivityCompat.requestPermissions(UpdateAppClass.this, new String[]{Manifest.permission.INSTALL_PACKAGES}, PERMISSION_REQUEST_CODE);
}
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.REQUEST_INSTALL_PACKAGES) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.REQUEST_INSTALL_PACKAGES)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("message");
alertBuilder.setMessage("message");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(UpdateAppClass.this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, PERMISSION_REQUEST_CODE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
Log.e("", "permission denied, show dialog");
} else {
ActivityCompat.requestPermissions(UpdateAppClass.this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, PERMISSION_REQUEST_CODE);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(UpdateAppClass.this, "com.example.sys3.abtechsolution.provider",toInstall);
Intent intent_install = new Intent(Intent.ACTION_VIEW);
intent_install.setDataAndType(uri, "application/vnd.android.package-archive");
intent_install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
UpdateAppClass.this.startActivity(intent_install);
} else {
Uri apkUri = Uri.fromFile(toInstall);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
}
AndroidMainfest.xml
below code need to add in an android manifest file.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.sys3.abtechsolution.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
and one more thing needs to add in our current activity tag in android manifest.
android:exported="true"
Comments
Post a Comment