Window Android 学习


参考博客:

http://www.androiddesignpatterns.com/2013/07/binders-window-tokens.html

参考类:

Window Tokens

If you've ever scrolled through the official documentation for Android's View class, chances are you've stumbled across thegetWindowToken() method and wondered what it meant. As its name implies, a window token is a special type of Binder token that the window manager uses to uniquely identify a window in the system. Window tokens are important for security because they make it impossible for malicious applications to draw on top of the windows of other applications. The window manager protects against this by requiring applications to pass their application's window token as part of each request to add or remove a window.3 If the tokens don't match, the window manager rejects the request and throws aBadTokenException. Without window tokens, this necessary identification step wouldn't be possible and the window manager wouldn't be able to protect itself from malicious applications.

By this point you might be wondering about the real-world scenarios in which you would need to obtain a window token. Here are some examples:

  • When an application starts up for the first time, the ActivityManagerService4 creates a special kind of window token called an application window token, which uniquely identifies the application's top-level container window.5 The activity manager gives this token to both the application and the window manager, and the application sends the token to the window manager each time it wants to add a new window to the screen. This ensures secure interaction between the application and the window manager (by making it impossible to add windows on top of other applications), and also makes it easy for the activity manager to make direct requests to the window manager. For example, the activity manager can say, "hide all of this token's windows", and the window manager will be able to correctly identify the set of windows which should be closed.

  • Developers implementing their own custom Launchers can interact with the live wallpaper window that sits directly behind them by calling the sendWallpaperCommand(IBinder windowToken, String action, int x, int y, int z, Bundle extras) method. To ensure that no other application other than the Launcher is able to interact with the live wallpaper, the framework requires developers to pass a window token as the first argument to the method. If the window token does not identify the current foreground activity window sitting on top of the wallpaper, the command is ignored and a warning is logged.

  • Applications can ask the InputMethodManager to hide the soft keyboard by calling thehideSoftInputFromWindow(IBinder windowToken, int flags) method, but must provide a window token as part of the request. If the token doesn't match the window token belonging to the window currently accepting input, theInputMethodManager will reject the request. This makes it impossible for malicious applications to force-close a soft keyboard opened by another application.

  • Applications which manually add new windows to the screen (i.e. using the addView(View, WindowManager.LayoutParams) method) may need to specify their application's window token by setting theWindowManager.LayoutParams.token field. It is very unlikely that any normal application would ever have to do this, since the getWindowManager() method returns a WindowManager which will automatically set the token's value for you. That said, if at some point in the future you encounter a situation in which you need to add a panel window to the screen from a background service, know that you would need to manually sign the request with your application window token in order to achieve it. :P


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值