仅在当前未运行时启动应用程序 [英] Starting app only if its not currently running

查看:33
本文介绍了仅在当前未运行时启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向用户发送推送通知,点击它会打开应用程序.

I am sending push notification to users which when clicking on it opens the app.

我的问题是当应用程序已经打开时,点击通知再次启动应用程序.

My problem is that when the app is already open, clicking on the notification start the app again.

如果应用程序尚未运行,我只希望它启动它.

I only want it to start the app if its not already running.

我在通知中使用了 Pending Intent:

I am using Pending Intent in the notification:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Splash.class), 0);

我看到帖子说使用:

<activity 
android:name=".Splash"
android:launchMode="singleTask"

但问题是我正在运行的应用程序正在运行其他活动,然后在应用程序启动 7 秒后完成启动,因此当应用程序运行时,启动画面不是当前活动

but the thing is that my running app is running other activity then the splash which is finished after 7 seconds from app start, so when the app is running Splash is not the current activity

推荐答案

为您的应用使用启动 Intent",如下所示:

Use a "launch Intent" for your app, like this:

PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("your.package.name");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent, 0);

将your.package.name"替换为 Android 清单中的包名称.

Replace "your.package.name" with the name of your package from the Android manifest.

此外,您应该从清单中删除特殊的 launchMode="singleTask".标准的 Android 行为可以满足您的需求.

Also, you should remove the special launchMode="singleTask" from your manifest. Standard Android behaviour will do what you want.

这篇关于仅在当前未运行时启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