使用持久通知,以允许用户返回到运行Android应用 [英] Use a persistent notification to allow the user to return to running Android app

查看:213
本文介绍了使用持久通知,以允许用户返回到运行Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发与众多活动的应用程序。我想创建一个持久的通知(或多或少)说,「APPNAME - 返回AppName的,这将是每当我的后台服务运行present。 创建和处置的通知是没有问题的。

I am developing an app with numerous Activities. I would like to create a persistent notification that (more or less) says, "AppName - Return to AppName" that will be present whenever my background services are running. Creating and disposing of the notification was no problem.

现在,用户可以在任意多个屏幕/活动,离开该应用程序,然后想通过通知重新进入应用程序。 的问题是,通知必须有一个意图,这将启动一个 predetermined活动。我希望通知在重新进入该应用程序的什么活动是在历史的栈顶

Now, the user could be on any of several screens/Activities, leave the application, then want to re-enter the app via the notification. The problem is, the notification must have an intent, which launches a predetermined Activity. I want the notification to re-enter the app in whatever Activity is at the top of the history stack.

在一个丑陋的解决办法我第一次尝试做一个活动(姑且称之为returnFromNotify),其唯一的工作就是完成本身在它的的onCreate。该通知将在应用历史,那么这将立即删除本身的范围打开returnFromNotify,发送用户返回到previous历史状态中的应用程序栈。这似乎工作......除非用户已经使用回完全背出该应用程序的。然后,当他们打的通知,returnFromNotify负载,然后完成,送他们回了到主屏幕(因为有在历史堆栈的应用程序不活动)。

My first attempt at an ugly workaround was to make an activity (let's call it "returnFromNotify") whose only job was to "finish" itself in it's "onCreate". The notification would open "returnFromNotify" in the scope of the applications history, which would then immediately remove itself, sending the user back to the previous history state in the application stack. This seems to work... unless the user has used "back" to completely back out of the app. Then, when they hit the notification, "returnFromNotify" loads, then finishes, sending them back out to the home screen (as there are no activities in the history stack for the app).

我认为是试图检测是否有在returnFromNotify前的历史堆栈任何东西,如果没有,火起来我的主要活动。我似乎无法找到一个方法来做到这一点,无论是。

I considered trying to detect if there was anything in the history stack before "returnFromNotify", and if not, fire up my main Activity. I can't seem to find a way to do this, either.

任何输入或建议为Java / Android的新手?仅供参考,我的主要历史是基于脚本的语言。

Any input or suggestions for a Java/Android novice? FYI, My primary history is with script-based languages.

推荐答案

我喜欢你原来创建returnFromNotify活动比你提出的解决办法的思路,因为它的的可能,如果检测ResumeActivity位于堆栈的底部(和因此在堆栈中的唯一活性)。

I like your original idea of creating a "returnFromNotify" activity better than your proposed workaround, as it is possible to detect if the ResumeActivity is at the bottom of the stack (and therefore the only activity in the stack).

下面是你如何能做到这一点:

Here's how you can do it:

添加您的ResumeActivity的清单,并指定 noHistory 属性:

Add your ResumeActivity to the manifest and specify the noHistory attribute:

<activity android:name=".ResumeActivity" android:noHistory="true" />

指定noHistory将确保该活动将不会留在堆栈中尽快完成。这样,你知道,只有一个当前正在运行的ResumeActivity的实例会在堆栈中显示出来。

Specifying noHistory will make sure this Activity won't stay in the stack as soon as it finishes. This way you know that only a currently running instance of the ResumeActivity will show up in the stack.

为了检查应用程序堆栈,你也有索要的GET_TASKS权限:

In order to check the application stack, you'll also have to ask for the GET_TASKS permission:

<uses-permission android:name="android.permission.GET_TASKS" />

现在,您可以使用<一个href="http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks%28int%29">ActivityManager::getRunningTasks()以确定是否ResumeActivity是堆栈中的唯一活性

Now you can use ActivityManager::getRunningTasks() to determine if ResumeActivity is the only activity in the stack:

public class ResumeActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(isOnlyActivityInStack()) { //check the application stack
            //This activity is the only activity in the application stack, so we need to launch the main activity
            Intent main = new Intent(this, MainActivity.class);
            main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(main);
        } else {
            //Return the user to the last activity they had open
            this.finish();
        }
    }

    /**
     * Checks the currently running tasks. If this activity is the base activity, we know it's the only activity in the stack
     *
     * @return  boolean  This activity is the only activity in the stack?
     **/
    private boolean isOnlyActivityInStack() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        boolean onlyActivityInStack = false;

        for(RunningTaskInfo tasks : manager.getRunningTasks(Integer.MAX_VALUE)) {
            if(tasks.baseActivity.getPackageName().equals(this.getPackageName())) { //find this package's application stack
                if(tasks.baseActivity.getClassName().equals(this.getClass().getName())) {
                    //If the ResumeActivity is the base activity, we know that it is the only activity in the stack
                    onlyActivityInStack = true;
                    break;
                }
            }
        }

        return onlyActivityInStack;
    }

}

我知道你在2年前问这个问题,但我提供的情况下,任何人都运行在这个特定的情况下这样的回答(像我一样)。我认为你是在正确的轨道与您最初赶制的解决方案。

I know you asked this question over 2 years ago, but I'm providing this answer in case anyone else runs in to this particular situation (as I did). I think you were on the right track with the solution you were originally working towards.

这篇关于使用持久通知,以允许用户返回到运行Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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