1凄吏、需求
視頻通話Activity可切換小窗(懸浮窗)顯示。不懂的話就是和微信的視頻通話差不多燃乍。
2楞卡、實(shí)現(xiàn)
視頻通話Activity全局只能存在一個(gè)實(shí)例霜运,遂用SingleInstance啟動(dòng)模式。切換到懸浮窗蒋腮,因?yàn)橐4嬉曨l通話Activity的狀態(tài)信息淘捡,所以不能銷毀Activity,那要怎么樣才能返回到上一個(gè)頁(yè)面呢池摧?這里我們利用SingleInstance啟動(dòng)模式的特性:新建一個(gè)Activity Task單獨(dú)用于存放視頻通話Activity的實(shí)例焦除。我們應(yīng)該知道Android系統(tǒng)有且僅有一個(gè)Activity Task會(huì)處于前臺(tái),當(dāng)一個(gè)新的Task開啟后作彤,原來(lái)的Task就會(huì)默認(rèn)退到后臺(tái)(不可見)膘魄,一般情況下,新的Task中的Activity銷毀后竭讳,會(huì)返回到上一個(gè)Task中的TopActivity頁(yè)面创葡,但是懸浮窗顯示的需求不允許我們銷毀這個(gè)Task中的Activity,那要用什么方法才能返回到上一個(gè)頁(yè)面而不銷毀視頻通話Activity呢绢慢?
/**
* 把當(dāng)前Activity示例所在的Task移動(dòng)到后臺(tái)
* {@link android.app.Activity#moveTaskToBack(boolean nonRoot)}
* @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.
*/
moveTaskToBack(true);
這樣當(dāng)前就達(dá)到了視頻通話Activity在不銷毀情況下隱藏的需求了蹈丸。
關(guān)于懸浮窗如何實(shí)現(xiàn)這里不再贅述了,給大家貼幾條鏈接看下吧呐芥。
Android懸浮窗操作使用總結(jié)
Andorid 應(yīng)用內(nèi)懸浮控件實(shí)踐方案總結(jié)
EasyFloat:浮窗從未如此簡(jiǎn)單
3逻杖、問(wèn)題
到現(xiàn)在基本實(shí)現(xiàn)了此需求的大致邏輯。
點(diǎn)擊懸浮窗回到視頻通話Activity很簡(jiǎn)單思瘟,調(diào)用startActivity重新打開視頻通話Activity就好了荸百。
自測(cè)沒問(wèn)題,提測(cè)后問(wèn)題來(lái)了滨攻。够话。。
榮耀和小米5返回不了視頻通話Activity光绕,startActivity絲毫反應(yīng)都沒有女嘲,日志也什么也沒有,我到最后也沒查出個(gè)所以然來(lái)诞帐。所以索性另尋重啟視頻通話Activity的方法了欣尼。
private void moveToFront() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (manager != null) {
List<ActivityManager.RunningTaskInfo> recentTasks = manager.getRunningTasks(Integer.MAX_VALUE);
if (recentTasks != null && !recentTasks.isEmpty()) {
for (ActivityManager.RunningTaskInfo taskInfo : recentTasks) {
ComponentName cpn = taskInfo.baseActivity;
if (null != cpn && TextUtils.equals(VideoChatActivity.class.getName(), cpn.getClassName())) {
manager.moveTaskToFront(taskInfo.id, ActivityManager.MOVE_TASK_NO_USER_ACTION);
break;
}
}
}
}
}
moveTaskToFront方法具體詳情查看下方源碼:
/**
* Flag for {@link #moveTaskToFront(int, int)}: also move the "home"
* activity along with the task, so it is positioned immediately behind
* the task.
*/
public static final int MOVE_TASK_WITH_HOME = 0x00000001;
/**
* Flag for {@link #moveTaskToFront(int, int)}: don't count this as a
* user-instigated action, so the current activity will not receive a
* hint that the user is leaving.
*/
public static final int MOVE_TASK_NO_USER_ACTION = 0x00000002;
/**
* Equivalent to calling {@link #moveTaskToFront(int, int, Bundle)}
* with a null options argument.
*
* @param taskId The identifier of the task to be moved, as found in
* {@link RunningTaskInfo} or {@link RecentTaskInfo}.
* @param flags Additional operational flags.
*/
@RequiresPermission(android.Manifest.permission.REORDER_TASKS)
public void moveTaskToFront(int taskId, @MoveTaskFlags int flags) {
moveTaskToFront(taskId, flags, null);
}
/**
* Ask that the task associated with a given task ID be moved to the
* front of the stack, so it is now visible to the user.
*
* @param taskId The identifier of the task to be moved, as found in
* {@link RunningTaskInfo} or {@link RecentTaskInfo}.
* @param flags Additional operational flags.
* @param options Additional options for the operation, either null or
* as per {@link Context#startActivity(Intent, android.os.Bundle)
* Context.startActivity(Intent, Bundle)}.
*/
@RequiresPermission(android.Manifest.permission.REORDER_TASKS)
public void moveTaskToFront(int taskId, @MoveTaskFlags int flags, Bundle options) {
try {
ActivityThread thread = ActivityThread.currentActivityThread();
IApplicationThread appThread = thread.getApplicationThread();
String packageName = mContext.getPackageName();
getTaskService().moveTaskToFront(appThread, packageName, taskId, flags, options);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
4、總結(jié)
沒問(wèn)題的方法就是好方法停蕉,希望你能用上愕鼓。