I am trying to access Cordova Contacts API to access the device contact details but i am not able to get the details .I have added the reference of Cordova.js and have added below in Conf.xml ..
<feature name="Contacts">
<param name="android-package" value="org.apache.cordova.contacts.ContactManager" />
</feature>
and i have given permission in AndroidManifest.xml file too..
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
Still on running my application in the OnLoad() event function only i am getting this error because of undefined ..Here is the code..
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
$("#searchby_chooser_ok_button").bind ("click", searchByCriteria);
if (typeof Contact === "undefined") {
getElement("contacts_list").innerHTML = "<p>The Cordova Contacts API is inaccessible</p>";
}
}
I tried my level best to search on web in vain only .Is there any specific file that i need to add into the project?..Please help me to resolve this ..Thanks..Here is my project directory structure
Read the following links for phonegap/cordova project.
http://thejackalofjavascript.com/phonegap-3-cli-setup-mac-windows/
http://coenraets.org/blog/cordova-phonegap-3-tutorial/
http://teusink.blogspot.in/2013/07/guide-phonegap-3-android-windows.html
http://sdk.revmobmobileadnetwork.com/phonegap_cordova.html
http://docs.phonegap.com/en/3.5.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide
Phonegap Cordova installation Windows
Related
I'm working on a demo app for android that needs to get information about the phone, the SIM, the network, etc.. I put all the code in a plugin and my Java function that that retrieves the SIM info looks like this:
private boolean getSIMInfo(CallbackContext cbc) throws JSONException {
// tm is a TelephonyManager instantiated in initialize
JSONObject res = new JSONObject()
.put("carrierID", tm.getSimCarrierId())
.put("carrierName", tm.getSimCarrierIdName())
.put("countryIso", tm.getSimCountryIso())
.put("operator", tm.getSimOperator())
.put("operatorName", tm.getSimOperatorName())
.put("state", tm.getSimState())
.put("msisdn", tm.getLine1Number()) // <== Requires permission
;
cbc.success(res);
return true;
}
As long as I dont call getLine1Number() everything goes fine.
getLine1Number() requires either android.permission.READ_PHONE_STATE or a.p.READ_SMS or a.p.READ_PHONE_NUMBERS to be set. I first declared a.p.READ_PHONE_STATE in plugin's plugin.xml and I checked it was injected into AndroidManifest.xml.
The platform part of the plugin.xml looks like this:
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="TelPlugin">
<param name="android-package" value="org.buguigny.CordovaPlugin.TelPlugin"/>
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml" />
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</config-file>
<source-file src="src/TelPlugin.java" target-dir="src/org/buguigny/CordovaPlugin/TelPlugin" />
</platform>`
Upon execution I get the error:
getLine1NumberForDisplay: Neither user 10190 nor current process has
android.permission.READ_PHONE_STATE, android.permission.READ_SMS, or
android.permission.READ_PHONE_NUMBERS
I tried with the other permissions: same error.
Any idea what I'm doing wrong ?
On Android >=6.0, Android app should request permission runtime.
So in Cordova, you can use the permission plugin like following
permissions.requestPermission(permissions.READ_PHONE_STATE, success, error);
function error() {
console.warn('Camera permission is not turned on');
}
function success( status ) {
if( !status.hasPermission ) error();
}
I'd like to set androids permissions to use geolocation in config.xml.
I don't want to use cordova-plugin-geolocation which would set these settings as a side-effect because the webviews I am targeting (crosswalk) support GeoLocation out of the box. The Plugin would just be bloatware.
I don't want to write it directly in AndroidManifest.xml because I am using cordova prepare to prevent having any plattform-specific stuff inside my repository. Everybody is currently able to build the plattforms from scratch without any plattform-specific stuff from our git-repo.
What I tried
I took a look at cordova-plugin-geolocation to see how they would achieve this.
<platform name="android">
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
</config-file>
</platform>
This returns the following in my build-phase
Error: Command failed: /bin/sh -c cordova build android
/Users/sven/.../cordova/platforms/android/build/intermediates/res/armv7/debug/xml/config.xml:34: error: Error parsing XML: unbound prefix
What my next step would be
I am unsure about why the 'rob-from-cordova-plugin-geolocation'-approach does not work, but maybe it would help to just put above into a new plugin? Is it worth a try?
Your error is "unbound prefix", have you provided a definition for the "android" prefix that you're using in "android:name"?
Looking at cordova-plugin-geolocation plugin.xml you may need to add this to your XML:
xmlns:android="http://schemas.android.com/apk/res/android"
So that the android namespace is defined.
You will be able to add permissions (and many other extra configuration actions) in Cordova straight from CLI's config.xml by adding the plugin cordova-custom-config to your environment. It will add nothing in your application and consists only in a group of Cordova hooks.
So, only add the XML chuck in your config.xml (<config-file...), and also add what #SimonPrickett say in his answer.
I am trying to set up a PhoneGap app on Android using the Media object. But no audio is being played on my Android device for some reason.
The source code of the HTML file is:
<input type="button" onclick="playAudio()" value="Play Sound">
<script>
function playAudio() {
var my_media = new Media("http://www.noiseaddicts.com/samples/3721.mp3");
my_media.play();
}
</script>
I have added the plugin and site whitelisting to the config.xml file like this:
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = XXXX
version = "0.1.0" >
<gap:platform name="android" />
<plugin name="Media" value="org.apache.cordova.AudioHandler" />
<access origin="http://www.noiseaddicts.com" />
</widget>
Any idea of what I could have done wrong?
You should attach success and error handlers to be able to debug what is going on.
var my_media = new Media("http://www.noiseaddicts.com/samples/3721.mp3",
// success callback
function () {
alert("playAudio():Audio Success");
},
// error callback
function (err) {
alert("playAudio():Audio Error: " + err);
}
);
my_media.play();
Update: If this is not working try doing the following. In app/res/xml/config.xml:
<feature name="Media">
<param name="android-package" value="org.apache.cordova.media.AudioHandler" />
</feature>
In app/AndroidManifest.xml:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
which is specified here: http://cordova.apache.org/docs/en/3.1.0/cordova_media_media.md.html#Media_accessing_the_feature
The problem was that I did not create the right file structure when I created the project and was missing critical files including the cordova.js file. I started over from the beginning according to cordova documentation and got it working then.
I'm writting an android and iphone app using phonegap 3.0.
So far I have been only compiling the apps remotely using phonegap build.
Now I'm trying to add the geolocation plugin to my app, and in iphone was easy, since I only had to modify the config.xml file.
<feature name="Geolocation">
<param name="ios-package" value="CDVLocation" />
</feature>
So far so good, the problem was that when adding geolocation to android, the documentation indicates that I have set the following configuration:
(in app/res/xml/config.xml)
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>
(in app/AndroidManifest.xml)
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
But I don't have an AndroidManifest.xml file. I could generate the complete android app using:
cordoba platform add android
But I'm not sure if this is what I should do.
Is is what I should be doing? or there is a way to add geolocation without generating the entire android project?
I am a bit confused because you say you are using phonegap 3.0 and you also mention phonegap-build (highest is 2.9). If you are in fact using phonegap-build, the only thing you need is in your config.xml:
<feature name="http://api.phonegap.com/1.0/geolocation"/>
I had the same problem. My geolocation worked on IOS but not on Android. So I Added to config.xml
<feature name="http://api.phonegap.com/1.0/geolocation"/>
As explained by Dom.This was not sufficient. I wanted to share what I did step by step because I lost about 18h of dev time looking for the solution.
This however did not reslove the problem. So I added the plugin using my IDE.
This added the following to the config:
<plugin name="cordova-plugin-geolocation" version="2.2.0" />
I then went inside the plugin.xml under the plugin directorty.
And looked if the following was added.
<!-- android -->
<platform name="android">
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</config-file>
...
It was, so I rebooted the device. (on the Ionic forum they say, it might help)
When reinstalling the app I checked for the permission. And it was added. So for the bad Q of the last pic but I took a pic with my cam for it. Hope this helps
I am working on an Android App with Phonegap Cordova-3.0.0 and when I call InAppBrowser I got MotionEvent mTouchMode = 4 error. And InAppBrowser function is not working. So how can I fix this? Do I need do some setting on AndroidManifest.xml or config.xml?
And I got this on my AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And I got this on my config.xml
<plugins>
<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" />
</plugins>
In Phonegap Cordova-3.0.0 version, for the app to communicate closely with various device-level features, we need to add plugins that provide access to core Cordova APIs.
The cordova plugin add command requires you to specify the repository for the plugin code.
For example, In-app browser:
$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser.git
we need run this in command line. Don't need worries about AndroidManifest.xml or config.xml files. After you run $ cordova build, it will auto write for you.
You could get more idea about it in doc.phonegap
Add following code in config.xml this works fine for mi.
<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" />
<access origin="*" browserOnly="true"/>
You have to mention the below code line in config.xml
<plugin name="InAppBrowser" value="CDVInAppBrowser" />
try to add this to your manifest, it helps me with a f*king plugin to work
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
try also not use 'plugin' tag in config.xml, but:
<feature name="InAppBrowser">
<param name="android-package" value="org.apache.cordova.InAppBrowser"/>
</feature>
it will help for future updates of phonegap
To open a link in browser inside the app without opening external browser
HTML
<input type="button" id="button1" value = "click here"
onclick="window.open('https://example.com','_blank','location=yes','closebuttoncaption = Return');">
Now go into your project folder and open Terminal or Command Prompt (Windows) and type the following command:
cordova plugin add cordova-plugin-inappbrowser --save
It will configure the required files and also add the plugin into your config.xml file.
Open your HTML page where you're trying to open the link, and put this JavaScript.
<script src="cordova.js"></script>
<script type = "text/javascript" charset = "utf-8">
function onLoad(){
document.addEventlistner("deviceready", OnDeviceReady, false);
}
function onDeviceReady(){
}
</script>