0

I'm trying to start another activity from html webview.So i have one working link but second link is not working. And it says "Webpage not available"

public class acilisbir extends AppCompatActivity {

WebView myBrowser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_acilisbir);

    myBrowser = (WebView) findViewById(R.id.anasayfagetir);
    myBrowser.setWebViewClient(new MyBrowser());
    myBrowser.getSettings().setJavaScriptEnabled(true);
    myBrowser.loadUrl("file:///android_asset/anasayfa.html");
}

 private class MyBrowser extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//This link is work
if (url.equalsIgnoreCase("Navigation://OpenNativeScreen")) {
                startActivity(new Intent(acilisbir.this,AnaEkran.class));
                finish();
                return true;
             //But this is not
            }else if (url.equalsIgnoreCase("activity_buy://Buy")) {
                startActivity(new Intent(acilisbir.this,Buy.class));
                finish();
                return true;
            }
            return false;
        }
    }

}

I'add to my manifest

<activity android:name="Buy" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="activity_buy" />
            </intent-filter>
        </activity>

Html link code

<INPUT TYPE="test" value="Test" onClick="window.location='Navigation://OpenNativeScreen'">
<INPUT TYPE="buycr" value="Buycr" onClick="window.location='activity_buy://Buy'">

here the link targeting the Buy Activity is not working.I have no error in android studio.But its not starting the BuyActivity when the link is clicked.

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
Dorbagna
  • 307
  • 1
  • 4
  • 16

2 Answers2

2

According to wikipedia your scheme must not contains any other caracters that letters or "+", "-", ".".

Try renaming activity_buy://Buy to activitybuy://Buy.

JulianCDC
  • 133
  • 7
0

Thanks to this answer and According to RFC 2396,

Appendix A: URI scheme shall be follow this:

scheme        = alpha *( alpha | digit | "+" | "-" | "." )

Then Replace this:

url.equalsIgnoreCase("activity_buy://Buy")
"window.location='activity_buy://Buy'"

With:

url.equalsIgnoreCase("activity.buy://Buy")
"window.location='activity.buy://Buy'"
  • Please consider to up-vote JulianCDC answer for mentioning reference for expected issue.

Update

You may use Uri.pasre(url) instead of url.equalsIgnoreCase

Uri uri = Uri.parse(url);
String scheme = uri.getScheme();
String host = uri.getHost();
switch (scheme){
    case "Navigation":
        handleNavigation(host);
        break;
    case "activity.buy":
        handleActivityActions(host);
        break;
}

private void handleNavigation(String host) {
    // do other stuff
    if ("OpenNativeScreen".equals(host)) {
        startActivity(new Intent(acilisbir.this, AnaEkran.class));
        finish();
    }
}

private void handleActivityActions(String host) {
    // do other stuff
    if ("Buy".equals(host)) {
        startActivity(new Intent(acilisbir.this,Buy.class));
        finish();
    }
}
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
  • 1
    Unfortunately my reputation point it's not enough yet for that.But i will when it's enough.I'm following this topic https://stackoverflow.com/questions/20791482/start-activity-intent-on-clicking-text-inside-webview and i didn't know about to Appendix.Thank you to. – Dorbagna Nov 08 '18 at 20:41