检查是否安装了应用程序

编程入门 行业动态 更新时间:2024-10-25 18:22:30
本文介绍了检查是否安装了应用程序 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从 Google Play 安装应用程序.我可以理解,在打开 Google Play 商店 URL 时,它会打开 Google Play,当我按下后退按钮时,活动会继续.

I'm trying to install apps from Google Play. I can understand that on opening the Google Play store URL, it opens the Google Play and when I press the back button, the activity resumes.

Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(appURL)); marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); startActivity(marketIntent);

当我回到活动时,我尝试调用此 onResume() 来检查应用程序是否已安装,但收到错误:

When I went back to the activity, I tried calling this onResume() to check if the app is installed, but I receive an error:

@Override protected void onResume() { super.onResume(); boolean installed = false; while (!installed) { installed = appInstalledOrNot(APPPACKAGE); if (installed) { Toast.makeText(this, "App installed", Toast.LENGTH_SHORT).show(); } } } private boolean appInstalledOrNot(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 ; }

错误如下:

E/AndroidRuntime(796): java.lang.RuntimeException: 无法启动活动组件信息{com.example.appinstaller/com.example.appinstaller.MainActivity}:android.content.ActivityNotFoundException:未找到处理 Intent { act=android.intent.action.VIEW 的 Activitydat=market://details?id=com.package.name flg=0x40080000 }

E/AndroidRuntime(796): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appinstaller/com.example.appinstaller.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=com.package.name flg=0x40080000 }

我猜活动是 onPause().有没有更好的方法来实现它?我正在尝试检查应用是否已完成安装.

I guess the activity is onPause(). Is there a better way to implement it? I'm trying to check if the app has finished installing.

推荐答案

试试这个:

private boolean isPackageInstalled(String packageName, PackageManager packageManager) { try { packageManager.getPackageInfo(packageName, 0); return true; } catch (PackageManager.NameNotFoundException e) { return false; } }

它会尝试获取有关您传入的名称的包的信息.如果失败,如果抛出 NameNotFoundException,则意味着没有安装具有该名称的包,因此我们返回 错误.

It attempts to fetch information about the package whose name you passed in. Failing that, if a NameNotFoundException was thrown, it means that no package with that name is installed, so we return false.

请注意,我们传入的是 PackageManager 而不是 Context,因此该方法使用起来稍微灵活一些,并且不违反 得墨忒耳法则.只要您有一个 PackageManager 实例,您就可以在不访问 Context 实例的情况下使用该方法.

Note that we pass in a PackageManager instead of a Context, so that the method is slightly more flexibly usable and doesn't violate the law of Demeter. You can use the method without access to a Context instance, as long as you have a PackageManager instance.

像这样使用它:

public void someMethod() { // ... PackageManager pm = context.getPackageManager(); boolean isInstalled = isPackageInstalled("com.somepackage.name", pm); // ... }

注意:从 Android 11 (API 30) 开始,您可能需要在清单中声明 <queries>,具体取决于您要查找的包.查看文档了解更多信息.

Note: From Android 11 (API 30), you might need to declare <queries> in your manifest, depending on what package you're looking for. Check out the docs for more info.

更多推荐

检查是否安装了应用程序

本文发布于:2023-10-28 08:39:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   安装了

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!