如何把一个后台任务,以低于蜂窝状前?后台、任务

2023-09-12 06:43:02 作者:嘻哈风格

在我的应用程序根系活力,我有一个自定义标签栏,包含三个选项卡使用ViewFlipper实现三屏之间切换。

in my applications root activity, I have a customized tab bar, containing three tabs to switch between three screens implemented using ViewFlipper.

我现在想要做的就是给每一个这些屏幕的它自己的活动栈,使得 标签栏在我所有的活动可用,和不同的堆栈之间切换时pressing一个标签。

What I now want to do is to give each of those screens it's own activity stack, making the tab bar available in all of my activities, and switching between the different stacks when pressing a tab.

作品等)在ActivityMaganger类提供一个风情万种的蜂窝,在那里我有bringTaskToFront(。

Works like a charme on honeycomb, where I have bringTaskToFront() available in ActivityMaganger class.

我尝试了许多解决方案,例如:

I tried many solutions, e.g.:

重新启动,希望能在特定的任务顶部的活动,任务将移动到前面 在某种程度上得到蜂窝类

所以,任何想法?

非常感谢,

斯文

推荐答案

在版本蜂窝了,你可以使用MoveTaskToFront像这样

In versions honeycomb and up, you can use MoveTaskToFront like so

//Bring Task to front with Android >= Honeycomb
if (Build.VERSION.SDK_INT >= 11) {
    ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
    List<RunningTaskInfo> rt = am.getRunningTasks(Integer.MAX_VALUE);

    for (int i = 0; i < rt.size(); i++) 
    {
           // bring to front
           if (rt.get(i).baseActivity.toShortString().indexOf("yourproject") > -1) {                     
              am.moveTaskToFront(rt.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);
           }
    }
}

请确保您的项目编译与Android V.11或最多sothat这符合编译不破:

Make sure your Project Compiles with Android v.11 or up sothat this line doesn't break with compile:

am.moveTaskToFront(rt.get(我).ID,ActivityManager.MOVE_TASK_WITH_HOME);

对于较旧的Andr​​oid版本,你可以使用下面的任务移动到前面:

For older Android versions you can use the following to move the task to the front:

Intent intent = new Intent(this, YourClass.class);//The class you want to show
startActivity(intent);

在Android清单添加以下

In the android manifest add the following

<!--The versions with which to compile-->
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11"/>

<!--The Activity you wish to bring forward-->
<activity android:name="YourClass" android:taskAffinity="" android:launchMode="singleTask" />

<!--User Permissions-->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />

然后最后,确保当你把任务回重写 onBack pressed(),不要使用 moveTaskToBack(),这将导致你的任务不是结束了对用户可见。而是使用以下命令:这将模拟一个home键preSS,这将有效地隐藏你的应用程序,并显示在主屏幕

And then lastly, Make sure when you push the task to back by overriding the onBackPressed(), DO NOT USE moveTaskToBack(), It will result in your task not ending up visible to the user. Rather use the following: This will simulate a home button press which will effectively hide your application and show the home screen

@Override
public void onBackPressed() {
    Utils.showToast(this, "Your application is running in the background");
    /*Simulate Home Button Press*/
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
}

和你去那里,我希望这对你的作品,并帮助一些人解决这个问题。

And there you go, I hope this works for you and helps a few people with this problem.