【问题标题】:Android Notification Intent called based on conditionAndroid Notification Intent 根据条件调用
【发布时间】:2016-02-25 06:57:58
【问题描述】:
我的应用添加了一个通知,点击该通知后,应将用户重定向到 Google Play 商店。该通知基本上是指导用户从谷歌商店下载应用程序。但是有一个问题;仅当未安装应用程序时,通知才应重定向用户,即:单击通知时,检查是否安装了应用程序;如果没有,请将用户带到 Google Play 商店。我该如何实现?
我应该添加一个虚拟活动来实现这一点吗?
【问题讨论】:
标签:
android
android-intent
notifications
【解决方案1】:
如果您没有在为通知指定的待处理意图中指定任何活动类,请添加一个,并在该活动类中,检查应用程序是否使用您的 google play store 应用程序 url 中指定的包名称安装,
- 如果您愿意或完成,请保持您的活动打开。
- 如果未安装,请在 Playstore 应用中打开网址后完成您的活动。
另一种处理方法是使用自定义广播接收器,使用PendingIntent.getBroadcast。因此,与您在活动中实现的检查相同,您可以在 BroadcastReceiver 类中实现它并同样处理该操作。
【解决方案2】:
试试这个,检查你的包名
boolean check_app_install = check_appInstalled("com.Example.package");
if(check_app_install) {
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.Example.package");
startActivity(LaunchIntent);
System.out.println("App is already installed on phone");
} else {
System.out.println("App is not installed on phone");
}
private boolean check_appInstalled(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
【解决方案3】:
是的,您应该添加虚拟活动。
在该活动中,将此代码添加到 onCreate 以处理应用程序是否在 android 手机中可用:
PackageManager pm = getPackageManager();
String packageName = "INSERT PACKAGE NAME OF APP";
Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
if(launchIntent != null){
startActivity(launchIntent);
}
else{
String googlePlayStoreLink = "INSERT URL OF PLAYSTORE LINK";
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(googlePlayStoreLink)));
}