Open Link from app in WebView android - 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!!
Here is the screenShot attached.

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);
}
}
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

Related

Android Chrome Custom Tab Intent Filter Restricted to Only My App?

I'm trying to achieve https://mobikul.com/use-safariviewcontroller-callback-url-objective-c/ on Android, where "sourceApplication" is checked in the iOS example and only allows an SFSafariViewController to redirect back to the application (presumably it's a SFSafariViewController instantiated by the app...but that's another question!).
I am having trouble achieving parity in Android. In my app, BrowserActivity opens up a Chrome Custom Tab, which redirects back to the CallbackActivity from the HTML. Chrome / any browser on phone, I find, is also allowed to redirect back to CallbackActivity if I just enter the correct URL schema, and that's bad news! It seems like there should be a whitelist mechanism in the manifest to only allow a Chrome Custom Tab opened from my application to redirect back to my activity, while disallowing anybody else. I can not set android:exported="false" on CallbackActivity in the manifest because that breaks the redirect capability from my application. How does one achieve this?
Here's what I've got for the Android manifest:
<activity android:name="com.epicgames.ue4.SplashActivity" android:launchMode="singleTask" android:debuggable="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.epicgames.ue4.GameActivity" android:exported="false">
<meta-data android:name="android.app.lib_name" android:value="UE4" />
</activity>
<activity android:name=".BrowserActivity" android:launchMode="singleTask" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<activity android:name=".CallbackActivity" android:launchMode="singleTask">
<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="customscheme" android:host="callback" />
</intent-filter>
And here's what I've got for the relevant activities:
public class BrowserActivity extends Activity {
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
final Uri uri = Uri.parse(getIntent().getStringExtra("url"));
final CustomTabsIntent intent = new CustomTabsIntent.Builder().build();
intent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.intent.setData(uri);
startActivity(intent.intent);
}
}
public class CallbackActivity extends Activity {
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//TODO validate only chrome custom tab HTML, started from my app, redirected here
//getCallingPackage() is always null and referrer host can be spoofed...how to?
final Intent intent = new Intent(this, com.epicgames.ue4.GameActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
}

Is it possible to set unique Intent filter?

What I am trying to achieve is to open Activity automatically when URL is clicked on a browser, notes app or messenger.
So I have this URL format:
http://testsite.com/code/AB1234
In AndroidManifest.xml, I have set Intent filter for Activity:
<activity android:name=".HomeActivity">
<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:host="testsite.com"
android:pathPrefix="/code"
android:scheme="http" />
</intent-filter>
</activity>
In HomeActivity, just getting code from URL and showing it on Toast:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
String code = getIntent().getData().getLastPathSegment();
Toast.makeText(this, code, Toast.LENGTH_LONG).show();
}
When I click on link, system is showing my application and browser application on application chooser. I know that this URL can be handled browser also. I would like to know how to make my intent filter to have priority if it is possible.
Is it possible to open my app automatically? If yes how to make it? How I should change my intent filter?
If you have control over the link, the easiest way to achieve what you want is to set unique scheme for your app.
<data android:host="testsite.com"
android:pathPrefix="/code"
android:scheme="myapp" />

android. Starting activity from url doesn't work from browser but works from text editor

I want to start my Activity when user clicks on URL from browser. I created customized my manifest this way:
<activity
android:name=".views.RestorePwActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data
android:host="mysite.com"
android:scheme="https" />
</intent-filter>
</activity>
Activity opens when I click URL from notes or any text editor, but when I click URL in browser it redirects me to a website.
I saw similar questions on stack-overflow but none of that answers helps.
I also read opinions that modern browsers restrict such behavior. I'm using Android 5+. Is it possible to accomplish that?
Deep link sample code:
Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="splus.in.codestructure">
<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/AppTheme">
<activity android:name=".HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://www.example.com/gizmosā€ -->
<data android:scheme="example"
android:host="deeplinksample" />
<!-- note that the leading "/" is required for pathPrefix-->
</intent-filter>
</activity>
</application>
</manifest>
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_two);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
}
}
You can execute in two ways:
Command Line:
F:\AndroidStudioSetup\sdk\platform-tools> adb shell am start -W -a android.intent.action.VIEW -d "example://deeplinksample" splus.in.codestructure
Output on console:
Starting: Intent { act=android.intent.action.VIEW dat=example://deeplinksample pkg=splus.in.codestructure }
Status: ok
Activity: splus.in.codestructure/.HomeActivity
ThisTime: 90
TotalTime: 10383
WaitTime: 108
Complete
If you want to open from Browser then try below code:
Deeplinking.html
May you keep this file on server or you can copy on your SD card & open with browser, then click on **Deep link Sample. It will open your app.
Let me know know if require more help!
Please refer original document of Android for deep link https://developer.android.com/training/app-links/deep-linking.html
As your code seem fine in manifest but I can't compile it. If you want code help share your project.
I hope given link will helpful!

I want to need android download manager application. i was finished up to my Knoweldge

Now i have one edit text and one button. i am copy the download link from the browser and paste it to the edit text it perfectly work. but i want to need if user using any browser and they click the download link the file will be download via my app.
First you have to have intent filter in your download activity inside manifest like this
<activity
android:name="Downloader">
<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:host="domain.com"
android:scheme="http" />
</intent-filter>
</activity>
Then inside your "Download" activity's onCreate handle data:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
}
You can download file using uri

Taking uri data through intent in android

Hi i have to do connectivity between android app and browser. So while clicking a button on browser it should redirect to android app. in android activity i have written
Uri data = getIntent().getData();
if (data.equals(null)) {
System.out.println("Data is null");
} else {
String scheme = data.getScheme();
System.out.println(scheme);
String host = data.getHost();
int port = data.getPort();
List<String> params = data.getPathSegments();
String first = params.get(0); // "hello"
System.out.println(first);
and in manifest i have already given
<intent-filter>
<data android:scheme="Integration" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
in html on button click i have given <FORM METHOD="LINK" ACTION="Integration://1">
it is throwing a indexoutofboundexception.
Please tell me where is the mistake
Updated
*I was unnecessarily using intent in an activity. By removing that n parameter in html5 my app is running successfully now.*
Quoting answer from: How to listen for a custom URI
To register a protocol in your android app, add an extra block to the AndroidManifest.xml
I modified the code a little, but thought I'd quote the source too
<manifest>
<application>
<activity android:name=".activityToCall">
<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="scheme" android:host="path"/>
</intent-filter>
</activity>
</application>
</manifest>
Then when a url matching your schema is opened your app will be called to handle it I assume.
Taking into consideration that scheme and pathcorrespond to this:
scheme://host/path
After that, in the activity you've set in the manifest to handle this stuff:
Uri data = getIntent().getData();
if (!data.equals(null)){
String scheme = data.getScheme();
//Or whatever you needed
}

Categories

Resources