最近开发了一款TV版app,主要功能是视频通话,所以要求机顶盒是一开机,就要把app打开,因为时刻有别人打过来。然后点击返回按钮,需要把app切到后台,但是app仍需要存活,需要收到消息。所以这里就设计到了app退到后台与切到前台的功能。

app退到后台

1.一般情况下,是启动Home页就可以实现这个功能,代码如下

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);

这里需要注意的是,如果Launcher和Home不是同一个,那就不能这么用。比如说华数机顶盒Launcher,启动第三方app都是从这里打开的。然后这里如果执行了上述代码,启动了Home,那就跳转到了Android系统的Home,与退到后台的效果有所出入了。

2.执行Activity#moveTaskToBack()

moveTaskToBack(false);

这里也需要注意,传入的参数(boolean nonRoot)

/*** Move the task containing this activity to the back of the activity* stack.  The activity's order within the task is unchanged.** @param nonRoot If false then this only works if the activity is the root*                of a task; if true it will work for any activity in*                a task.** @return If the task was moved (or it was already at the*         back) true is returned, else false.*/public boolean moveTaskToBack(boolean nonRoot) {}

根据源码注释可以知道,nonRoot=false时,只有当当前Activity为root activity根Activity时才会把当前task退回到后台。
notRoot=true时,不管当前是否是root activity都会把当前task退回到后台

app切到前台

1.使用Intent启动需要切到前台的Activity

Intent intent = new Intent(this, MainActivity.class);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setAction(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);

这里的MainActivity.class就是需要启动的Activity,用这个方法,当启动了其他app时,可能会失效。

2.通过ActivityMananger把task切到前台

ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
am.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_WITH_HOME);

Android退到后台与切到前台相关推荐

  1. android 切回前台,Android退到后台与切到前台

    最近开发了一款TV版app,主要功能是视频通话,所以要求机顶盒是一开机,就要把app打开,因为时刻有别人打过来.然后点击返回按钮,需要把app切到后台,但是app仍需要存活,需要收到消息.所以这里就设 ...

  2. Android 应用在后台时,跳转 Activity 会自动切换应用至前台

    本博客 demo 见:demo. 平常用手机的时候经常碰到这种情况,用首屏广告举个栗子~很多应用都会有首屏广告 activity A,假设此应用是 app C,如果此时要使用别的应用,就会使得 app ...

  3. 【Android】判断你的应用在前台还是在后台

    我的尝试 /*** 判断应用是否是在后台*/public static boolean isBackground(Context context) {ActivityManager activityM ...

  4. android唤醒前台,Android将后台应用唤起到前台的方法 (SDK 4.0, ActivityLifecycleCallbacks)...

    对于4.0以上的机型也是采用通过Application注册ActivityLifecycleCallbacks接口,对于4.0以下的机型我们会尝试反射ActivityThread中的mInstrume ...

  5. android 应用退到后台,类似最小化

    2019独角兽企业重金招聘Python工程师标准>>> 在activity中调用 moveTaskToBack (boolean nonRoot)方法即可将activity 退到后台 ...

  6. android 8.0后台定位,Android8.0适配前台定位服务service的示例代码

    从Android 8.0开始系统为实现降低功耗,对后台应用获取用户位置信息频率进行了限制,每小时只允许更新几次位置信息,详细信息请参考官方说明.按照官方指引,如果要提高位置更新频率,需要后台应用提供一 ...

  7. android 8.0后台定位,Android 8.0权限说明

    从Android 8.0开始系统为实现降低功耗,对后台应用获取用户位置信息频率进行了限制,每小时只允许更新几次位置信息,详细信息请参考官方说明.按照官方指引,如果要提高位置更新频率,需要后台应用提供一 ...

  8. 【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 启动相同 id 的第二个前台 Service 关闭通知 )

    文章目录 一. 前台 Service 通知问题 二. 设置 startForeground id 参数为 0 三. 启动相同 id 的第二个前台 Service 关闭通知 1. 前台服务 1 2. 关 ...

  9. Android监听后台状态,退出即杀死并显示退出提示框

    一.当界面不可见时释放内存 @Overridepublic void onTrimMemory(int level) {super.onTrimMemory(level);switch (level) ...

最新文章

  1. python autopy_安装python autopy时出错
  2. C#正则实现匹配一块代码段
  3. Sources for NFC/RFID inspection ( Soft Materials )
  4. OpenDataSource和OPENROWSET
  5. 文献记录(part14)--Biclustering with dominant sets
  6. 面试被问 Go 的map[int64]int64 在 redis 会占多少空间...
  7. (21)Verilog HDL结构:task语句
  8. tomcat J2EEApplication=none,J2EEServer=none
  9. python接球游戏
  10. Eclipse组合KEmulator
  11. SpringCloud第十章zuul路由网关
  12. Simulink统一模块大小
  13. android c++服务器端,c++服务器与android客户端进行文件传输功能的实现
  14. mutual information matlab,Mutual information and Normalized Mutual information 互信息和标准化互信息...
  15. ActiveMQ 基础知识
  16. pyecharts js 地图无法显示 Map china not exists the geoJson of the map must be provided
  17. 64位驱动 hp630打印机_HP Deskjet 1280打印机x64驱动下载|HP1280打印机windows7 x64位驱动 - 驱动无忧...
  18. Android 状态栏工具类(实现沉浸式状态栏/变色状态栏)
  19. 分子模拟对接教程—带你从 0 到 1
  20. DXC帮助全球企业和公共领域客户拥抱变革

热门文章

  1. Maven项目打包遇到的两个问题
  2. 递归方程组解的渐近阶的求法--套用公式法
  3. notion 科研_Extensions of the notion of overall comonotonicity to partial comonotonicity
  4. 【中级软件设计师】—(摆烂记点单词)计算机专业英语单词总结(四十三)
  5. arcgis10.3 Unauthorized Access无权限解决方法
  6. 盛大游戏李阳:龙之谷服务器设计
  7. springMVC源码分析--@SessionAttribute用法及原理解析SessionAttributesHandler和SessionAttributeStore
  8. 小学计算机小组期末总结范文,第一学期小学三年级级组工作总结
  9. 红魔风扇拆解原理和手册解读
  10. [2016湘潭邀请赛 A. 2016] 大数取模+循环节