Android Custom WebView How to use GPS with analog GPS

Source: Internet
Author: User

1 How to fake GPS?

GPS fake There's a strange phenomenon you need to put fake GPS code into a service without knowing if the system is the source of the fake information. The results of the experiment are required to be placed in the service

The code is roughly as follows

Package Com.yiqiding.ktvbox.view.service;import Java.lang.reflect.method;import Android.app.service;import Android.content.intent;import Android.location.location;import Android.location.locationmanager;import Android.os.bundle;import Android.os.handler;import Android.os.ibinder;import Android.util.log;import Com.yiqiding.ktvbox.util.logutil;public class Gpsfakeservice extends Service {private static final String Log_tag = "GPSF Akeservice ";p rivate float accuracy;private double altitude;private float bearing;private Bundle Bl;private boolean Forflag = true;private Handler Handler = new Handler ();p rivate double lat;private double lng;private Locationmanager Mloca Tionmanager;private Runnable Runnable = new Runnable () {public void run () {try {Mlocationmanager.sendextracommand ("GPs", "Force_xtra_injection", BL); Mlocationmanager.sendextracommand ("GPs", "force_time_injection", BL); Location locallocation = Getloc ("GPs"); Mlocationmanager.settestproviderlocation ("GPs", locallocation); LOGUTIL.V ("seT localcation "+ locallocation); handler.postdelayed (this, 1000L);} catch (Exception Exception) {exception.printstacktrace ();}}}; Private float speed;private location Getloc (String paramstring) {Location locallocation = new location (paramstring); Locallocation.setlatitude (LAT); Locallocation.setlongitude (LNG); Locallocation.setaltitude (altitude); Locallocation.setbearing (bearing); Locallocation.setspeed (speed); locallocation.setaccuracy (accuracy); Locallocation.settime (System.currenttimemillis ()); try {method = Location.class.getMethod ("Makecomplete"); if (Method! = null) {Method.invoke (locallocation);}} catch (Nosuchmethodexception e) {e.printstacktrace ();} catch (Exception e) {e.printstacktrace ();} return locallocation;} private void Removeprovider () {try {mlocationmanager.removetestprovider ("GPs");} catch (Exception Exception) {LOG.E ( Log_tag, Exception.getmessage ());}} Public IBinder Onbind (Intent paramintent) {return null;} public void OnCreate () {super.oncreate ();} public void OnDestroy (){Super.ondestroy (); Removeprovider (); try {handler.removecallbacks (runnable);} catch (Exception Exception) { Exception.printstacktrace ();}} public void OnStart (Intent paramintent, int paramint) {Super.onstart (paramintent, paramint);} public int Onstartcommand (Intent paramintent, int paramInt1, int paramInt2) {logutil.i ("would fetch Locationmanager then SE T location "), Mlocationmanager = ((Locationmanager) getsystemservice (" location "); Mlocationmanager.addtestprovider ( "GPs", false, False, False,false, False, False, False, 0, 0); mlocationmanager.settestproviderenabled ("GPs", true); bl = PA Ramintent.getextras (); if (bl! = null) {if (Bl.containskey ("lat")) lat = Paramintent.getdoubleextra ("lat", 0.0D); Bl.containskey ("LNG")) LNG = Paramintent.getdoubleextra ("LNG", 0.0D); if (Bl.containskey ("accuracy")) accuracy = Paramintent.getfloatextra ("accuracy", 0.0F); handler.postdelayed (runnable, 100L);} return start_redeliver_intent;}}


Then you just need to send him the GPS coordinates to fake.


private void Starttestgps () {logutil.i ("would start Gpsfakeservice"); Intent mintent = new  Intent (this, Gpsfakeservice.class); Mintent.putextra ("lat", 31.12121245); Mintent.putextra ("LNG", 121.124546461); Mintent.putextra ("accuracy", 5.0f), Mintent.putextra ("bearing", 0.0f); Mintent.putextra ("Speed", 10.0f); StartService (mintent);} At the end of the day be careful to close the fake service private void Endtestgps () {logutil.i ("will stop Gpsfakeservice"); Intent mintent = new  Intent (this , Gpsfakeservice.class); StopService (mintent);}


2 How to get a custom WebView to call GPS

WebView Mwebview = (WebView) Dacheviewroot.findviewbyid (r.id.webview1); Mwebview.getsettings (). setjavascriptenabled (true);//enable support for Javascriptmwebview.getsettings (). Setdomstorageenabled (true);// Add this is to solve the problem of parsing error when opening the page mwebview.getsettings (). Setgeolocationenabled (true);//Support Geomwebview.loadurl (" Your need to call the GPS function of the webpage "); Mwebview.setwebchromeclient (new Webchromeclient () {
Add this like you agree to share your location in the browser public void Ongeolocationpermissionsshowprompt (String origin, Geolocationpermissions.callback Callback) {logutil.i ("We allow GEO location permission"); Callback.invoke (origin, True , false);});




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.