我是Android新手,我有一个应用程序,它包含一个服务,在某些方面,服务需要安装一个新的.apk(基本上是一个自动更新),目前安装完成就像在下面的代码中一样不允许知道它何时完成或从中得到结果我需要知道为了执行其他驱动该结果的行为。
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);
我想知道是否有办法做到这一点? 提前谢谢。
答案 0 :(得分:0)
逻辑可能是:
appInstalledOrNot
功能这可能是代码:
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 ;
}
代码基于:How to check programmatically if an application is installed or not in Android?