Android Phonegap - Open iframe links in external browser instead of Phonegap WebView - android

I'm having what seems to be a pretty small issue in the Phonegap 1.3.0 application I am working on. It is for Android devices.
The problem is stemming from the ads I am trying to incorporate into my app. I am trying t use Leadbolt's no-SDK offer wall as well as their banner ads.
I am loading the offer wall within an iframe in my app, inside of a hidden DIV, and then displaying it when required - this part is working great.
The problem comes when I click on one of the links on the offer wall: instead of launching the clicked URL in a new External/Native browser, the link is being opened within the iframe. The same thing happens when someone clicks the banners, though these are integrated by inserting a in the location you want the ad to show rather than via an iframe. (Maybe the script injects an iframe, dunno, the end behavior is the same)
What I'm trying to do now is to implement a method in Java to catch any clicks, and open a new browser if the link is not relative/local - i.e. if it is prefixed by "http", "https", or "market" protocols
And here lies the real issue - the only experience I have with Java has been messing with Phoengap a bit :(
I have been reading and testing things all day, but finally I must resort to asking those of you who are more knowledgeable than I am in regards to Java programming.
Here is what I have been attempting so far:
package com.phonegap.my_great_app;
import android.os.Bundle;
import android.webkit.WebView;
import com.phonegap.*;
public class MyGreatApp extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
/* Intercept clicked links, and open external URLs in external browser */
public boolean shouldOverrideUrlLoading(DroidGap super, String url) {
if (url != null && url.startsWith("market://") || url.startsWith("http://") || url.startsWith("https://")) {
/* Open new WebView or external browser with URL */
/* Do it here */
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else {
/* Do nothing, it's relative URL from my app */
return false;
}
}
}
}
I am sure this code is laughably wrong, but hopefully it can illustrate what I'm trying to achieve here. Truth be told, I have no idea if shouldOverrideUrlLoading() is even the right method to be calling for this problem!
I will be very grateful for any advice or code samples you can offer! Thanks :)
EDIT: Just to be clear - I cannot alter the iframe source code so javascript or changing the link targets is not an option due to XSS issues.

You can open external links in the default browser instead of opening inside your phonegap application. This can be done in two ways easily.
One is by using Phonegap medthod like this,
navigator.app.loadUrl('http://stackoverflow.com');
/*NOTE : In some phonegap versions it does not works*/
Another way is using jquery mobile, you can do like this,
YouTube Channel
Thats all.

Related

Android and custom URL scheme

