How to transfer a recently used third party to the front?

In my application, I want to programmatically bring the most recently used third party to the front. Looking at the other answers here, I tried to restart the activity using baseIntent returned from the list of recent tasks, but this does not seem to lead to activity to the front, no matter what happens.

My ultimate goal is to create an application that replaces the incoming call screen with a small overlay so that the user is not completely removed from any application that they use when they receive the call. I found that you cannot replace the default incoming call screen (if this is not correct, let me know how I would prefer to do it instead) so that the workaround I am trying to call the last used application is in front of the screen (to overlay the incoming call screen ), and then display my overlay on top of this.

Here's the code I'm using (the action runs from the 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 transfer any outside activities to the front? Activity is guaranteed in memory (if it is not in memory, then I would just display the initial screen), so there should be no problems. I also don’t think that this will be a security problem in this case, since it will just show the application that was visible right before the phone rang - although I understand that opening this as a whole in the SDK can be a risk ... hoping this is possible.

EDIT: Changed the code a bit to reflect what I'm doing. The desired task will almost always be the third task on the list — the first current task, and the second — the caller’s task. I can bring the task to the forefront, but it is not always in the same state (for example, on the browser page instead of the settings screen in the browser). How does the recent task list do it?

+4
source share
3 answers

I found this out by looking at the Android source code - this is exactly what the "New Tasks" screen does, both before and post-cell:

 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); } } } 

This requires permission android.permission.REORDER_TASKS.

+7
source

Start with Android L, getRecentTasks () returns to the application its own tasks and, possibly, some other insensitive tasks (for example, Home), so you cannot perform such a task.

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

+1
source

Why don’t you use the global static variable that all actions refer to, but in onPause just set this variable to this activity value. For example, I put a static variable called ActivityName

 public static String ActivityName = ""; 

and in onPause of each action, just give it the pacakage name for the action

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

So, when any action pauses the value, and you learn about the last paused action, and you can restart it using Class.forName (ActivityName)

-1
source

Source: https://habr.com/ru/post/1442229/


All Articles