Android - Install Application Programmatically With Result
I am new to Android and I have an application which consists in a service and on some point the service needs to install a new .apk (basically an auto-update), currently the instal
Solution 1:
The Logic Could Be:
- Install Script
- Wait 5 seconds
- Run the
appInstalledOrNot
function - If not installed, then try again in 1 second
Here could be the code:
publicvoidinstallApp(){
FilemFile=newFile(Uri.parse(downloadedPackageUriString).getPath());
IntentpromptInstall=newIntent(Intent.ACTION_VIEW)
.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(promptInstall);
Threadt=newThread(){
publicvoidrun(){
booleaninstalled= checkInstalled("vnd.android.package-archive");
while(!installed)
{
System.out.println("Not Installed Yet..Waiting 1 Second");
t.sleep(1000);
}
//Now it has been installedif(installed) {
//This intent will help you to launch if the package is already installedIntentLaunchIntent= getPackageManager()
.getLaunchIntentForPackage("com.Ch.Example.pack");
startActivity(LaunchIntent);
System.out.println("App already installed on your phone");
}
else {
System.out.println("App is not installed on your phone");
}
}
t.start();
}
privatebooleancheckInstalled(String uri) {
PackageManagerpm= getPackageManager();
booleanapp_installed=false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed ;
}
Code based on: How to check programmatically if an application is installed or not in Android?
Post a Comment for "Android - Install Application Programmatically With Result"