Deep linking the right page inside the webview - android

I made a android webview app for our website. Now i want to add deep links to the app, like when somebody clicks a link of our website, you can open it with the webview app instead of chrome web browser. I added these to my manifest:
<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"
android:host="www.example.com" />
Now it opens the webview app when I click a link which is related to us, but it doesnt open the right page, its just starting the app again. But I want to open the directly the right page inside the webview app.
EDIT:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//added
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
But it seems that the variables action and data are not used.
I load the webview like this (for three languages).
//loads the main website
//sets the matching language
web2.clearCache(true);
String loc = Locale.getDefault().toString();
if (loc.startsWith("de")) {
web2.loadUrl("https://www.example.de");
} else if (loc.startsWith("nl")) {
web2.loadUrl("https://www.example.nl");
} else {
web2.loadUrl("https://www.example.com");
}

You must use the intent data that your activity receives when its opened via deep link. Example in Kotlin:
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webview)
val uri = intent.data
webView.loadUrl(uri.toString())
}

Now I did it like this. That is the solution.
Intent intent = getIntent();
Uri data = intent.getData();
String loc = Locale.getDefault().toString();
if (data==null && loc.startsWith("de")) {
web2.loadUrl("https://www.example.de");
} else if (data==null && loc.startsWith("nl")) {
web2.loadUrl("https://www.example.nl");
} else if (data==null && !loc.startsWith("de") && !loc.startsWith("nl")){
web2.loadUrl("https://www.example.com");
} else {
web2.loadUrl(data.toString());
}

Related

Android custom chrome tab with deep links

I have an Android/Kotlin application and I would like to configure deep links for certain pages to open inside my application (to look native).
At the moment, I have intent filters that redirect the user to an activity with WebView in which I open the desired url:
<activity android:name=".activity.WebViewActivity">
<intent-filter android:label="Futurity">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="api.example.com"
android:pathPrefix="/auth/confirm"
android:scheme="https" />
</intent-filter>
</activity>
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)
val data = intent.data
// construct url
val url = if (intent.data != null ) {
"https://" + data.host + data.path + "?" + data.query
}
appWebView.webViewClient = WebViewClient()
appWebView.loadUrl(url)
}
}
This works fine, but I would like to use Chrome custom tabs instead for security reasons.
However, when I try to configure the custom tab instead of WebView, I am getting an infinite loop of redirects between the page (launched in the chrome tab) and the intent filter which immediately redirects user back to the activity:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
How can I achieve the same behavior as with webviews but without modifying the url? Is it even doable?
I did some digging and found out that you should search for packages that support the "warming services". If a package is returned then you can assign the package name to the Intent. Google has a GitHub repo with some helper classes. The one in question is CustomTabHelper#getPackageNameToUse().
So in your case:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
String packageName = CustomTabsHelper.getPackageNameToUse(getContext());
if (packageName != null) {
customTabsIntent.intent.setPackage(packageName);
}
customTabsIntent.launchUrl(this, Uri.parse(url));
If a package is not found then you'll want to have some fallback code else the infinite loop will keep happening.

Android Download file from a link

After I've implemented a solution for the Android app to post to web server and validate Google Order then issue a download link. Now I am trying to work on a code for the App to read the link from the thankyou.php page
Download File
The file is a ".DAT" extension and coming from a certain link. The App should retrieve the link in a new page and let the customer download the file.
Inside you manifest file you need to add an intent filter:
<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:
public class Download extends Activity {
private String filename;
#Override
protected void onCreate (final Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(<your layout file>);
final Intent intent = getIntent ();
final Uri data = intent.getData ();
if (data != null) {
final List<String> pathSegments = data.getPathSegments ();
fileName = pathSegments.get (pathSegments.size () - 1);
}
}
Then in the clickhandler for the download button you can use a view intent to act like a link in android.
button.setOnClickListener (new View.OnClickListener () {
#Override public void onClick (final View v) {
Uri intentUri = Uri.parse("http://domain.com/" + filename);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(intentUri);
startActivity(intent);
}
});

Android redirect uri

I'm working with the instagram API and I don't understand what I should put in for the redirect api. At the moment I simply put in https://127.0.0.1
But I dont really understand. Also, once I get the allow/cancel screen and I press allow it gives me an error saying that I cant go to that address but I can also see the authorization code appended on to the address. So how can i redirect back from my redirect uri? How can I tell android that after user clicks allow to come back into the app use the code for further authentication?
Im sure you will say something like make my own custom scheme/ intent filters etc but please be a little more supportive im new and I dont understand and I did do research on them.
My on resume method is below
#Override
protected void onResume() {
super.onResume();
// the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(redirectUri)) {
// use the parameter your API exposes for the code (mostly it's "code")
String code = uri.getQueryParameter("code");
if (code != null) {
// get access token
LoginService loginService =
ServiceGenerator.createService(LoginService.class, clientId, clientSecret);
AccessToken accessToken = loginService.getAccessToken(code, "authorization_code");
} else if (uri.getQueryParameter("error") != null) {
// show an error message here
}
}
}
This is my manifest snippet dealing with intent filters:
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
<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="redirecturi"
android:scheme="your" />
</intent-filter>
</activity>
You have to setup an event listener on the browser view and check for code in URL param if the URL is equal to the redirect_uri, and then make POST request to the auth URL using the code and client_secret as documented in Instagram authentication
something like this:
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String code = url.getQueryParameter("code");
// ...
}
});

Enable deep linking for android for various structured urls

I've already enabled deep linking & it opens app from url. However it opens only the main url i.e. m.example.com & not m.example.com\products\iphone-5s\.
I'm using WebView to load my mobile website into an Android app.
Here's a code of xml file:
<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="m.example.com"
android:pathPrefix="/"
android:scheme="http" />
</intent-filer>
Java file:
private String url='m.example.com';
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
mainWebView.loadUrl(url);
}
Can anybody help to resolve this?
Get the URL link with this code, right after Uri data = intent.getData();
String link = intent.getDataString();
Then, set simple if/else
if (link == null) {
mWebView.loadUrl("yourMainURL");
} else {
mWebView.loadUrl(link);
}

Android return from browser to app

I have option in my app to start browser and load imdb website.
I'm using ActionView for this.
Intent intent1 = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(website));
try {
activity.startActivity(intent1);
} catch (Exception e) {
Toast.makeText(activity, R.string.no_imdb, Toast.LENGTH_SHORT)
.show();
}
The problem occurs when I tap on back button.
When default browser app is launched everything is ok.
When Opera Mini app is launched, when I tap on back button, it seems like my app receives two back actions, and finish my current activity.
How to prevent this?
Try starting the intent in a new task:
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Or
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Please add this code to your android manifest for activity that you need return
<activity
android:name="YourActivityName"
android:launchMode="singleTask">
<intent-filter>
<action android:name="schemas.your_package.YourActivityName" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.ALTERNATIVE" />
</intent-filter>
</activity>
and add this to your web page
click to load app
because only one app has this action name (schemas.your_package.YourActivityName) on your phone, web page directly return to app
Also You can Use Airbnb DeepLink lib
Example
Here's an example where we register SampleActivity to pull out an ID from a deep link like example://example.com/deepLink/123. We annotated with #DeepLink and specify there will be a parameter that we'll identify with id.
#DeepLink("example://example.com/deepLink/{id}")
public class SampleActivity extends Activity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) {
Bundle parameters = intent.getExtras();
String idString = parameters.getString("id");
// Do something with idString
}
}
}

Categories

Resources