wil*_*kee 4 android webview shareactionprovider
我想在WebView中获取用户用户所在的任何页面,并允许他们将这些URL与FaceBook/etc共享为ACTION_SEND意图.
我试过这个,但显然在onCreateOptionsMenu中不存在这个URL.如何将其移动到onOptionsItemsSelected?
private ShareActionProvider mShareActionProvider;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider)item.getActionProvider();
mShareActionProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
mShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
web.getUrl());
return shareIntent;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,因为onCreateOptionsMenu第一次显示选项菜单时只调用一次.
解决这个问题非常简单.我们在onOptionsItemSelected调用时构建我们的Intent .这是当选择了膨胀菜单的任何资源时.如果所选项是共享资源,shareURL则执行该项现在构建并启动Intent.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareURL();
}
return super.onOptionsItemSelected(item);
}
private void shareURL() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, web.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This!"));
}
Run Code Online (Sandbox Code Playgroud)
我没有测试上面的代码示例.既不是在实际设备上也不是在Java编译器上.不过它应该可以帮助您解决问题.
| 归档时间: |
|
| 查看次数: |
2568 次 |
| 最近记录: |