Bring application to front after user clicks on home button

后端 未结 7 819
礼貌的吻别
礼貌的吻别 2020-11-28 09:21

My application is in running mode[foreground] and user clicks on home button, which puts application to background[and still running]. I ha

相关标签:
7条回答
  • 2020-11-28 09:52

    A combination that works for me is to use:

    Intent bringToForegroundIntent = new Intent(context, RootActivity.class);
    bringToForegroundIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(bringToForegroundIntent);
    

    If you check the logs on the device whenever you start an activity from a launcher icon this is the intent that gets passed to launch the app or move it back to foreground if e.g. user clicked the Home button.

    0 讨论(0)
  • 2020-11-28 10:01

    For an alarm, you want to take a look at starting an Android Service

    This service will be more resilient than your application which may be killed while in the background and can fire off an intent to bring your application to the front (or restart it if it was killed) when it is time for the alarm to go off.

    0 讨论(0)
  • 2020-11-28 10:05

    I find a new way to bring app to foreground, It imitate click Icon to launch application.

        PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(pkName);
        if (intent != null)
        {
                //模拟点击桌面图标的启动参数
                intent.setPackage(null);
             // intent.setSourceBounds(new Rect(804,378, 1068, 657));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
               context.startActivity(intent);
    
        }
    

    it work for me, But who can tell me why package must be null

    0 讨论(0)
  • 2020-11-28 10:05

    Using the ActivityManager class you can bring a running task to front

    void bringToFront(){
    
     ActivityManager activtyManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
     List<ActivityManager.RunningTaskInfo> runningTaskInfos = activtyManager.getRunningTasks(3);
     for (ActivityManager.RunningTaskInfo runningTaskInfo : runningTaskInfos)
                {
                  if (this.getPackageName().equals(runningTaskInfo.topActivity.getPackageName()))
                    {
                         activtyManager.moveTaskToFront(runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
                         return;
                     }
                 }
    }
    
    0 讨论(0)
  • 2020-11-28 10:06
    Intent intent = new Intent(context, MyRootActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    

    You should use your starting or root activity for MyRootActivity.

    This will bring an existing task to the foreground without actually creating a new Activity. If your application is not running, it will create an instance of MyRootActivity and start it.

    EDIT

    I added Intent.FLAG_ACTIVITY_SINGLE_TOP to Intent to make it really work!

    Second EDIT

    There is another way to do this. You can simulate the "launching" of the app the same way that Android launches the app when the user selects it from the list of available apps. If the user starts an application that is already running, Android just brings the existing task to the foreground (which is what you want). Do it like this:

    Intent intent = new Intent(context, SplashScreen.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
                                                    //  the activity from a service
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-28 10:08

    You can use the below code to bring the application to front:

    private void bringApplicationToFront()
        {
    
            Log.d(TAG, "====Bringging Application to Front====");
    
            Intent notificationIntent = new Intent(this, MainActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
            try 
            {
                pendingIntent.send();
            }
            catch (CanceledException e) 
            {
                e.printStackTrace();
            }
        }
    
    0 讨论(0)
提交回复
热议问题