Dynamic API KEY for Google Maps on Android - java

We are developing an Android application that uses google maps.
Right now, for development purposes, we're using the key like this:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
And
<string name="google_maps_key">KEY_HERE</string>
For deploying purposes, we will implement a "Bring Your Own Key" approach where each client that buys the product, also put his key to be used.
I know it's weird and somewhat unnecessary, but it is a process from a big company and this decision comes from "the top".
Is there any way that we can have dynamic keys? Like putting a key into a service and consuming it in the app or something like that?
Appreciate any help.

This is not possible in Google API V2 according to the documentation
the API key has to be assigned using the Manifest file.
https://developers.google.com/maps/documentation/android/start#adding_the_api_key_to_your_application
But its possible for MapView.
If you are instantiating a MapView directly from the code, you should pass the Maps API Key in the MapView constructor.
Code:
#Override
protected void onCreate(.....) {
super.onCreate(....);
mMapView = new MapView(this, YOUR_MAP_API_KEY); //pass key to MapView Constructor here
setContentView(mMapView);
// ....
}

Related

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).

Parse core doesn't show any data sent from Android Studio project

This is the same code from
https://parse.com/apps/quickstart#parse_data/mobile/android/native/existing
Everything is done right, Android Studio recognizes Parse objects and everything, but when I run the app, it runs perfectly.. but no data is sent to parse.
Insted appid and userid I actually have real Keys.
Parse.enableLocalDatastore(this);
Parse.initialize(this, "appid", "userid");
ParseObject testObject = new ParseObject("TestObject");
testObject.put("foo", "bar");
testObject.saveInBackground();
has anyone had this problem? I cannot find a solution. I only have one app in Parse, so the Keys must be right.
Ok so on your manifest you should include the name attribute on your application tag like this
<application
android:name="com.packname.MyApplication"
.../application>
where MyApplication is the class that contains app key and id that connects to parse

Offline tile caching using MapBox Android SDK

I have a working iOS prototype using the iOS tile-caching technique as shown below (Objective-C code):
RMTileCache * tileCache = [[RMTileCache alloc] initWithExpiryPeriod:0];
[tileCache setBackgroundCacheDelegate:self];
RMMapboxSource * tileSource = [[RMMapboxSource alloc] initWithMapID:mapID];
[tileCache beginBackgroundCacheForTileSource:tileSource southWest:southWest northEast:northEasth minZoom:minZoom maxZoom:maxZoom];
What this basically does is download the map, cache the tiles permanently and make it possible for the app to run offline in the future. Since we're going through the official payed API, this is of course not violating any of the legal restrictions.
Now I'd like to achieve the same on Android. I have the SDK running in Android Studio and a working project with a remote map using the Map ID, basically this (Android Eclipse layout XML):
<com.mapbox.mapboxsdk.views.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
mapid=“my_map_id" />
This works fine, but the solution has to be completely offline once the caching is done. My question is: is there a Java equivalent of the iOS source code above in the MapBox SDK? I attempted to look in the API, but could not find a solid reference to the tile caching system. And after some painful time trying to get it running based on the method names and code documentation, I gave up.
I'm running the latest GitHub distribution of MapBox along with the latest Android Studio, everything's up and running fine, but can't find the code to accomplish this. I don’t necessarily need an API reference, a few lines of code showing how it’s done would be enough.
Offline Tile Caching support is now available in the Mapbox Android SDK as of version 0.5.1. It was released on 20-December-2014. Here's a basic example of how to get started:
OfflineMapDownloader offlineMapDownloader = OfflineMapDownloader.getOfflineMapDownloader(getActivity());
BoundingBox boundingBox = mapView.getBoundingBox();
CoordinateSpan span = new CoordinateSpan(boundingBox.getLatitudeSpan(), boundingBox.getLongitudeSpan());
CoordinateRegion coordinateRegion = new CoordinateRegion(mapView.getCenter(), span);
offlineMapDownloader.beginDownloadingMapID("MapboxMapID", coordinateRegion, (int) mapView.getZoomLevel(), (int) mapView.getZoomLevel());
To load a previously saved map:
ArrayList<OfflineMapDatabase> offlineMapDatabases = offlineMapDownloader.getMutableOfflineMapDatabases();
OfflineMapDatabase db = offlineMapDatabases.get(0);
OfflineMapTileProvider tp = new OfflineMapTileProvider(getActivity(), db);
offlineMapOverlay = new TilesOverlay(tp);
mapView.addOverlay(offlineMapOverlay);
I asked this question to the support team, here's the answer:
"We don't currently have a release date for the Android SDK or for this feature, because both are in very early stages of development.
--
Tom MacWright
support#mapbox.com"
It's a very good product, I hope we can use it soon in Android.
In your layout file there must be:
<com.mapbox.mapboxsdk.views.MapView
android:id="#+id/yourMapViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
In code where you want to initialize MapView:
File file = new File("your full path to tiles db");
MBTilesLayer mbTilesLayer = new MBTilesLayer(file);
MapView mapView = (MapView) findViewById(R.id.yourMapViewId);
mapView.setTileSource(mbTilesLayer);

