skapaid
skapaid

Reputation: 169

how to properly open google play links on html page in android webview with setWebViewClient?

Basically My question is
HOW CAN I POSSIBLY PUT THESE TWO CODES TOGETHER TO FUNCTION PROPERLY!!!
this is killing my trying to figure it out so i'll explain in deep detail...
Any help would be much appreciated!!!

i have a webpage at http://s-ka-paidbeats.com/app_tree/ourotherapps
it lists a bunch of apps i have made in google play.
in the webpage html i have them listed with standard links like - https://play.google.com/store/apps/details?id=com.yesorno.app.yesorno

i've created a tab in ALL the android applaications that i've made called "My Other Apps"
this tab is a webview window that shows the same html page i was talking about above
- http://s-ka-paidbeats.com/app_tree/ourotherapps

the problem is when a user visits the webview window in any of my android applications and clicks on any of the apps i have listed on the html webpage (https://play.google.com/store/apps/details?id=) it takes them to google play in the webview window and asks them to login to google play in the webview window (even if they are already logged in to google play on their device)...
this is extremely ugly and annoying for users to face.

i want to make it so when a user visits the "my other apps" tab in any of my applications, and clicks on one of the apps in the webview window it opens the actual google play application (if google play is installed) or opens in the default browser installed on the device (if google play is not installed)

i have tried to change all the (https://play.google.com/store/apps/details?id=) links to (market://details?id=) links on the html page and then visited the webview window again in my app however this time when i click on any of the apps listed i just get a page error "page does not exist" window

i have looked into setWebViewClient and i am sure that there is someway to do this using something like the code posted below

 webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getScheme().equals("market")) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            Activity host = (Activity) view.getContext();
            host.startActivity(intent);
            return true;
        } catch (ActivityNotFoundException e) {
            // Google Play app is not installed, you may want to open the app store link
            Uri uri = Uri.parse(url);
            view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
            return false;
        }

    }
    return false;
} });


I tried adding the code above to my current code but now my loading dialog box wont close when the webview is loading.... it just stays loading forever.... so i took the code snippet above out of my code again because i dont think i am placing it in the right place or that i am integrating the two codes together properly....

i noticed my current code already has a setWebViewClient defined so im not sure if im allowed to have two in the same code or if i am suppose to try and combine it with the current one....... I HAVE NO CLUE where to begin and i have been reading for hours....

Here is my current code

package com.yesorno.app.yesorno;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
import com.yesorno.app.yesorno.NetorkConnection;
@SuppressLint("SetJavaScriptEnabled")
public class OtherApps extends AppCompatActivity {

private WebView webView;
NetorkConnection ntwrk_con = new NetorkConnection(this);
ProgressDialog dialog;


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    TextView toolsresources5 = (TextView)findViewById(R.id.feedbacktextview);
    toolsresources5.setVisibility(View.INVISIBLE);





    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    webView = (WebView) findViewById(R.id.activity_main_webview);
    dialog = new ProgressDialog(OtherApps.this);


    if (ntwrk_con.isConnectingToInternet()) {

        webView();

    } else {
        dialog_box_for_internet();
    }

}

public void dialog_box_for_internet() {
    if (ntwrk_con.isConnectingToInternet()) {

        webView();

    } else {
        // dismis_dialog_box_for_internet = true;
        AlertDialog.Builder builder = new AlertDialog.Builder(
                OtherApps.this);
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_custom_titile, null);
        TextView title = (TextView) view.findViewById(R.id.myTitle);
        title.setText("Unable To Connect");
        builder.setCustomTitle(view);
        builder.setMessage("No Internet Connection")
                .setCancelable(false)
                .setPositiveButton("Retry",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                if (ntwrk_con.isConnectingToInternet()) {

                                    webView();

                                } else {
                                    new Thread_for_internet().execute();
                                }
                                // dialog.cancel();
                            }
                        })
                .setNegativeButton("Okay",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                finish();

                                // Gridview.super.onBackPressed();
                            }
                        });

        AlertDialog alert = builder.create();
        alert.show();
    }
}

class Thread_for_internet extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.setMessage("Loading..Please wait.");
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

    }

    @Override
    protected Boolean doInBackground(String... args) {

        try {

            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

    protected void onPostExecute(Boolean result) {
        dialog.dismiss();
        dialog_box_for_internet();

    }

}

public void webView() {


    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    });
    dialog.setMessage("Loading All Our Apps...\nPlease wait...");
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();

    webView.loadUrl("http://s-ka-paidbeats.com/app_tree/ourotherapps.html");

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
}





@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (webView.canGoBack()) {
                    webView.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    boolean bRet=false;//set true is menu selection handled
    switch (item.getItemId()) {
        case R.id.action_settings_3:
            Toast.makeText(this, Html.fromHtml("<big><b>Develeped By S-Ka-Paid</b></big><br>© 2016 S-Ka-Paid"), Toast.LENGTH_LONG).show();
            bRet=true;
            break;
        case R.id.action_settings_4:
            Intent intent2 = new Intent(Intent.ACTION_VIEW);
            //Try Google play
            intent2.setData(Uri.parse("market://details?id=com.yesorno.app.yesorno"));
            startActivity(intent2);
            bRet=true;
            break;
        default:
            bRet=super.onOptionsItemSelected(item);
    }
    return bRet;
} }

Basically My question is
HOW CAN I POSSIBLY PUT THESE TWO CODES TOGETHER TO FUNCTION PROPERLY!!!
this is killing my trying to figure it out
Any help would be much appreciated!!!

Upvotes: 1

Views: 1997

Answers (2)

kris larson
kris larson

Reputation: 31015

i have tried to change all the (https://play.google.com/store/apps/details?id=) links to (market://details?id=) links on the html page

Don't do that. Use the google play URLs you started with.

i noticed my current code already has a setWebViewClient defined so im not sure if im allowed to have two in the same code or if i am suppose to try and combine it with the current one

You combine them. You create one WebViewClient to handle all the needs of one WebView. It's not difficult:

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            // is this a play store URL?
            String partialUrl = "/store/apps/details?id=";
            if (url.contains(partialUrl)) {

                // extract the app id from the URL
                int pos = url.indexOf(partialUrl) + partialUrl.length();
                String appId = url.substring(pos);

                try {
                    // open the google play app
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse("market://details?id=" + appId));
                    OtherApps.this.startActivity(intent);
                    return true;  // we overrode the url load

                } catch (ActivityNotFoundException e) {
                    // no google play app, load URL in device browser
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    OtherApps.this.startActivity(intent);
                    return true;
                }
            }
            return false;  // no override, let the webview load this url
        }
    });

Upvotes: 3

skapaid
skapaid

Reputation: 169

for anyone looking for the answer to this... here is the correct code

    webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        // is this a play store URL?
        String partialUrl = "/store/apps/details?id=";
        if (url.contains(partialUrl)) {

            // extract the app id from the URL
            int pos = url.indexOf(partialUrl) + partialUrl.length();
            String appId = url.substring(pos);

            try {
                // open the google play app
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("market://details?id=" + appId));
                OtherApps.this.startActivity(intent);
                return true;  // we overrode the url load

            } catch (ActivityNotFoundException e) {
                // no google play app, load URL in device browser
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                OtherApps.this.startActivity(intent);
                return true;
            }
        }
        return false;  // no override, let the webview load this url
    }
});

Upvotes: 0

Related Questions