javascript callback in phonegap - android

An API call
<script type="text/javascript"
src="http://api.ipayy.com/v001/c/dc/getjsbykey?gh="xyz">
</script>
returns
window.onload = function() {
callback_fn.call(new Object(), false);
}
This works perfectly fine with browser.
But the same doesn't work with Phonegap, the callback function isn't executed in Javascript in Phonegap
<!DOCTYPE HTML>
<html>
<head>
<script src="cordova.js"></script>
<script>
function callback_fn() {
navigator.notification.alert("new func");
for (var i = 0; i < arguments.length; i++) {
alert(arguments[i]);
}
}
</script>
<script type="text/javascript" src="http://api.ipayy.com/v001/c/dc/getjsbykey?gh="+window.Encryption.encrypt()"></script>
</head>
<body>
</body>
</html>
Is there any equivalent of the following in Phonegap, which will call the function callback_fn()?
window.onload = function() {
callback_fn.call(new Object(), false);
}

Related

Why is my phonegap camera app not working?

First the code:
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource,
destinationType
document.addEventListener("deviceready",loaded,false);
function loaded() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
function getPhoto(imageData) {
var smallImage = document.getElementById('smallImage');
smallImage.style.display = 'block';
smallImage.src = "data:image/jpeg;base64," + imageData;
}
function capturePhoto() {
navigator.camera.getPicture(getPhoto, onFail, { quality: 50 });
}
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
</body>
</html>
I use BuildTool in combination with phonegap (build.phonegap.com). I have zipped this file as index.zip and uploaded this file in BuildTool. BuildTool then creates an app for me and store this app on phonegap.
When I click the button, nothing happens. I won´t even see the failed alert.
Does someone know what I'm doing wrong here? Do I need to include some library or something?
Note: I am trying to get this to work for Android.
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
the above two line is not complete. it should be like these
pictureSource=navigator.camera.PictureSourceType.PHOTOLIBRARY;
destinationType=navigator.camera.DestinationType.FILE_URI;
after changing still not working then try these html its working...
<!DOCTYPE html>
<html>
<head>
<title>Submit form</title>
<script type="text/javascript" src="js/jquery-2.0.2.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
function getPhoto() {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFailure, {
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
}
function onPhotoURISuccess(imageURI) {
$("#smallImage").attr('src',imageURI);
}
function onFailure()
{
alert('failed');
}
</script>
</head>
<body>
<br><br><br>
<button onclick="getPhoto();">Select Photo:</button><br>
<img style="width:200px;height:200px;" id="smallImage" />
</body>
</html>
Try adding the deviceready listener to a function that is called after the body loads, like this:
<body onload="onLoad()">
function onLoad() {
document.addEventListener("deviceready",loaded,false);
}

html2canvas with phonegap error

Getting error
"11-13 13:10:55.470: E/Web Console(9799):
Uncaught Error: TYPE_MISMATCH_ERR: DOM Exception 17 at
file:///android_asset/www/index.html:304"
when converting html div content to canvas in android with phonegap.
It is working properly on browser. Any help will be appreciated. I am using jQuery-1.9.1, jQuery-UI.
code snippet:
// html file
<html>
<head>
<meta charset="utf-8" />
<title>jQuery UI Draggable - Default functionality</title>
<link rel="stylesheet" href="resourcess/css/jquery-ui.css" />
<script src="js/jquery-1.9.1.js"></script>
<script src="js/html2canvas.js"></script>
<script src="js/jquery.plugin.html2canvas.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/jquery.ui.touch-punch.min.js"></script>
<script src="cordova-2.5.0.js"></script>
<link rel="stylesheet" href="resourcess/css/style.css" />
</head>
<body>
<div class="imag-container">
<div class="dragImg">
<div id="dropHere"></div>
<div id="click" > click </div>
<div id="img-check">check</div>
<canvas id="canvas" width="100" height="100">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
</body>
// script
$(function(){
//Make every clone image unique.
var counts = [0];
var resizeOpts = {
handles: "all" ,autoHide:true
};
$(".dragImg").draggable({
helper: "clone",
//Create counter
start: function() { counts[0]++; }
});
$("#dropHere").droppable({
drop: function(e, ui){
if(ui.draggable.hasClass("dragImg")) {
$(this).append($(ui.helper).clone());
//Pointing to the dragImg class in dropHere and add new class.
$("#dropHere .dragImg").addClass("item-"+counts[0] );
$("#dropHere .img").addClass("imgSize-"+counts[0]);
//Remove the current class (ui-draggable and dragImg)
$("#dropHere .item-"+counts[0]).removeClass("dragImg ui-ggable ui-draggable-dragging");
$(".item-"+counts[0]).dblclick(function() {
$(this).remove();
});
make_draggable($(".item-"+counts[0]));
$(".imgSize-"+counts[0]).resizable(resizeOpts);
}
}
});
var zIndex = 0;
function make_draggable(elements)
{
elements.draggable({
containment:'parent',
start:function(e,ui){ ui.helper.css('z-index',++zIndex); },
stop:function(e,ui){
}
});
}
$('#click').click(function(){
//Some code
var domElement = document.getElementById('dropHere');
setTimeout(function() {
var canvas = document.getElementById("canvas"),
context = canvas.getContext('2d');
html2canvas(domElement, {
onrendered: function (domElementCanvas) {
var canvas = document.getElementById("canvas");
canvas.getContext('2d').drawImage(domElementCanvas, 0, 0, 50, 50,0,0,100,100);
}
});
}, 10000);
});
});

iScroll4 with long list not response on Samsung galaxy2 and 3

I have a PhoneGap application with iScroll4, its basically iScroll example with 2000 list items.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>iScroll demo: iOS Perfect Scrollbar</title>
<link rel="stylesheet" type="text/css" href="css/general.css">
<link rel="stylesheet" type="text/css" href="css/scrollbar.css">
<script src="js/jquery-1.7.1.js"></script>
<script src="js/iscroll.js"></script>
<script src="js/alldata.js"></script>
<script type="text/javascript">
var myScroll;
function loaded() {
myScroll = new iScroll('wrapper',
{
scrollbarClass: 'myScrollbar',
hideScrollbar:false,
/*onBeforeScrollStart: function (e) { return false; }*/
});
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);
</script>
<script>
function initList(){
for(var i=0; i<allData.length; i++){
var line = "<li style='color:#"+ allData[i].Color + ";'>" + allData[i].EventName + "</li>"
$("#thelist").append(line);
}
}
$(document).ready(function(){
initList();
initList();
initList();
initList();
//alert("Finished!");
});
</script>
</head>
<body>
<div id="header">iScroll</div>
<div id="wrapper">
<div id="scroller">
<ul id="thelist">
</ul>
</div>
</div>
<div id="footer"></div>
</body>
</html>
Note: the variable allData is a JSON array with 500 objects.
EDITED:
It works fine on Motorola Atrix (2.3.3) and on Galaxy 3,
But it doesn't move on Galaxy 2 (4.2.2) and Galaxy3 after 1000 list items, strange Motorola Atrix is has lower Android version and it's web-toolkit is slower
Is there any way to resolve that problem?
After the data is loaded you just need to refresh the list.
function initList(){
for(var i=0; i<allData.length; i++){
var line = "<li style='color:#"+ allData[i].Color + ";'>" + allData[i].EventName + "</li>"
$("#thelist").append(line);
}
myScroll.refresh();
}

Open Play Store with PhoneGap

I'm developing a very simple webapp with Phonegapp, and I want to open the Play Store APP (Android), when the user clicks on a specific button. I read some q%a but I don't know how I can do it
I have a index.html like this:
<!DOCTYPE html>
<html>
<head>
<title>Just a development test ->dany_danay</title>
<link href="jquery-mobile/jquery.mobile-1.0a3.min.css" rel="stylesheet" type="text/css"/>
<script src="jquery-mobile/jquery-1.5.min.js" type="text/javascript"></script>
<script src="jquery-mobile/jquery.mobile-1.0a3.min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var watchID = null;
var pictureSource; // Origen de la imagen
var destinationType; // Formato del valor retornado
// Espera a que PhoneGap conecte con el dispositivo.
//
document.addEventListener("deviceready",onDeviceReady,false);
function alertDismissed() {
// do something
}
</script>
</head>
<body>
<div data-role="page" id="page">
<div data-role="header">
<h1>Your SmartPhone info</h1>
</div>
<center><a href="https://play.google.com/store/apps/details?id=com.tusmartphone.dany&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS50dXNtYXJ0cGhvbmUuZGFueSJd" rel="external" /><img src="http://media.go2speed.org/brand/files/offermobi/1981/20130605194659-WARFAL_b01_320X50_04.png"/></a>
</center>
</body>
</html>
I tried adding rel="external" or rel="system" in the
and this js function:
<script type="text/javascript">
$(function() {
updateAndroidMarketLinks();
// some more core here ...
function updateAndroidMarketLinks()
{
var ua = navigator.userAgent.toLowerCase();
if (0 <= ua.indexOf("android")) {
// we have android
$("a[href^='http://play.google.com/']").each(function() {
this.href = this.href.replace(/^http:\/\/play\.google\.com\//,
"market://");
});
}
}
});
</script>
I have to call a intent, but not sure how i can do this with html or js...
Dont know how to prove this: https://stackoverflow.com/questions/15325864/phonegap-android-open-play-store or this: How to Open Google Play store from Phonegap http://tannerburson.com/blog/2012/05/28/IntentChooser-my-first-PhoneGap-Cordova-plugin/
You want to use market links.
market://details?id=<package_name>
http://developer.android.com/distribute/googleplay/promote/linking.html

open a notification in webview

I am working on an application and i want that my notification should be opened in a webview please help.
or
I want that my application should respond to that notification.
just start intent calling an activity which will open your url in webView
Maybe the new InAppBrowser function in Cordova 2.3.0 (newest version) is something you looking for.
http://docs.phonegap.com/en/2.3.0/cordova_inappbrowser_inappbrowser.md.html#InAppBrowser
<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" />
Example:
<!DOCTYPE html>
<html>
<head>
<title>InAppBrowser.addEventListener Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
var ref = window.open('http://apache.org', '_blank', 'location=yes');
ref.addEventListener('loadstart', function() { alert('start: ' + event.url); });
ref.addEventListener('loadstop', function() { alert('stop: ' + event.url); });
ref.addEventListener('exit', function() { alert(event.type); });
}
</script>
</head>
<body>
</body>
</html>

Categories

Resources