UiModeManager 最简单切换模式的途径
Posted Iaouei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UiModeManager 最简单切换模式的途径相关的知识,希望对你有一定的参考价值。
这是源码,通俗易懂,就贴出来了。
package android.app;
import android.content.Context;
import android.content.res.Configuration;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
/**
* 这个类提供系统UI模式服务的途径. 这个服务允许应用控制设备的UI模式.
* 提供设置行车模式和夜间模式的功能.
*
* <p>这个功能是构建在广播的基础上:当用户把设备放进一个地方或从中取出
* @link android.content.Intent#ACTION_DOCK_EVENT . 这个时候,
* the UiModeManager 会切换系统 @link android.content.res.Configuration
* 进入设置 UI 模式, 发送进入此模式的广播, 并且开始在适应此时的模式.
* 参看广播 @link #ACTION_ENTER_CAR_MODE 和
* @link #ACTION_ENTER_DESK_MODE 获取更多信息.
*
* <p>另外, 用户也可以手动切换模式. While in car mode -- whether by manual action
* from the user or being physically placed in a dock -- a notification is
* displayed allowing the user to exit dock mode. Thus the dock mode
* represented here may be different than the current state of the underlying
* dock event broadcast.
*
* <p>不可以直接实例化这个类; 通过
* @link android.content.Context#getSystemService
* Context.getSystemService(Context.UI_MODE_SERVICE)获取这个服务.
*/
public class UiModeManager
private static final String TAG = "UiModeManager";
/**
* 当设备UI进入行车模式,发送广播, either
* by being placed in a car dock or explicit action of the user. After
* sending the broadcast, the system will start the intent
* @link android.content.Intent#ACTION_MAIN with category
* @link android.content.Intent#CATEGORY_CAR_DOCK
* to display the car UI, which typically what an application would
* implement to provide their own interface. However, applications can
* also monitor this Intent in order to be informed of mode changes or
* prevent the normal car UI from being displayed by setting the result
* of the broadcast to @link Activity#RESULT_CANCELED.
*/
public static String ACTION_ENTER_CAR_MODE = "android.app.action.ENTER_CAR_MODE";
/**
* 当设备UI离开行车模式回到正常模式,发送广播. Typically used by a car mode app, to dismiss itself
* when the user exits car mode.
*/
public static String ACTION_EXIT_CAR_MODE = "android.app.action.EXIT_CAR_MODE";
/**
* Broadcast sent when the device's UI has switched to 夜间模式,
* by being placed in a desk dock. After
* sending the broadcast, the system will start the intent
* @link android.content.Intent#ACTION_MAIN with category
* @link android.content.Intent#CATEGORY_DESK_DOCK
* to display the desk UI, which typically what an application would
* implement to provide their own interface. However, applications can
* also monitor this Intent in order to be informed of mode changes or
* prevent the normal desk UI from being displayed by setting the result
* of the broadcast to @link Activity#RESULT_CANCELED.
*/
public static String ACTION_ENTER_DESK_MODE = "android.app.action.ENTER_DESK_MODE";
/**
* Broadcast sent when the device's UI has switched 离开夜间模式回到正常模式. Typically used by a desk mode app, to dismiss itself
* when the user exits desk mode.
*/
public static String ACTION_EXIT_DESK_MODE = "android.app.action.EXIT_DESK_MODE";
/** Constant for @link #setNightMode(int) and @link #getNightMode():
* automatically switch night mode on and off based on the time.
*/
public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_UNDEFINED >> 4;
/** Constant for @link #setNightMode(int) and @link #getNightMode():
* never run in night mode.
*/
public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4;
/** Constant for @link #setNightMode(int) and @link #getNightMode():
* always run in night mode.
*/
public static final int MODE_NIGHT_YES = Configuration.UI_MODE_NIGHT_YES >> 4;
private IUiModeManager mService;
/*package*/ UiModeManager()
mService = IUiModeManager.Stub.asInterface(
ServiceManager.getService(Context.UI_MODE_SERVICE));
/**
* Flag for use with @link #enableCarMode(int): go to the car
* home activity as part of the enable. Enabling this way ensures
* a clean transition between the current activity (in non-car-mode) and
* the car home activity that will serve as home while in car mode. This
* will switch to the car home activity even if we are already in car mode.
*/
public static final int ENABLE_CAR_MODE_GO_CAR_HOME = 0x0001;
/**
* Flag for use with @link #enableCarMode(int): allow sleep mode while in car mode.
* By default, when this flag is not set, the system may hold a full wake lock to keep the
* screen turned on and prevent the system from entering sleep mode while in car mode.
* Setting this flag disables such behavior and the system may enter sleep mode
* if there is no other user activity and no other wake lock held.
* Setting this flag can be relevant for a car dock application that does not require the
* screen kept on.
*/
public static final int ENABLE_CAR_MODE_ALLOW_SLEEP = 0x0002;
/**
* 使设备进入行车模式, like it had been placed in the car dock.
* This will cause the device to switch to the car home UI as part of
* the mode switch.
* @param flags 必须为 0.
*/
public void enableCarMode(int flags)
if (mService != null)
try
mService.enableCarMode(flags);
catch (RemoteException e)
Log.e(TAG, "disableCarMode: RemoteException", e);
/**
* Flag for use with @link #disableCarMode(int): go to the normal
* home activity as part of the disable. Disabling this way ensures
* a clean transition between the current activity (in car mode) and
* the original home activity (which was typically last running without
* being in car mode).
*/
public static final int DISABLE_CAR_MODE_GO_HOME = 0x0001;
/**
* 如果当前在行车模式,关闭这个模式.参数可以为0:退出此模式;DISABLE_CAR_MODE_GO_HOME:退出此模式并返回桌面
* @param flags May be 0 or @link #DISABLE_CAR_MODE_GO_HOME.
*/
public void disableCarMode(int flags)
if (mService != null)
try
mService.disableCarMode(flags);
catch (RemoteException e)
Log.e(TAG, "disableCarMode: RemoteException", e);
/**
* 返回当前运行的模式. 可以为:
* @link Configuration#UI_MODE_TYPE_NORMAL Configuration.UI_MODE_TYPE_NORMAL,
* @link Configuration#UI_MODE_TYPE_DESK Configuration.UI_MODE_TYPE_DESK,
* @link Configuration#UI_MODE_TYPE_CAR Configuration.UI_MODE_TYPE_CAR,
* @link Configuration#UI_MODE_TYPE_TELEVISION Configuration.UI_MODE_TYPE_TELEVISION,
* @link Configuration#UI_MODE_TYPE_APPLIANCE Configuration.UI_MODE_TYPE_APPLIANCE, or
* @link Configuration#UI_MODE_TYPE_WATCH Configuration.UI_MODE_TYPE_WATCH.
*/
public int getCurrentModeType()
if (mService != null)
try
return mService.getCurrentModeType();
catch (RemoteException e)
Log.e(TAG, "getCurrentModeType: RemoteException", e);
return Configuration.UI_MODE_TYPE_NORMAL;
/**设置夜间模式。在API22及以下当在设备上同时也设置了行车模式或桌面模式,夜间模式才会生效。在API23及以上则没有这个限制
*
* <p>模式可以为:
* <ul>
* <li><em>@link #MODE_NIGHT_NO<em> - 日间模式
* mode.</li>
* <li><em>@link #MODE_NIGHT_YES</em> - 夜间模式.
* </li>
* <li><em>@link #MODE_NIGHT_AUTO</em> - 自动切换,根据设备位置及相关传感器.</li>
* </ul>
*/
public void setNightMode(int mode)
if (mService != null)
try
mService.setNightMode(mode);
catch (RemoteException e)
Log.e(TAG, "setNightMode: RemoteException", e);
/**
* @return 返回当前配置的夜间模式. May be one of
* @link #MODE_NIGHT_NO, @link #MODE_NIGHT_YES,
* @link #MODE_NIGHT_AUTO, or -1 on error.
*/
public int getNightMode()
if (mService != null)
try
return mService.getNightMode();
catch (RemoteException e)
Log.e(TAG, "getNightMode: RemoteException", e);
return -1;
除了在代码中进行相关配置外,在xml文件进行布局时,同时可以进行相关模式的布局,即在相同布局名后面加个后缀就可以了,这样就指定了相关模式的UI配置。这是最简单的切换模式的一种实现途径。
相关后缀参考API指南提供资源部分。
以上是关于UiModeManager 最简单切换模式的途径的主要内容,如果未能解决你的问题,请参考以下文章