android技术Android疑难杂症Android开发经验谈

005 功能实现-Activity运行在后台时不在最近任务栏显

2021-12-31  本文已影响0人  凤邪摩羯

1 只要给Activity设置“android:excludeFromRecents=true”即可,

官方的解释如下:
如果设置为true,那么这个Activity将不会出现在最近任务列表中,如果这个Activity是整个Task的根Activity,整个Task将不会出现在最近任务列表中。

这是“android:excludeFromRecents的官方解释:

android:excludeFromRecentsWhether or not the task initiated by this activity should be excludedfrom the list of recently used applications, 
the [overview screen](http://link.zhihu.com/target=http%3A//developer.android.com/guide/components/recents.html). 
That is, when this activityis the root activity of a new task, this attribute determines whether the task should not appear in thelist of recent apps. 
Set "true" if the task should be *excluded* from the list; set "false" if it shouldbe *included*. The default value is "false

2 App首页重写返回键的时间

可以监听onKeyDown方法

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);

}
 @Override
    public void onBackPressed() {
        finishAndRemoveTask();
        Intent intent = new Intent();// 创建Intent对象
        intent.setAction(Intent.ACTION_MAIN);// 设置Intent动作
        intent.addCategory(Intent.CATEGORY_HOME);// 设置Intent种类
        startActivity(intent);// 将Intent传递给Activity
    }

上一篇下一篇

猜你喜欢

热点阅读