Creating an Android Lock Screen App.

How to create a lock-screen app that acts as a lock for android mobile. I did find one, But it was poorly constructed code wise and if I pressed the physical home key, it unlocked, making the application pointless.
I did come across a forum stating some method of blocking home button functionality was removed in Android 4.x
Yet, I have an awesome idea for a lock-screen but no ground to get started. If anyone has any knowledge on the subject, I'd love to hear it.
Thanks all :-)
Yes, it is possible. This is a simple lock screen Source Code from GitHub
Creating an app that works like a lock is no big deal but as you said for Home key issue, I would suggest you go on and develop the App as much as you need and the only final area you would get stuck is the home key control so, try to find some tricky way to get the control of home key and make it as an app launcher for your lock app. It is not very complicated but kinda tricky though. I will post you, if I can find any Home-key access source codes
PS:
Here is the tutorial for accessing Home Key
I found the home key override somewhere. Add these lines in the App Manifest.
Following two lines will do the magic
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
and override this method in your activity
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
Keep in mind that I didn't test these codes or methods, just tried to help you (you might find some drawbacks).
PS: based on the votes I can guarantee that my suggestion is working and you can develop such app with the above help :)

Why do I get "Unable to start service Intent"?

I'm trying get some licensing code from AndroidPit.com working, but I get "Unable to start service Intent". Basically my code looks like this:
Intent licenseIntent = new Intent("de.androidpit.app.services.ILicenseService");
if (mContext.bindService(licenseIntent, this, Context.BIND_AUTO_CREATE))
{
// success
}
else
{
// failure (I get this all the time(
}
I tried passing ILicenseService class explicitly:
Intent licenseIntent = new Intent(mContext, de.androidpit.app.services.ILicenseService.class);
but I still get the same problem.
I managed to get Android Market LVL library working which uses identical code, so I don't really understand why it fails to find "de.androidpit.app.services.ILicenseService", but manages to find "com.android.vending.licensing.ILicensingService".
Most of the answers I found here say that you need to append stuff to your AndroidManifest.xml, but you don't anything for "com.android.vending.licensing.ILicensingService" to work, so I guess I shouldn't need anything "de.androidpit.app.services.ILicenseService" (they both derive from android.os.IInterface).
Thanks in advance.
Most of the answers I found here say that you need to append stuff to your AndroidManifest.xml
Those answers are correct.
but you don't anything for "com.android.vending.licensing.ILicensingService" to work
That is because com.android.vending.licensing.ILicensingService is a remote service, one that is not in your project, but rather in the firmware of the device.
so I guess I shouldn't need anything "de.androidpit.app.services.ILicenseService" (they both derive from android.os.IInterface).
That is flawed reasoning. By your argument, java.util.HashMap does not go in the manifest, and both java.util.HashMap and any implementation of Activity all derive from Object, so therefore you do not need to put your activities in the manifest. If you try this, you will quickly discover that your activities no longer work.
If it is a component (activity, service, content provider, or some implementations of BroadcastReceiver), and the implementation of the component is in your project (directly, via a JAR, via a library project, etc.), you must have an entry in the manifest for it.
Wherever you got the service from should provide you with instructions for adding the service to your manifest, and they should also supply you with instructions for creating the Intent used to bind to it. If they do not provide this documentation, perhaps you should reconsider your use of this product.
The solution in my case was to start a server part on my phone (AppCenter from AndroidPit.com in this case). No entries in AndroidManifest are necessary for the client application.

Categories

Resources