I am developing an Android application using PhoneGap.
My application is launched with a custom URI with something like myscheme://mydata.
I can get the data in my activity class as follows.
Intent intent = getIntent();
if(intent.getAction().equals(Intent.ACTION_VIEW))
{
Uri data = intent.getData();
if (data != null)
data.toString();
}
However I couldn't find any way to pass this data onto my javascript code on phonegap side.
Any help is appreciated,
Regards
You might want to look at the WebIntent plugin - https://github.com/phonegap/phonegap-plugins/tree/master/Android/WebIntent in the PhoneGap Plugins repo https://github.com/phonegap/phonegap-plugins.
Have you tried checking window.Invoke_params in your javascript?
The following link seems to indicate that it should work on iOS at least
http://remysharp.com/2010/09/01/custom-url-schemes-in-phonegap/
For passing a data from android to javascript using phonegap
You can try to make a plugin
and via exceute() method you can pass the data.
Related
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.
I have a problem now regarding about the json that can't be read in my android..
the json file is where my data is place..it act as an static database..
I can get it with my Desktop but when it come to my mobile it didn't show..
Here is my sample code:
Here is my Services to get my json file..
var serviceUrl = '/';
$http.get(serviceUrl + 'JSON/Books.json').success(function (results) {
$scope.New = results;
});
Please help me to solve this problem.. my idea about the problem is the serviceUrl. Any idea about it. Thank you so much..
Im definitely a beginner for this Ionic Framework.
To all who still in this problem I just find something that solve it. I don't know if it will solve in your problem but it really solve in me.. I use file:///android_asset/www/ as my serviceUrl
So this is an example:
var serviceUrl = 'file:///android_asset/www/';
$http.get(serviceUrl + 'JSON/Books.json').success(function (results) {
$scope.New = results;
});
Just try it and explore to it.. i just tried that one and it worked in me..
Maybe the reason is the all the json file in your apk installer will be placed in file:///android_asset/www/json directory in android phone so let your url point to that directory..
I hope it will help you and explore in it..that might not be the correct answer but i hope it will help you find the hint.
Thank you
Starting the serviceUrl with a '/' makes it an absolute URL. It will work in chrome since the root is the www folder. But in cordova mobile environment it will translate to file:///.
Simply add a '.' ('./') to make it a relative path and it will work in both android and ios environments.
another easy way is to turn your json file into a javascript file, (for static files if you want to update use something like pouchdb).
eg save the json document as myjsondata.js
var mynamespace = mynamespace || {};
mynamespace.data = [{"foo":"bar"}];
then reference the javascript file in you main page where you load all the other js files
<script src="js/myjsondata.js"></script>
then in your service you can just access the json with mynamespave.data
Based on #Datz Me answer, I've found it's way easier if you treat your application as a web server (which it is) and request your file as being served by it instead of trying to figure out how to manage the different file paths between the several builds.
Here is what I did.
I've placed my json file on
www/json/app.json
Inside it I've put
{
"title": "Application Title",
"icon" : "custom-icon.png"
}
And in my app controller I used the following code to read the properties:
$http.get('json/app.json').success(function (results) {
$rootScope.title = results.title;
$rootScope.icon = results.icon;
});
And in all my child controllers, I just need to add $rootScope as a dependency and I'm able to use
{{title}} //on headings
{{icon}} //to display the image path
<img src="{{icon}}"/> //to display the icon
In my case, it's an app that will be customised for several clients, so I needed a way to quickly change the app's properties and keep them in one place.
I need to launch an andoird app via codename one.
We have already built an app in android.
The second app needs to be built in CN1.
I need to launch the first app using a button in the second app.
I did stumble across NativeInterface, however, did not find any examples to achieve the above.
Also, I need to create a pdf file using CN1. How do I achieve that ?
Any help would be appreciated.
Thanks,
Sanket
Regarding launching an app from a CodenameOne project you can do the following:
1)Use Display.getInstance().extecute(intentUri);
where the intent uri need to comply with the android intent convention it should look something like this: intent:#Intent;action=...
2)Create a NativeInterface then in the android implementation create your intent and execute it.
Regarding pdf generate, there isn't such api in codenameone, personally I would recommend on sending the data to the server and do the generation on the server side.
I can't find the intentUri in Android. How look the intentUri? I would execute Skype if i clilck on a Button but Skype can't be executed.
My Code:
#Override
protected void onKomm_Button2Action(Component c, ActionEvent event) {
try {
Display.getInstance().execute("com.skype.raider");
} catch (Exception e) {
Dialog.show("Error!", "Failed to start. Skype installed?", "OK", null);
e.printStackTrace();
}
}
How should like look my Code?
Thanks.
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.
How can I embed a PDF viewer for a phonegap application? I have decided to use PhoneGap + Sencha Touch to develop an application for iOS and Android.
I only have iOS phonegap experience. The solution that has worked for me is to make a plugin that pops up a native webview that uses iOS native pdf viewer. The way to get this to work is to follow the instructions on this website.
http://spin.atomicobject.com/2010/12/07/updating-phonegap-s-childbrowser-plugin-to-handle-local-files/
This link modifies an existing plugin, "Child browser" to use the native webview display pdf's. The original chilbrowser plugin can be found here.
To give you more of an idea of what it will be like, here is my particular javascript call that I put into my sencha application.
PhoneGap.exec("ChildBrowserCommand.showFilePage", GlobalVar.localRoot + "/" + record.get("FilePath"));
This is inside the handler for the buttonTap inside the sencha, pressing the button will then call the objective C method "showFilePage". The parameter is the filepath that the plugin will use.
Here is the Objective C part (again, you should follow the links for full instructions)
- (void) showFilePage:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options // args: url
{
NSLog(#"showFilePage entered: ");
if(childBrowser == NULL)
{
childBrowser = [[ ChildBrowserViewController alloc ] initWithScale:FALSE ];
childBrowser.delegate = self;
}
PhoneGapViewController* cont = (PhoneGapViewController*)[ super appViewController ];
childBrowser.supportedOrientations = cont.supportedOrientations;
[ cont presentModalViewController:childBrowser animated:YES ];
NSString *path = (NSString*) [arguments objectAtIndex:0];
NSLog(#"Our argument 0 is: %#",[arguments objectAtIndex:0]);
NSLog(#"The url is: %#",path);
[childBrowser loadFileURL:path];
}
There's not much documentation for mixing PhoneGap and ObjC, but here is some example code that lets you embed PhoneGap with an ObjectiveC-Application. For the PDF viewer, you can either use basic ones like Apple's QuickLook or UIWebView, or more advanced ones like PSPDFKit.
For Android, you could simply search for an Intent that is capable of displaying pdf (like Adobe's official Reader for Android) or integrate a full PDF viewer yourself. There's an open source project for that, but it looks not quite complete. Or check out apv, or droidreader, which is GPLv3.
Hi just use HTML5 object tag, you can add,swf, pdf etc..