You might be in:
(unknown)
latitude and also
(unknown)
longitude.
Wednesday, February 26, 2014
Turn about is Fair Play
Turn about is Fair Play is usually Fair Participate in
Since we now have witnessed precisely how Javascript can contact in Coffee, it would be great if
Coffee might in some way contact out and about to Javascript. In our instance, it would be
valuable if we could reveal automated area updates on the Web page, so
it may proactively revise the positioning since the user movements, in lieu of wait
for a go through the "Update Location" website link.
Well, seeing that good luck could have it, we could accomplish that too. This is the a valuable thing,
usually, this particular would be a truly vulnerable portion of the actual guide.
What exactly is unconventional is usually how you will contact out and about to Javascript. 1 could possibly visualize right now there can be a great Turn about is Fair Play
executeJavascript()
comparable version to
addJavascriptInterface()
,
where you could source a number of Javascript origin and possess it performed inside of
the actual wording with the currently-loaded Web page.
Oddly enough, that's not precisely how that is accomplished.
6
Signed up
to
updates
in
http: //commonsware. com
Specific
Resourceful
Commons
BY-SA
3. 0
License
Model
WebView, Interior and also Away
Rather, given ones snippet involving Javascript origin to perform, an individual contact
loadUrl()
on the
WebView
, just like you were being likely to fill some sort of Web page, but you
put
javascript:
in front of ones value and also use that will since the "address" to fill.
If you've ever designed some sort of "bookmarklet" for a desktop Internet browser, an individual
will certainly understand this method as being the Operating system analogue – the actual
javascript:
prefix explains to the actual visitor to treat the rest of the handle seeing that
Javascript origin, being injected into your currently-viewed Web page.
Consequently, provided using this type of capacity, allow us to adjust the previous instance to
continually revise each of our situation on the web site.
The actual structure just for this new task (
WebView/GeoWeb2
) is equivalent to before. The actual
Coffee origin for the activity alterations a little:
open
course
GeoWebTwo
provides
Exercise
{
private
static
String
PROVIDER
=
"gps"
;
private
WebView
browser
;
private
LocationManager
myLocationManager
=
null
;
@Override
public
void
onCreate
(
Bundle
icicle
)
{
super
.
onCreate
(
icicle
);
setContentView
(
R
.
layout
.
main
);
browser
=(
WebView
)
findViewById
(
R
.
id
.
webkit
);
myLocationManager
=(
LocationManager
)
getSystemService
(
Context
.
LOCATION_SERVICE
);
browser
.
getSettings
().
setJavaScriptEnabled
(
true
);
browser
.
addJavascriptInterface
(
new
Locater
(),
"locater"
);
browser
.
loadUrl
(
"file:///android_asset/geoweb2.html"
);
}
@Override
public
void
onResume
()
{
super
.
onResume
();
myLocationManager
.
requestLocationUpdates
(
PROVIDER
,
0
,
0
,
onLocationChange
);
}
@Override
public
void
onPause
()
{
super
.
onPause
();
myLocationManager
.
removeUpdates
(
onLocationChange
);
}
7
Subscribe
to
updates
at
http://commonsware.com
Special
Creative
Commons
BY-SA
3.0
License
Edition
WebView, Inside and Out
LocationListener
onLocationChange
=
new
LocationListener
()
{
public
void
onLocationChanged
(
Location
location
)
{
StringBuilder
buf
=
new
StringBuilder
(
"javascript:whereami("
);
buf
.
append
(
String
.
valueOf
(
location
.
getLatitude
()));
buf
.
append
(
","
);
buf
.
append
(
String
.
valueOf
(
location
.
getLongitude
()));
buf
.
append
(
")"
);
browser
.
loadUrl
(
buf
.
toString
());
}
public
void
onProviderDisabled
(
String
provider
)
{
// required for interface, not used
}
public
void
onProviderEnabled
(
String
provider
)
{
// required for interface, not used
}
public
void
onStatusChanged
(
String
provider
,
int
status
,
Bundle
extras
)
{
// required for interface, not used
}
}
;
public
class
Locater
{
public
double
getLatitude
()
{
Location
loc
=
myLocationManager
.
getLastKnownLocation
(
PROVIDER
);
if
(
loc
==
null
)
{
return
(
0
);
}
return
(
loc
.
getLatitude
());
}
public
double
getLongitude
()
{
Location
loc
=
myLocationManager
.
getLastKnownLocation
(
PROVIDER
);
if
(
loc
==
null
)
{
return
(
0
);
}
return
(
loc
.
getLongitude
());
}
}
}
Previous to, the actual
onLocationChanged()
means of each of our
LocationListener
callback
do absolutely nothing. Right now, it builds some sort of contact to your
whereami()
Javascript purpose,
delivering the actual latitude and also longitude seeing that guidelines fot it contact. Consequently, for
8
Signed up
to
updates
in
http: //commonsware. com
Specific
Resourceful
Commons
BY-SA
3. 0
License
Model
WebView, Interior and also Away
instance, if each of our area were being 45 levels latitude and also -75 levels longitude,
the email can be
whereami(40, -75)
. Subsequently, it places
javascript:
in front of it
and also message or calls
loadUrl()
around the
WebView
. The result is usually that the
whereami()
purpose
within the Web page will get referred to as while using new area.
That Web page, needless to say, likewise desired hook version, to accommodate
the option of needing the positioning be handed down with:
Operating system GeoWebTwo Test
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment