This is a small code sniped about installing an APK from Asset folder in mobile .
Let me know if you face any issues and do comment for feedback.
package com.android.parentapplication;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
public class ParentActivity extends Activity {
// Package name of APK in asset file
private String mInitialApplication = "com.android.initialapplication";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parent);
if (appInstalledOrNot(mInitialApplication)) {
} else {
installAPK();
}
}
/*
* Installs application from Asset folder
*/
public void installAPK() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("InitialApplication.apk");
out = new FileOutputStream("/sdcard/InitialApplication.apk");
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/InitialApplication.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
} catch(Exception e) {
e.printStackTrace();
}
}
/*
* return
* appInstalled = true
* appUninstalled = false
*/
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
/*
* code to derive VersionName and VersionCode
*/
/* mVersionCode = pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES).versionCode;
mVersionName = pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES).versionName;*/
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
/*
* Verify version code
*/
public static boolean checkVersionCode(int mVersionCode, int toInstallVersionCode)
{
return(mVersionCode > toInstallVersionCode);
}
}
Let me know if you face any issues and do comment for feedback.
package com.android.parentapplication;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
public class ParentActivity extends Activity {
// Package name of APK in asset file
private String mInitialApplication = "com.android.initialapplication";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parent);
if (appInstalledOrNot(mInitialApplication)) {
} else {
installAPK();
}
}
/*
* Installs application from Asset folder
*/
public void installAPK() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("InitialApplication.apk");
out = new FileOutputStream("/sdcard/InitialApplication.apk");
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/InitialApplication.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
} catch(Exception e) {
e.printStackTrace();
}
}
/*
* return
* appInstalled = true
* appUninstalled = false
*/
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
/*
* code to derive VersionName and VersionCode
*/
/* mVersionCode = pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES).versionCode;
mVersionName = pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES).versionName;*/
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
/*
* Verify version code
*/
public static boolean checkVersionCode(int mVersionCode, int toInstallVersionCode)
{
return(mVersionCode > toInstallVersionCode);
}
}
No comments:
Post a Comment