如果您在Android Web View中调用了shareActionProvider设置菜单项,但无法显示分享表单,则可能是因为您没有正确处理加载成功的事件。您可以尝试以下解决方案:
在您的Activity中创建一个ShareActionProvider对象并为其设置Intent,然后将其传递给菜单项。
在Web View的onPageFinished()方法中调用setShareIntent()方法,将分享Intent传递给ShareActionProvider。
下面是一些示例代码,可能有所帮助:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ShareActionProvider mShareActionProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Other code
MenuItem shareItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
// Other code
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_share) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Share message");
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
在Web View中调用setShareIntent()方法:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Share message");
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
});