Android的 - 有结果编程安装应用程序 [英] Android - Install Application programmatically with result
问题描述
我是新来的Android,我有其中包括在服务上的一些点服务需要安装一个新的apk(基本上是一个自动更新)的应用程序,目前安装像在$ C $完成ç娄不允许知道,当它完成或得到一个结果了它,我需要知道,为了执行驱动该结果的其他操作。
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 installation is done like in the code bellow which does not allow to know when it finishes or get a result out of it and I need to know that in order to perform other actions driven on that result.
File mFile = new File(Uri.parse(downloadedPackageUriString).getPath());
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(promptInstall);
我想知道是否有这样做的一种方式?
谢谢你在前进。
I would like to know if there is a way of doing that? Thank you in advance.
推荐答案
的逻辑可能是:
- 安装脚本
- 等待5秒钟
- 运行
appInstalledOrNot
功能 - 如果没有安装,那么在1秒内再试
下面可能是code:
public void installApp(){
File mFile = new File(Uri.parse(downloadedPackageUriString).getPath());
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(promptInstall);
Thread t = new Thread(){
public void run(){
boolean installed = checkInstalled("vnd.android.package-archive");
while(!installed)
{
System.out.println("Not Installed Yet..Waiting 1 Second");
t.sleep(1000);
}
//Now it has been installed
if(installed) {
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = 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();
}
private boolean checkInstalled(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed ;
}
根据
code:<一href=\"http://stackoverflow.com/questions/11392183/how-to-check-if-the-application-is-installed-or-not-in-android-programmatically\">How检查应用程序是否安装或没有在android系统编程
这篇关于Android的 - 有结果编程安装应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!