下面列出了android.os.Message#setTarget ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
/**
* handles long presses for the browser, tries to get the
* url of the item that was clicked and sends it (it can be null)
* to the click handler that does cool stuff with it
*/
public void onLongPress() {
if (mClickHandler == null) {
mClickHandler = new ClickHandler(mActivity);
}
Message click = mClickHandler.obtainMessage();
if (click != null) {
click.setTarget(mClickHandler);
getCurrentWebView().getWebView().requestFocusNodeHref(click);
}
}
/**
* Initialize JavaScript framework
* @param framework String representation of the framework to be init.
*/
public synchronized void initScriptsFramework(String framework) {
Message msg = mJSHandler.obtainMessage();
msg.obj = framework;
msg.what = WXJSBridgeMsgType.INIT_FRAMEWORK;
msg.setTarget(mJSHandler);
msg.sendToTarget();
}
public void takeJSHeapSnapshot(String filename) {
Message msg = mJSHandler.obtainMessage();
msg.obj = filename;
msg.what = WXJSBridgeMsgType.TAKE_HEAP_SNAPSHOT;
msg.setTarget(mJSHandler);
msg.sendToTarget();
}
@Override
public void onLongPress(MotionEvent e) {
if (mCanTriggerLongPress) {
Message msg = mWebViewHandler.obtainMessage();
if (msg != null) {
msg.setTarget(mWebViewHandler);
if (mWebView == null) {
return;
}
mWebView.requestFocusNodeHref(msg);
}
}
}
/**
* Initialize JavaScript framework
* @param framework String representation of the framework to be init.
*/
public synchronized void initScriptsFramework(String framework) {
Message msg = mJSHandler.obtainMessage();
msg.obj = framework;
msg.what = WXJSBridgeMsgType.INIT_FRAMEWORK;
msg.setTarget(mJSHandler);
msg.sendToTarget();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complex);
main_image_view = (ImageView) findViewById(R.id.main_image_view);
bitmapObject = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
main_image_view.setImageBitmap(bitmapObject);
boolean isUiThread = Looper.getMainLooper().getThread() == Thread.currentThread();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
isUiThread = Looper.getMainLooper().isCurrentThread();
}
Log.e("Edwin", "isUiThread = " + isUiThread);
Message message = mHandler2.obtainMessage();
message.what = 1;
message.obj = 100;
message.setTarget(mHandler2);
message.sendToTarget();
Message message2 = mHandler2.obtainMessage();
message2.what = 0;
message2.obj = 200;
message2.sendToTarget();
new LooperThread1().start();
new LooperThread2().start();
}
/**
* Initialize JavaScript framework
* @param framework String representation of the framework to be init.
*/
public synchronized void initScriptsFramework(String framework) {
Message msg = mJSHandler.obtainMessage();
msg.obj = framework;
msg.what = WXJSBridgeMsgType.INIT_FRAMEWORK;
msg.setTarget(mJSHandler);
msg.sendToTarget();
}
@Override
public void onLongPress(MotionEvent e) {
if (mCanTriggerLongPress) {
Message msg = mWebViewHandler.obtainMessage();
if (msg != null) {
msg.setTarget(mWebViewHandler);
if (mWebView == null) {
return;
}
mWebView.requestFocusNodeHref(msg);
}
}
}
public void onLongPress() {
Message click = clickHandler.obtainMessage();
if (click != null) {
click.setTarget(clickHandler);
}
requestFocusNodeHref(click);
}
@Override
public boolean onLongClick(View v) {
if (callback == null) {
return false;
}
final WebView.HitTestResult hitTestResult = webView.getHitTestResult();
switch (hitTestResult.getType()) {
case WebView.HitTestResult.SRC_ANCHOR_TYPE:
final String linkURL = hitTestResult.getExtra();
callback.onLongPress(new IWebView.HitTarget(true, linkURL, false, null));
return true;
case WebView.HitTestResult.IMAGE_TYPE:
final String imageURL = hitTestResult.getExtra();
callback.onLongPress(new IWebView.HitTarget(false, null, true, imageURL));
return true;
case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
// hitTestResult.getExtra() contains only the image URL, and not the link
// URL. Internally, WebView's HitTestData contains both, but they only
// make it available via requestFocusNodeHref...
final Message message = new Message();
message.setTarget(new Handler() {
@Override
public void handleMessage(Message msg) {
final Bundle data = msg.getData();
final String url = data.getString("url");
final String src = data.getString("src");
if (url == null || src == null) {
throw new IllegalStateException("WebView did not supply url or src for image link");
}
if (callback != null) {
callback.onLongPress(new IWebView.HitTarget(true, url, true, src));
}
}
});
webView.requestFocusNodeHref(message);
return true;
default:
return false;
}
}
@Override
public boolean onLongClick(View v) {
if (callback == null) {
return false;
}
final WebView.HitTestResult hitTestResult = webView.getHitTestResult();
switch (hitTestResult.getType()) {
case WebView.HitTestResult.SRC_ANCHOR_TYPE:
final String linkURL = hitTestResult.getExtra();
callback.onLongPress(new IWebView.HitTarget(true, linkURL, false, null));
return true;
case WebView.HitTestResult.IMAGE_TYPE:
final String imageURL = hitTestResult.getExtra();
callback.onLongPress(new IWebView.HitTarget(false, null, true, imageURL));
return true;
case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
// hitTestResult.getExtra() contains only the image URL, and not the link
// URL. Internally, WebView's HitTestData contains both, but they only
// make it available via requestFocusNodeHref...
final Message message = new Message();
message.setTarget(new Handler() {
@Override
public void handleMessage(Message msg) {
final Bundle data = msg.getData();
final String url = data.getString("url");
final String src = data.getString("src");
if (url == null || src == null) {
throw new IllegalStateException("WebView did not supply url or src for image link");
}
if (callback != null) {
callback.onLongPress(new IWebView.HitTarget(true, url, true, src));
}
}
});
webView.requestFocusNodeHref(message);
return true;
default:
return false;
}
}