使用操作栏中的图标设置共享意图 [英] Set sharing intent using icon in action bar

查看:37
本文介绍了使用操作栏中的图标设置共享意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to share the website that the user is viewing in the webview in my app, however any of the tutorials I have followed, it just isn't working. I can get the icon to load in the action bar but it doesn't click. I have had onclick listeners implemented and nothing so far. The app is only meant for ICS or higher so it doesn't need to be backwards compatible with older androids. The code I am putting up is before any share icon is implemented as I want to do it from scratch or to see if anyone has any ideas to what I can do

   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);
}
}

编辑

我已使用此代码尝试使其正常工作,但是当我单击该图标时,它根本不执行任何操作.

I have used this code to try and get it working, however when I go to click the icon it doesn't do anything at all.

@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!"));
}

推荐答案

我找到了答案.最后很简单.

I have found the answer. It was rather simple in the end.

@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!"));
}


}

这也让我有一个刷新按钮.

This also allowed me to have a refresh button too.

这篇关于使用操作栏中的图标设置共享意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