How to bring most recently used third party Activity to front?

半腔热情 提交于 2020-02-01 05:21:30

问题


In my app, I want to programmatically bring the most recently used third party Activity to the front. After looking at other answers here, I've tried relaunching the activity using the baseIntent returned from a list of recent tasks, but that does not seem to bring the activity to the front over whatever else is going on.

My end goal is to create an app that replaces the incoming call screen with a small overlay so the user is not pulled completely out of whatever app they are using when they get a call. I've found you can't replace the default incoming call screen (if this is not true, please let me know as I'd rather do that instead) so as a workaround, I am trying to call the most recently used app to the front of the screen (to overlay the incoming call screen) and then display my overlay on top of that.

Here's the code I am using (The activity is launched from a broadcast receiver)

public class BringMRUAppToFront extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        ActivityManager activityManager = (ActivityManager) getSystemService("activity");
        List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
        if(recentTasks.size() > 2) {
            RecentTaskInfo recentTask = recentTasks.get(2);
            Intent testIntent = recentTask.baseIntent;
            Log.i("MyApp", "Recent task - " + recentTask.baseIntent.getComponent().getPackageName());

            testIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
            startActivity(testIntent);
        }
        finish();
    }
}

Is there a reliable way to bring any third party activity to the front? The activity is guaranteed to be in memory (if one is not in memory, then I would just display the home screen), so there shouldn't be an issue there. I also don't believe it would be a security issue in this case as it would just be displaying an app that was visible right before the phone rang - though I do understand that opening this up in general in the SDK could pose a risk...still hoping it is possible.

EDIT: Modified the code slightly to reflect what I'm doing. The desired task will almost always be the 3rd task in the list - first is the current task and second is the task of the ringing phone. I am able to call the task to the front, but it is not always in the same state (going to the browser's page instead of the settings screen in the browser, for example). How does the recent tasks list do this?


回答1:


Figured it out by looking at the Android source code - this is exactly what the Recent Tasks screen does, both pre- and post-Honeycomb:

RecentTaskInfo recentTask = recentTasks.get(1); // task 0 is current task
// maintains state more accurately - only available in API 11+ (3.0/Honeycomb+)
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
    final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    am.moveTaskToFront(recentTask.id, ActivityManager.MOVE_TASK_WITH_HOME);
} else { // API 10 and below (Gingerbread on down)
    Intent restartTaskIntent = new Intent(recentTask.baseIntent);
    if (restartTaskIntent != null) {
        restartTaskIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
        try {
            startActivity(restartTaskIntent);
        } catch (ActivityNotFoundException e) {
            Log.w("MyApp", "Unable to launch recent task", e);
        }
    }
}

Permission android.permission.REORDER_TASKS is necessary for this to work correctly.




回答2:


Start from Android L, getRecentTasks() returns application’s own tasks and possibly some other non-sensitive tasks (such as Home), so you can not do such job.

References: https://developer.android.com/preview/api-overview.html#Behaviors




回答3:


Why dont you use a global static variable that is accessed by all activities and on its onPause just set that variable to that activity value Eg I am placing a static variable called ActivityName

public static String ActivityName = "";

and in the onPause of every activity just assign it the activities pacakage name

@Override
public void onPause() {
    super.onPause();
    // The static global variable 
    com.pacakagename.Utls.ActivityName = "com.pacakagename.Act1";
}

So when any of the activity pauses the value is assinged and you will come to know of the most recent paused activity and you can restart it using Class.forName(ActivityName)



来源:https://stackoverflow.com/questions/13080177/how-to-bring-most-recently-used-third-party-activity-to-front

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!