public class ViewServer
extends java.lang.Object
implements java.lang.Runnable
This class can be used to enable the use of HierarchyViewer inside an application. HierarchyViewer is an Android SDK tool that can be used to inspect and debug the user interface of running applications. For security reasons, HierarchyViewer does not work on production builds (for instance phones bought in store.) By using this class, you can make HierarchyViewer work on any device. You must be very careful however to only enable HierarchyViewer when debugging your application.
To use this view server, your application must require the INTERNET permission.
The recommended way to use this API is to register activities when they are created, and to unregister them when they get destroyed:
public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set content view, etc. ViewServer.get(this).addWindow(this); } public void onDestroy() { super.onDestroy(); ViewServer.get(this).removeWindow(this); } public void onResume() { super.onResume(); ViewServer.get(this).setFocusedWindow(this); } }
In a similar fashion, you can use this API with an InputMethodService:
public class MyInputMethodService extends InputMethodService { public void onCreate() { super.onCreate(); View decorView = getWindow().getWindow().getDecorView(); String name = "MyInputMethodService"; ViewServer.get(this).addWindow(decorView, name); } public void onDestroy() { super.onDestroy(); View decorView = getWindow().getWindow().getDecorView(); ViewServer.get(this).removeWindow(decorView); } public void onStartInput(EditorInfo attribute, boolean restarting) { super.onStartInput(attribute, restarting); View decorView = getWindow().getWindow().getDecorView(); ViewServer.get(this).setFocusedWindow(decorView); } }
限定符和类型 | 方法和说明 |
---|---|
void |
addWindow(android.app.Activity activity)
Invoke this method to register a new view hierarchy.
|
void |
addWindow(android.view.View view,
java.lang.String name)
Invoke this method to register a new view hierarchy.
|
static ViewServer |
get(android.content.Context context)
Returns a unique instance of the ViewServer.
|
boolean |
isRunning()
Indicates whether the server is currently running.
|
void |
removeWindow(android.app.Activity activity)
Invoke this method to unregister a view hierarchy.
|
void |
removeWindow(android.view.View view)
Invoke this method to unregister a view hierarchy.
|
void |
run()
Main server loop.
|
void |
setFocusedWindow(android.app.Activity activity)
Invoke this method to change the currently focused window.
|
void |
setFocusedWindow(android.view.View view)
Invoke this method to change the currently focused window.
|
boolean |
start()
Starts the server.
|
boolean |
stop()
Stops the server.
|
public static ViewServer get(android.content.Context context)
android:debuggable
flag set in its manifest, the server returned by this method will
be a dummy object that does not do anything. This allows you to use
the same code in debug and release versions of your application.context
- A Context used to check whether the application is
debuggable, this can be the application contextpublic boolean start() throws java.io.IOException
java.io.IOException
- If the server cannot be created.stop()
,
isRunning()
public boolean stop()
start()
,
isRunning()
public boolean isRunning()
public void addWindow(android.app.Activity activity)
activity
- The activity whose view hierarchy/window to registeraddWindow(android.view.View, String)
,
removeWindow(android.app.Activity)
public void removeWindow(android.app.Activity activity)
activity
- The activity whose view hierarchy/window to unregisteraddWindow(android.app.Activity)
,
removeWindow(android.view.View)
public void addWindow(android.view.View view, java.lang.String name)
view
- A view that belongs to the view hierarchy/window to registerremoveWindow(android.view.View)
public void removeWindow(android.view.View view)
view
- A view that belongs to the view hierarchy/window to unregisteraddWindow(android.view.View, String)
public void setFocusedWindow(android.app.Activity activity)
activity
- The activity whose view hierarchy/window hasfocus,
or null to remove focuspublic void setFocusedWindow(android.view.View view)
view
- A view that belongs to the view hierarchy/window that has focus,
or null to remove focuspublic void run()
run
在接口中 java.lang.Runnable