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!!
3 Answers
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
}
}

- 6,194
- 5
- 32
- 56
-
Thanks Dear for Your answer but it crashed and showing this Runtime Exception. java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference – Droid Developer Jul 11 '17 at 08:26
-
@ShahrozJavaid probably you need to check also for getIntent().getData() != null. Also show your link if possible. – Andrey Danilov Jul 11 '17 at 09:37
-
i made some changes now when i click on the link it ask for where to open browser or my app. when i select my app it shows only blank page and no action happening. – Droid Developer Jul 12 '17 at 05:21
-
@ShahrozJavaid as I said you have to show your link first. If your link is a path to apk file its normal that you see black page. – Andrey Danilov Jul 12 '17 at 07:40
-
yes @DEADMC my path is to downloadable apk like www.example.com./...apk. I want when i click on any path it apps start downloading the file. How it will be possible? plz guide. – Droid Developer Jul 12 '17 at 09:48
-
@ShahrozJavaid I cant get your idea. You click link in browser, then you open it in your app. Then your app opens it *again* in browser (WebView), right? – Andrey Danilov Jul 12 '17 at 10:07
-
@ShahrozJavaid if yes it sounds like madness. Better idea to download apk file in your own application and then ask to install it. – Andrey Danilov Jul 12 '17 at 10:08
-
NO dear I have a website androidapksfree.com which provides apk's of many apps. And i am developing its application using wordpress api v2. i want user can also download apk's from my app as in web. like playstore. – Droid Developer Jul 12 '17 at 10:40
-
Hopefully u got my concept now. – Droid Developer Jul 12 '17 at 10:40
-
@ShahrozJavaid so did you try https://stackoverflow.com/questions/33434532/android-webview-download-files-like-browsers-do ? – Andrey Danilov Jul 12 '17 at 11:16
-
Yes but it didn't solve my problem. could u plz give me your email for some help. – Droid Developer Jul 13 '17 at 06:41
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

- 428
- 4
- 12
-
Thanks Dear. U explained very well i understood the whole problem. Trying to solve it. – Droid Developer Jul 11 '17 at 08:33
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);
}
}
and my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyTheme"
android:usesCleartextTraffic="true">
<!-- Splash Activity -->
<activity
android:name=".Splash"
android:exported="true">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main Activity -->
<activity
android:name=".MainActivity"
android:launchMode="singleInstance"
android:exported="true" >
<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="http" />
<data android:scheme="https" />
<data android:host="example.myapp" />
<data android:host="www.example.myapp" />
</intent-filter>
</activity>
</application>
Because I was facing an issue app was opening under the title bar of other apps like browsers I needed to set android:launchMode="singleInstance"
and then needed to override another method onNewIntent
to handle singleInstance Launch mode

- 423
- 5
- 14