简体   繁体   English

Android 待定意图恢复应用程序,无需启动活动

[英]Android pending intent to resume app, without starting activity

Are there any way top resume app from background to foreground without starting activity?有没有什么方法可以在不启动活动的情况下从后台到前台的顶级简历应用程序?

I have 20 activities in my app.我的应用中有 20 个活动。 When i receive notification i cant know which activity is top now.当我收到通知时,我不知道现在哪个活动是最热门的。 Can i somehow resume whole app to foreground (not calling some activity with SingleTop,ReoderToTop flags and so on but just move existing app to foreground)?我可以以某种方式将整个应用程序恢复到前台(不是使用 SingleTop、ReoderToTop 标志等调用某些活动,而是将现有应用程序移动到前台)?

Your code is far too complicated.你的代码太复杂了。 You don't need a dummy Activity .你不需要一个虚拟的Activity You should just do what Android does when it launches an app from the HOME screen.您应该只执行 Android 从主屏幕启动应用程序时所做的操作。

Use a "launch Intent" in your Notification:在您的通知中使用“启动意图”:

val intent = getPackageManager().
                    getLaunchIntentForPackage("my.package.name")

If your app is already running, this will bring the app to the foreground without creating any new components.如果您的应用程序已经在运行,这会将应用程序带到前台,而无需创建任何新组件。 If the app is NOT already running, this will start the app by launching the root Activity.如果应用程序尚未运行,这将通过启动根活动来启动应用程序。

I found one solution, it may looks weired but it works.我找到了一个解决方案,它可能看起来很奇怪,但它有效。

1) Create some dummy activity. 1)创建一些虚拟活动。 This activity will be used as a starting point for pending intent.此活动将用作未决意图的起点。

class DummyPendingIntentActivity:AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
    }
}

2) Made this logic in onCreate of dummy Activity. 2) 在 dummy Activity 的onCreate中做了这个逻辑。 In this method you can check are there any activities in stack and resume them if needed, or you can restart your app with starting your 'launcher' activity在这种方法中,您可以检查堆栈中是否有任何活动并在需要时恢复它们,或者您可以通过启动“启动器”活动来重新启动您的应用

//Here we check if there any activities in stack
override fun onCreate(savedInstanceState: Bundle?)
{
    if(isTaskRoot)
    {
        //No previous activity running will restart app
        val intent = Intent(this,YourFirstLauncherAcitivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        startActivity(intent)
    }
    else
    {
        //Has some previous activity so will finish to show it
        finish()
    }
}

3) Call this dummy start point in any pending intent and you will resume your app or restart it if needed. 3) 在任何挂起的意图中调用此虚拟起点,您将恢复您的应用程序或在需要时重新启动它。

val intent = Intent(context,DummyPendingIntentActivity::class.java)
val pendingIntent = PendingIntent.getActivity(AppClass.getApp(), 777, intent, 0)
...
setContentIntent(pendingIntent)

Recently i have faced with issue I had to resume top activity by clicking on push notification while app was in the background.最近我遇到了一个问题,我必须在应用程序处于后台时单击推送通知来恢复最热门的活动。 I found this case close to you)我发现这个案例离你很近)

I resolve this issue by saving top activity intent in onResume() method of BaseActivity , like below:我通过在BaseActivity onResume()方法中保存顶级活动意图来解决这个问题,如下所示:

Intent currentActivityIntent = new Intent(this, getClass());
currentActivityIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);
currentActivityIntent.addFlags(FLAG_ACTIVITY_SINGLE_TOP);
App.setCurrentActivityIntent(currentActivityIntent);

I get top activity intent after clicking on push.单击推送后,我获得了最高活动意图。

All you need is to get intent of last top activity with both flags : FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_SINGLE_TOP .您所需要的只是使用两个 标志获取最后一个顶级活动的意图: FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_SINGLE_TOP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM