简体   繁体   中英

Open Link from app in WebView android

I am developing app which have a downloading link which opens in mobile browser by clicking upon it but I want it to open in my webView in my app. My app is not web app I just want to open apps links in webview. I have hundreds of apps on my website. I am getting apps from wp api and like playstore the user can download apps from my application. I tried different solution but not succeeded. SS is attached to clerify my question. Need Help!!

First of all specify intent-filters to open link in app

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:scheme="https"/>
        <data android:scheme="http"/>
        <data android:host="pollux.androidapksfree.com"/>
        <data android:pathPrefix="/hdata"/>

    </intent-filter>

Then handle it and pass to WebView in your launcher Activity.

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //check that link is not null 
        //or that you opened app from deep link
        if (getIntent() != null) {
            Uri intentUri = getIntent().getData(); //get link
            webView = (WebView) findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(intentUri.toString()); //open it in webView
         }
  }

On clicking the link, Android OS will create an Intent with that link as URL and search for apps that can handle that intent.Since mobile browser can handle those "http://" intents , the intent is thrown to mobile browser and the link is opened there. If you want to open it in your webView than you have to declare that your activity can handle these intents and have to make your activity default to handle these intents.

It can be done by following this link

https://developer.android.com/training/app-indexing/deep-linking.html

please note that if you have done this then any link with these domain clicked from anywhere will be opened only with your app as it is made default

For someone who is still struggling with this here is what worked for me

String defaultUrl = "myapp.com/home"
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = findViewById(R.id.web);
    webView.getSettings().setJavaScriptEnabled(true);

    // Handle if the url from external app was clicked
    Intent appLinkIntent = getIntent();
    manageIntent(appLinkIntent, defaultUrl);
 }

// override to get the new intent when this activity has an instance already running
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // again call the same method here with the new intent received
    Log.e(TAG, "onNewIntent: URL called on new intent");
    manageIntent(intent, defaultUrl);
}

public void manageIntent(Intent intent, String defaultUrl) {
    String appLinkAction = intent.getAction();
    Uri appLinkData = intent.getData();
    if (appLinkData != null) {
        webView.loadUrl(String.valueOf(appLinkData));
    } else{
        webView.loadUrl(defaultUrl);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM