Allow mock Locations #Android - java

I have been trying to use this feature available under the developer options on a android device. i am a tester so trying to fake a location on android device. I have already given permission ACCESS_MOCK_LOCATION under android manifest permissions. Not sure what and where should i put my fake longitude and latitude in my code. does anyone have tried it before? I am new to code and a tester so don't have idea what code should i write for fake long and lat and where should i put it. I have the source code for my test app and its a health app with maps on the home screen.

After instantiating your LocationClient you can call locationclient.setMockMode(true);
After that you can have your code generate Location objects like so
public Location createLocation(double lat, double lng, float accuracy) {
// Create a new Location
Location newLocation = new Location(PROVIDER);
newLocation.setLatitude(lat);
newLocation.setLongitude(lng);
newLocation.setAccuracy(accuracy);
return newLocation;
}
After that you could do something like
Location testLocation = createLocation(12.34, 45.679, 9.0f);
and
locationClient.setMockLocation(testLocation);
(This is taken from here)
This blog describes how to use mock locations when debugging and turning them off when not debugging.

Related

"Access denied finding property 'camera.hal1.packagelist'" error thrown when open QR Scanner Intent on LG V30?

Coming from this question, here
I am currently developing an application that needs to scan QR Codes via the Device's built-in camera, using the following libraries:
implementation 'com.google.zxing:core:3.3.3'
implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
The logic I am using to scan the QR Code is as follows, which has been taken from this tutorial:
IntentIntegrator integrator = new IntentIntegrator(QR_Activity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
This code is being called inside a Button using View.OnClickListener(), and the result is being handled by onActivityResult
Whenever I run this code and get the result back from the scanner, the scanner returns nothing, and a exception is written to the console, namely Access denied finding property "camera.hal1.packagelist". Permissions have been successfully granted to the application. The permissions in question are the CAMERA and INTERNET permissions.
The test device in question is a LG V30+ ThinQ Smartphone.
Is there a way to get around this issue? Or is this problem prevalent on all smartphones that have a dual camera setup, and therefore 'unfixable'?
UPDATE
Conducting a simple test using the following code:
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
(The code has been taken from this answer from the above post.)
The above code yields the same results as the IntentIntegrator code shown above, and throws the same Access denied finding property "camera.hal1.packagelist" error as before.
After a lot of deliberation and a lot of experimentation, I have found a solution to this problem. The solution seems to come from a new declaration of the IntentIntegrator Class every time the QR Scanning Button is clicked. By this, I mean that the access to the camera is granted using the following line
new IntentIntegrator(MyActivity.this).initiateScan();
From my understanding, declaring the optional parameters of the object's instantiation explicitly (i.e. setBeepEnabled and so on) is causing the problem, and therefore denies the application permission to actually use the camera, even though permissions to the hardware have already been granted beforehand.
Building on this, the optional parameters should be declared and set within the IntentIntegrator declaration, and not explicitly on different lines. This way, the application still retains access to the camera hardware, but can now cater for other optional parameters and features. By this, I mean the parameters should be specified as the following:
new IntentIntegrator(MyActivity.this).setBeepEnabled(false).initiateScan();
This fix appears to work on all dual camera setups

Fusion Tables v2 POIs on Google Maps: Android example?

I'm looking for an example on how to display points of interest (saved in fusion tables) on a google map.
To do this in html and javascript it's quite trivial, check my javascript map with fusion tables example
See Fusion Tables page containing my POIs
My goal/question is how (need help in coding it) to achieve the same in an Android app. I'm new to android development and I already invested hours for the basics and checking documentation and examples.
Check this very good Google Maps example for Android I've found to get started (my test app is based on this code).
Fusion Tables v2 reference (points to google api client)
Google API Java client samples on github (most outdated: examples on v1)
So far I achieved to display a map centered on my last known location and to show a marker on it.
Because I couldn't find good examples for this, I decided to publish and share my findings, see: firepol / android-google-maps-fusion-tables on github
Now I'd like to show markers coming from fusion tables.
I'm stuck at executing the request, which I try to do via google api client.
Google API client example for Android
ActivityFeed feed = listActivities.execute();
Here my code (which I've put inside onCreate):
protected void prepareFusion() {
// Normally READONLY should be enough (see credential with one scope), but I checked online a console
// and I could see a public table only if I would grant both permissions
List<String> scopes = new ArrayList<>(Arrays.asList(FusiontablesScopes.FUSIONTABLES, FusiontablesScopes.FUSIONTABLES_READONLY));
credential = GoogleAccountCredential.usingOAuth2(this, scopes);
//credential = GoogleAccountCredential.usingOAuth2(this, Collections.singleton(FusiontablesScopes.FUSIONTABLES_READONLY));
// TODO : get account name automatically
// http://stackoverflow.com/questions/35789071/getting-the-gmail-id-of-the-user-in-android-6-0-marshmallow
credential.setSelectedAccountName("YOUR_GOOGLE_ACCOUNT");
client = new Fusiontables.Builder(
transport, jsonFactory, credential).setApplicationName("TestMap/1.0")
.build();
try {
String tableId = "1774o_WcrqSQlepLXlz1kgH_01NpCJ-6OyId9Pm1J";
Fusiontables.Query.Sql sql = client.query().sql("SELECT FileName,Name,Location FROM " + tableId);
//sql.execute();
//java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
Fusiontables.Table.Get table = client.table().get(tableId);
table.setFields("items(FileName,Name,Location)");
//table.execute();
// TODO : can't execute like this on main thread as the documentation example "suggests"
//https://developers.google.com/api-client-library/java/google-api-java-client/android
} catch (IOException e) {
e.printStackTrace();
}
}
If I try to do the same and call sql.execute() or table.execute() I get:
java.lang.IllegalStateException: Calling this from your main thread
can lead to deadlock
So I'm kinda stuck here and I'd like to know how to proceed from somebody who has experience with the google api client, even better if you can help me to get the result on the map! Thank you.
How to display the fusion tables POIs on the map?
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker to Zurich Oerlikon and move the camera
mMap.addMarker(new MarkerOptions().position(mDefaultLatLng).title("Zurich Oerlikon"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
mDefaultLatLng, 13));
// TODO: add fusion tables POIs
}
To see where I'm stuck and help me, clone my github repo firepol / android-google-maps-fusion-tables on github, open it in Android Studio, add your own Google Maps API Key and debug on your device. Thanks for your constructive comments, answers and help. Feel free to push on github as well.
In android networks calls are never made on UI/main thread.
Try using an async task if you just want to see things working or import a networking library like volley/robospice if you are developing a full project.
This commit implements the fusion tables query and shows the resulting POIs on google maps
Explanation: GoogleAccountCredential was wrong and had to be replaced with GoogleCredential. To make this work you need to create a service account, role project > service account actor (generate a private key), download the json file and place it under app/res/raw/service_account_credentials.json (in my commit I refer to this file with this precise name, but feel free to raname it and adapt the code to your needs).
Make sure to enable the Fusion Tables API in the API Manager for your project.
I implemented also a class deriving from the AsyncTask to solve the problem of the main thread.
There is some little refactoring in the commit (Location changed to LatLng) but that's it.
Whoever needs to make an android app and place fusion tables POIs on a google map can clone the github repo and have something to begin with (which was also the initial idea of my question).

How to make my app receive location intents like Google Maps

If you opened a link like this on your android phone Google maps will automatically open and show you the location on it's map.
I want to make my app receive intents if the user opened a link like this and also i want to get the lat,long from that link.
Is this Possible ?
Assuming the link is stored as String variable link
Now,
String[] split_link = link.split(Pattern.quote("#"));
String[] post_link = split_link[1].split(Pattern.quote(","));
double lattitude = Double.parseDouble(post_link[0]);
double longitude = Double.parseDouble(post_link[1]);
Try This!!

Android Studio Offline Map/Navigation

I'll be doing an app for my thesis. My thesis is a Offline Map which includes landmarks/routing. My thesis Maps is only for my City here in the philippines. I tried to use OSMDroid and failed to produce the wanted result.
What I want is (if possible) I want this to happen in my app:
Install APK
Prompt the user which country he/she wants to use
Download the chosen country for offline use.
Done
Here's my code in OSMDroid
public class MainActivity extends Activity {
public static final GeoPoint myCity = new GeoPoint(14.54321,120.23451);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.map);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapView.setUseDataConnection(true);
mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
IMapController mapViewController = mapView.getController();
mapViewController.setZoom(13);
mapViewController.setCenter(myCity);
}
}
Here's what I've done:
I Use Mobac to a portion of the map
Save it to my Internal Phone Memory(i named it MapquestOSM)
Run the program
But when I run the program it's just Blank.
I Don't Mind using OSMDroid again as long as it will produce the same result.
I'm willing to bet you didn't do anything to tell osmdroid that you want a different map source. Here's a few pointers to help
if you use mobac to create a zip file, it source names have to match. i.e. if you get Mapnik tiles, then use the Mapnik tile source in osmdroid. Mapquest tiles, mapquest source.
zip files have an upper limit in android, use sqlite instead (also head the first point).
tell the MapView you want offline maps (setUseDataconnection(false);)
If you still have issues (and are using zip files) try altering the contents of the zip to have the first directory = Mapnik. Then using the default tile source in osmdroid.
Until my PR gets merged (to switch tile providers at runtime) and a new release cut, that's really your only option.
Edit: osmdroid only looks in /sdcard/osmdroid/ for map tiles.

How To Show Current Location in URL Custom Google Map by Android Eclipse Project

I Have a Create Android Application With Eclipse , But I Have a Problem ...
I Want to go my custom google map with URl and see device's current location in my map on the device
For Example :
I have a button in one of pages on app , when touch button in the page go to the my custom google map (URL) and show the device's current location.
tanks for your help
Get current Latitude and Longitude using LocationManager.
simply replace the values in the following string with my start and current Lng/Lat
http://maps.google.com/maps?saddr=START_ADD&daddr=DEST_ADD&ll=START_ADD
Then launch the url like this in android, which will let the user choose to either use the browser or their native maps app:
String url = "http://maps.google.com/maps?saddr=START_ADD&daddr=DEST_ADD&ll=START_ADD";
startActivity( new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url)));

Categories

Resources