I'm working on an app with a custom url scheme.
It is opening some webpage for authentication in a chrome tab. This is done in xamarin forms like this:
Browser.OpenAsync(apiUrl + "mobile", new BrowserLaunchOptions
{
LaunchMode = BrowserLaunchMode.SystemPreferred,
TitleMode = BrowserTitleMode.Hide,
});
everything work as expected if I return a webpage with a link and click the the link manually:
Click here to go the app
But if i return a 302 redirect to the same url it will not close the chrome tab and dont focus the app again.
If i add a javascript in the response, it will not automatically open the url (close the chrome tab and focus the app)
I've tried things like this:
window.location = url;
window.open(url,'_self');
setTimeout(()=>window.open(url,'_self'),10);
(url is a valid variable, even tried alert(url) after changing the location and it show the correct url.
Why does it only work when I click the link manually?
In order to maintain the user's security and experience, the browser prohibits the direct use of window.open(URL) in JS to open new links.
Try to change like below:
setTimeout(()=>window.open(url,'_self'),500); //The delay time must not be too short or you will be intercepted

Custom Tabs Support Library

Recently was added this support library, but I couldn't find any example.
What the purpose of this library?
Could you post any example using this library?
CustomTabs is used to open links in a browser that supports CustomTabs. Most likely opening is done on Chrome, hence CustomTabs is part of chromium platform.
Purpose is to avoid implementing WebViews in your application and yet giving you option for styling actual chrome tabs, like toolbar color, title, various exit/enter transition, adding action buttons and menues. CustomTabs will allow your application bind to the chrome service and make chrome work as part of your application. Styling which will give you feel the opened web resource is part of your application.
Beside the styling, CustomTabs will give you full chrome web capabilities that probably couldn't be achieved with standard WebView.
Here are demos, which are straight forward.
Edit:
A snippet from my application which is "simplified" version of the Google's demo, lacking fallback mechanism, for now.
Usage of the helper is the following:
Initialize it when your activity is alive
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preview);
mCustomTabHelper = new SimpleCustomChromeTabsHelper(this);
}
2. When the instance is alive and we have an url ready to be opened we can call:
mCustomTabHelper.prepareUrl(mProduct.getRedirectUrl());
Which will bind to the Chrome service, if not previously bind, or will just notify Chrome service that we might be opening that link in the future.
CustomTabSession can be used to open or prepare multiple url.
Open the url
mCustomTabHelper.openUrl(mProduct.getRedirectUrl());
The overloaded method of openUrl is using sort of ui options builder that is replica of the CustomTabIntent.Builder, but I have dropped the CustomTabsSession argument so the helper later will build CustomTabIntent internally.
I'm running Chrome Dev version along stable one. If I choose the stable one, I'm not able to use CustomTabsat all. As Google advices, CustomTabs will only work on Chrome 45 and beta versions of Chrome.
Demo from my application: https://youtu.be/fnIZwuJXjHI
Edit: Post
Try this:
gradle dependency:
dependencies {
...
compile 'com.android.support:customtabs:25.1.0'
}
Code :
Uri uri = Uri.parse("https://github.com/mzelzoghbi");
// create an intent builder
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
// Begin customizing
// set toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
// build custom tabs intent
CustomTabsIntent customTabsIntent = intentBuilder.build();
// launch the url
customTabsIntent.launchUrl(activity, uri);
There is demo project on github, mentioned by #NikolaDespotoski, which can be partially reusable.
Solution is based on this article.
Add project shared to your project. Shared is a name of project (I don't know why Google didn't add it into customtabs library). link to shared project
Copy Activity helper from demo project to your project and put correct package. CustomTabActivityHelper
To pre-fetch url use CustomTabActivityHelper#mayLaunchUrl method (if needed) and CustomTabActivityHelper#openCustomTab to open Chrome custom tab.
For instance openning custom tab:
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, uri,
new CustomTabActivityHelper.CustomTabFallback() {
#Override
public void openUri(Activity activity, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Pre-fetching of url is more complicated. You can see this demo for better understanding.

AdMob: display advertisement in WebView

I use AdMob service to display advertisement in my android app.
I need to display advertisement using WebView instead of opening external web browser for it.
Overriding onLeaveApplication() method in AdListener doesn't help, external application is opened anyways after running method code.
Any solution?
you mean when you click on your ad it opens in a separate browser window? you would like it open inside the same webview?
then you just need to add a shouldoverrideurlloading. something like this:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle it
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
EDIT: sorry, this is only for the actual webview you are using in your app. since admob ads are displayed in their own little banner (which is essentially a webview too) it doesnt make sense to load that webpage into the banner. therefore a browser window has to pop up. may i ask why that is so important to you? I dont think it can be easily realized, meaning its not really possible.

Android: Need Mopub Ad to Open in WebView when clicked on

I am curious how to load a webview when clicking on a mopub ad. By default the ad is opening a browser. There is very little documentation on this and I haven't been able to find any examples.
I instantiate the AdView class that I found in the mopub api as it seems like it should do what I want but to no avail. And I'm still not sure how to switch the ad click from browser to webview.
Also, getClickThroughUrl() returns null. Mopub is storing the url though because the page opens in a browser. So how do I properly retrieve the URL?
Here's my code:
mAdView.setAdUnitId(MOPUB_SALES_LIST_ID);
mAdView.loadAd();
mAdView.setOnAdLoadedListener(new OnAdLoadedListener() {
public void OnAdLoaded(MoPubView mpv) {
adPopupLayout.setVisibility(View.VISIBLE);
}
});
mAdView.setOnAdClickedListener(new OnAdClickedListener() {
#Override
public void OnAdClicked(MoPubView m) {
AdView adview = new AdView(getApplicationContext(), mAdView);
adview.loadUrl(m.getClickthroughUrl());
}
});
This is not very easy. You will have to make changes in the mopub SDK.
In the AdView class, you need to change the showBrowserForUrl() to load a webview instead of browser.
Since you have the URL that you need to go to. You could probably make yourself a WebView and call its load method passing along the URL, instead of calling load on the adview, which is probably just sending out the URL as an ACTION_VIEW intent, and the browser is set to listen for these when the content is a URL. If you install a 3rd party browser (firefox, opera, dolphin etc) and when you click the ad if you are given a list of options that includes all of the browsers installed you could verify that it is using the intent that way.

Android : how to stream/play any music file on HTML page in android browser

There is one HTML page, streaming a music file and It's working on window but i need to run this html file on android.
also i need to control all the feature of music by javascript function e.g. play,pause,stop, volume up & down.
Can someone give me any idea?
Note : i am new to android but good hand in java. Just going through android tutorial and i came to know this feature can be done using webview. is it ?
Its not clear from the post if its a requirement that you have to play it on a html page or you just want to play the .mp3 file linked from some online page.
However, You can look at this Streaming Audio tutorial as an initial reference. The MediaPlayer API would be your goto place in android for anything to do with Audio/ Video. WebView is mainly for embedding a browser view within your android App.
Here is an example quite close to the thing you're going to do...
public class MyJSInterface{
private MediaPlayer mp = new MediaPlayer();
....
public void play(String url){
this.mp.setDataSource(url);
this.mp.prepare();
this.mp.start();
// AlterDialog etc.
}
public void stop(){
this.mp.stop();
}
....
}
HTML from your website:
function _play(url){
window.myappname.play(url);
}
function _stop(url){
window.myappname.stop();
}
thing you may need
Android Media

Categories

Resources