1

所有这些都与我自己的应用程序有关。

我有两个案例。首先,我的活动是最重要的,一切正常。我的服务广播信息,我的活动更新它的 GUI。

我的问题是,如果我的活动还没有出现,我不知道如何把它带到最前面。理想情况下,它前面的所有活动都将关闭,并将这一活动带到最前面。我该怎么做呢?

broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //what code do I put here to bring this activity to the front and close all other activities on top of it?
        }
    };

这是我在下面的帖子的帮助下解决问题的方法。

            Intent i = new Intent();
            i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.setClass(MyClass.this, MyClass.class);
            startActivity(i);
4

1 回答 1

1

First of all there isn't a direct command to bring an activity to front such as myActivity.bringToFront. Instead you will have to let android do it for you through the intents mechanism.

So if you want to bring an activity to front you will have to call startActivity and pass an intent with the correct flags. For example the flag FLAG_ACTIVITY_CLEAR_TOP will do your job. However there is a catch, you cannot set this flag if you use startActivity from within a broadcast receiver or a service. So in your case I think that you should use the flag FLAG_ACTIVITY_NEW_TASK.

Here is an example:

Intent i = null;
i = new Intent(context, My_activity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i); 

Hope this helps...

于 2013-01-20T01:57:30.543 回答