Share webview url

Asked

Viewed 538 times

0

I have a webview and a share button, I would like to know how to pass the web view url through the share?

Below the code of my Mainactivity.java

package radardf.radardf;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.*;
import android.widget.ShareActionProvider;


import radardf.radardf.R;

import static radardf.radardf.R.menu.menu_main;

public class MainActivity extends Activity {

//Floating Button
ImageButton floatButton;



    //Faz a verificacao da conexao com a internet
//Fim da Verifica��o de conexao com a internet
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView wv = (WebView) findViewById(R.id.webView);
        wv.setWebViewClient(new WebViewClient());
        final WebSettings ws= wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);
        //news implementation
        ws.setSaveFormData(true);
        wv.loadUrl("http://bbacpropaganda.com.br/RaDF");
        wv.getSettings().setUseWideViewPort(true);
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.setWebChromeClient(new WebChromeClient());


//Floating Button
        floatButton = (ImageButton) findViewById(R.id.imageButton);
        floatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             Toast.makeText(getApplicationContext(),
                        "Button is clicked", Toast.LENGTH_LONG).show();
            }
        });
//Fim do Floating Button


        ///

        //Barra de Progress StackOverflow
       /* ProgressDialog progress = new ProgressDialog();
        progress.setMessage("Carregando");
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progress.setIndeterminate(true);
        progress.show();*/


        //Barra de Progresso / Carregando
       final ProgressBar Pbar;
        Pbar = (ProgressBar) findViewById(R.id.progressBar);


        wv.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress < 100 && Pbar.getVisibility() == ProgressBar.GONE) {
                    Pbar.setVisibility(ProgressBar.VISIBLE);
                }
                Pbar.setProgress(progress);
                if (progress == 100) {
                    Pbar.setVisibility(ProgressBar.GONE);

                }
            }
        });
        //Fim da Barra de Progresso / Carregando

        //Verifica se a internet está ativa no aparelho
       /* ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
        NetworkInfo ninfo = cManager.getActiveNetworkInfo();
        if(ninfo!=null && ninfo.isConnected()){
            Toast.makeText(this,"Conectado na internet", Toast.LENGTH_LONG).show();
        }else{
                //Caso não tenha internet, Recarrega a SplashScreen
                    Intent i = new Intent(MainActivity.this, splash_screen.class);
                    startActivity(i);
               //Caso não tenha internet, Recarrega a SplashScreen

            Toast.makeText(this,"Sua Internet Precisa estar Ativa. Estamos Tentando conectar...", Toast.LENGTH_LONG).show();
        }
*/

        /* Caso a pagina da web não funciona*/
        wv.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Intent i = new Intent(MainActivity.this, radardf.radardf.error_webview.class);
                startActivity(i);

            }
        });
         /* Fim:: Caso a pagina da web não funciona*/


        }

//Fecha a Aplicacao Quando pressionar o botao voltar
@Override
public void onBackPressed(){
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
   //Fecha a Aplicacao Quando pressionar o botao voltar



/*
::::::::::::::Menu com Compartilhamento
*/
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.menu_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, "http://www.radardf.com.br");
        return shareIntent;
    }
/*
::::::::::::::Menu com Compartilhamento
*/




}//EndClass

1 answer

0


To get the url just use WebView.getUrl(), but first we need to change some things

Then put WebView as an accessible variable between methods, thus:

public class MainActivity extends Activity
{
    //Floating Button
    ImageButton floatButton;

    WebView wv; //sua variavel

Then change the onCreate for this:

You had called twice super.onCreate and setContentView, is not necessary

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

    wv = (WebView) findViewById(R.id.webView);

And finally access the variable wv so:

shareIntent.putExtra(Intent.EXTRA_TEXT, wv.getUrl());

It would look something like:

private Intent createShareIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, wv.getUrl());
    return shareIntent;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.