Reputation: 27
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!!
Here is the screenShot attached.
Upvotes: 1
Views: 6173
Reputation: 501
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
Upvotes: 0
Reputation: 428
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
Upvotes: 0
Reputation: 6602
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
}
}
Upvotes: 1