使用操作栏中的图标设置共享意图

时间:2013-10-02 19:26:30

标签: android share

我想在我的应用中分享用户正在webview中查看的网站,但是我遵循的任何教程都没有用。我可以在操作栏中加载图标,但不会点击。到目前为止,我已经实现了onclick听众。该应用程序仅适用于ICS或更高版本,因此它不需要向后兼容旧版机器人。我提出的代码是在实现任何共享图标之前,因为我想从头开始或者看看是否有人对我能做的事情有任何想法

   public class WebActivity extends Activity {

    private WebView mWebView = null;
    private EditText mInputUrl = null;

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.activity_web_view);

Intent intent = getIntent();
String thesite = intent.getStringExtra(MainPage.EXTRA_MESSAGE);

mInputUrl = (EditText)findViewById(R.id.input_url);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          String baseurl = "http://";
          String url = baseurl + mInputUrl.getText().toString();
          mWebView.loadUrl(url);
        }
    });

mWebView = (WebView) findViewById(R.id.webview);
mWebView.loadUrl(thesite);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
    public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
     }
 public void onProgressChanged(WebView view, int progress)   
 {
  //Make the bar disappear after URL is loaded, and changes string to Loading...
  MyActivity.setTitle("Loading...");
  MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

  // Return the app name after finish loading
     if(progress == 100)
        MyActivity.setTitle(R.string.app_name);
 }
 });
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
 // Check if the key event was the Back button and if there's history
WebView myWebView = (WebView) findViewById(R.id.webview);
 if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
 myWebView.goBack();
 return true;
 }
 // If it wasn't the Back key or there's no web page history, bubble up to the default
 // system behavior (probably exit the activity)
 return super.onKeyDown(keyCode, event);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
    // This ID represents the Home or Up button. In the case of this
    // activity, the Up button is shown. Use NavUtils to allow users
    // to navigate up one level in the application structure. For
    // more details, see the Navigation pattern on Android Design:
    //
    // http://developer.android.com/design/patterns/navigation.html#up-vs-back
    //
    NavUtils.navigateUpFromSameTask(this);
    return true;
}
return super.onOptionsItemSelected(item);
}
}

修改

我已经使用此代码尝试使其正常工作,但是当我点击图标时它根本没有做任何事情。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, 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, mWebView.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This!"));
}

2 个答案:

答案 0 :(得分:1)

我找到了答案。最后它很简单。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
return true;
}

@Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case R.id.menu_item_share:
        shareURL();
}
if(item.getItemId() == R.id.menu_item_refresh){
    mWebView.reload();
    return true;
}
return super.onOptionsItemSelected(item);
}

private void shareURL() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This Website!"));
}


}

这也让我也有了一个刷新按钮。

答案 1 :(得分:0)

我没有看到onCreateOptionsMenu(..)的任何覆盖,你确定你已经添加了你的ActionItems吗?看看http://developer.android.com/guide/topics/ui/actionbar.html

阿里。