Android Location API: Unterschied zwischen den Versionen
| Zeile 48: | Zeile 48: | ||
Die Zeile mit SHA1 enthält den fingerprint, eine Sequenz von 20 two-digit hexadecimal numbers separated by colons. | Die Zeile mit SHA1 enthält den fingerprint, eine Sequenz von 20 two-digit hexadecimal numbers separated by colons. | ||
| − | + | ===== Release Zertifikat anzeigen ===== | |
| − | + | Auch hier muß man das release certificate keystore file finden, dies ist jedoch nicht immer am gleichen Ort. Man kann einen Ort festlegen, wenn man das release build für die App macht. Wenn man es nicht macht bleibt die App unsigniert und man kann Sie beim Publizieren Signieren. Da wir aber den API Key brauchen geht das in diesem Fall nicht. | |
| + | |||
| + | Für das release Zertifikat braucht man auch den certificate's alias und die Passwörter für den keystore und das Zertifikat. | ||
| + | |||
| + | So kann man die Alias Liste für alle Keys im Keystore anzeigen: | ||
| + | |||
| + | keytool -list -keystore your_keystore_name | ||
| + | Ersetze ''your_keystore_name'' mit dem fully-qualified Pfad und dem Namen des Keystore, inklusive der .keystore extension. Es wird nach dem keystore Passwort gefragt und danach die Liste angezeigt. | ||
| + | |||
| + | Jetzt können wir folgendes eingeben: | ||
| + | <pre> | ||
| + | keytool -list -v -keystore your_keystore_name -alias your_alias_name | ||
| + | </pre> | ||
| + | Replace your_keystore_name with the fully-qualified path and name of the keystore, including the .keystore extension. Replace your_alias_name with the alias that you assigned to the certificate when you created it. | ||
| + | |||
| + | Caution: To protect your keystore and key, don't enter the storepass or keypass arguments on the command line unless you're confident of your computer's security. For example, on a public computer, someone could look at your terminal window history or list of running processes, get the password, and then have write access to your signing certificate. This would allow that person to modify or replace your application with their own. | ||
| + | |||
| + | You should see a display that is similar to this: | ||
| + | |||
| + | Alias name: <alias_name> | ||
| + | Creation date: Feb 02, 2013 | ||
| + | Entry type: PrivateKeyEntry | ||
| + | Certificate chain length: 1 | ||
| + | Certificate[1]: | ||
| + | Owner: CN=Android Debug, O=Android, C=US | ||
| + | Issuer: CN=Android Debug, O=Android, C=US | ||
| + | Serial number: 4cc9b300 | ||
| + | Valid from: Mon Feb 02 08:01:04 UTC 2013 until: Mon Feb 02 18:05:04 PST 2033 | ||
| + | Certificate fingerprints: | ||
| + | MD5: AE:9F:95:D0:A6:86:89:BC:A8:70:BA:34:FF:6B:AC:F9 | ||
| + | SHA1: BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:90:AF:A1:66:6E:44:5D:75 | ||
| + | Signature algorithm name: SHA1withRSA | ||
| + | Version: 3 | ||
| + | The line that begins SHA1 contains the certificate's SHA-1 fingerprint. The fingerprint is the sequence of 20 two-digit hexadecimal numbers separated by colons. | ||
| + | |||
| + | ===== Projekt in der Google APIs Console registrieren ===== | ||
| + | and add the Maps API as a service for the project. | ||
*Once you have a project set up, you can request one or more keys. | *Once you have a project set up, you can request one or more keys. | ||
*Finally, you can add your key to your application and begin development. | *Finally, you can add your key to your application and begin development. | ||
Version vom 8. Oktober 2013, 14:52 Uhr
Links und Quellen
http://www.vogella.com/articles/AndroidLocationAPI/article.html
http://developer.android.com/reference/android/location/package-summary.html
Google Maps API Key
- Notwendig für Zugriffe auf den Google Maps Kartenserver.
- Alle Android Apps müssen mit einem digitalen Zertifikat signiert werden für das der Entwickler den private key hat. Damit werden die Apps auf dem Map-Server oder im PlayStore getrackt.
- Man benötigt einen Schlüssel für jedes Zertifikat
- In der Praxis erstellt man ein Zertifikat für jede App (theoretisch kann man auch mehrere mit einem Zertifikat signieren)
- Für die Entwicklung nutzt Eclipse ein Entwickler Zertifikat ohne API Key dies reicht nicht bei Apps die einen Maps API Key benötigen
Google Maps API Key erstellen
Google Play Services Installieren
Dieses SDK benötigt man um einen Key zu holen (Dieser Schritt muß nur einmal gemacht werden)
- Android SDK aus Eclipse oder über Terminal starten.(On Mac or Linux, tools/ directory Befehl: android sdk)
- Im Android SDK Extras > Google Play services SDK installieren
App Zertifikat Fingerprint anzeigen
Es gibt Debug Zertifikate und Release Zertifikate. Das Debug Zertifikat wird von Eclipse automatisch beim Debuggen der App erstellt. Um den passenden Key zu bekommen braucht man den Fingerprint des gewünschten Zertifikats.
Debug Zertifikat Fingerprint
Die Zertifikate kann man aus dem debug keystore file auslesen. Es nennt sich debug.keystore und wird beim ersten erstellen des Projekts erzeugt. Es befindet sich im gleichen Verzeichnis wie die Android Virtual Device (AVD) Dateien. Unter Mac ist das:
~/.android/
Windows Vista and Windows 7:
C:\Users\your_user_name\.android\
In Eclipse kann man den Ort mit
Windows > Prefs > Android > Build
Anzeigen lassen (dann kopieren und in den Finder kopieren.
List the SHA-1 fingerprint im Terminal:
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
Ausgabe sieht etwa so aus:
Alias name: androiddebugkey
Creation date: Jan 01, 2013
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 4aa9b300
Valid from: Mon Jan 01 08:04:04 UTC 2013 until: Mon Jan 01 18:04:04 PST 2033
Certificate fingerprints:
MD5: AE:9F:95:D0:A6:86:89:BC:A8:70:BA:34:FF:6A:AC:F9
SHA1: BB:0D:AC:74:D3:21:E1:43:07:71:9B:62:90:AF:A1:66:6E:44:5D:75
Signature algorithm name: SHA1withRSA
Version: 3
Die Zeile mit SHA1 enthält den fingerprint, eine Sequenz von 20 two-digit hexadecimal numbers separated by colons.
Release Zertifikat anzeigen
Auch hier muß man das release certificate keystore file finden, dies ist jedoch nicht immer am gleichen Ort. Man kann einen Ort festlegen, wenn man das release build für die App macht. Wenn man es nicht macht bleibt die App unsigniert und man kann Sie beim Publizieren Signieren. Da wir aber den API Key brauchen geht das in diesem Fall nicht.
Für das release Zertifikat braucht man auch den certificate's alias und die Passwörter für den keystore und das Zertifikat.
So kann man die Alias Liste für alle Keys im Keystore anzeigen:
keytool -list -keystore your_keystore_name
Ersetze your_keystore_name mit dem fully-qualified Pfad und dem Namen des Keystore, inklusive der .keystore extension. Es wird nach dem keystore Passwort gefragt und danach die Liste angezeigt.
Jetzt können wir folgendes eingeben:
keytool -list -v -keystore your_keystore_name -alias your_alias_name
Replace your_keystore_name with the fully-qualified path and name of the keystore, including the .keystore extension. Replace your_alias_name with the alias that you assigned to the certificate when you created it.
Caution: To protect your keystore and key, don't enter the storepass or keypass arguments on the command line unless you're confident of your computer's security. For example, on a public computer, someone could look at your terminal window history or list of running processes, get the password, and then have write access to your signing certificate. This would allow that person to modify or replace your application with their own.
You should see a display that is similar to this:
Alias name: <alias_name>
Creation date: Feb 02, 2013
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 4cc9b300
Valid from: Mon Feb 02 08:01:04 UTC 2013 until: Mon Feb 02 18:05:04 PST 2033
Certificate fingerprints:
MD5: AE:9F:95:D0:A6:86:89:BC:A8:70:BA:34:FF:6B:AC:F9
SHA1: BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:90:AF:A1:66:6E:44:5D:75
Signature algorithm name: SHA1withRSA
Version: 3
The line that begins SHA1 contains the certificate's SHA-1 fingerprint. The fingerprint is the sequence of 20 two-digit hexadecimal numbers separated by colons.
Projekt in der Google APIs Console registrieren
and add the Maps API as a service for the project.
- Once you have a project set up, you can request one or more keys.
- Finally, you can add your key to your application and begin development.
Position bestimmen
Android kann die Position mit dem GPS Empfänger, über umgebende WLAN Netzwerke (die dann Online mit einer Google Datenbank abgeglichen werden) oder über die aktuelle Handyzelle bestimmen.
Für die Positionsbestimmung stellt Android das Package:
android.location
zur Verfügung.
Wichtige Komponenten
Location Manager
Class LocationManager für Zugriff auf Android location service
LocationProvider
Superclass für die LocationProvider die die Position liefern. Es gibt 3:
network //Mobile oder WiFi gps passive // für externe Geräte
LocationListener
Über den Location Manager kann man einen LocationListener registrieren um regelmäßige Positionsupdates zu bekommen.
Hinweis in Stackoverflow wird über Probleme mit dem onStatusChanged Listener auf diversen Versionen von Android s.u.
Criteria Objekt zur Provider Auswahl
Mit einem Criteria Objekt bleibt man flexibel bei der Auswahl des Providers (GPS, Netzwerk...)
Im Criteria Objekt werden Anforderungen zum Beispiel für die Genauigkeit des Ortes gesetzt und diese anschließend bei der Providerauswahl übergeben:
Criteria criteria = new Criteria(); // Default Settings können über set Methoden verändert werden
provider = locationManager.getBestProvider(criteria, false); // Provider mit Kriterien auswählen false bedeutet auch nicht aktivierte Provider werden berücksichtigt.
Location location = locationManager.getLastKnownLocation(provider);
Geocoder
Über die Geocoder Klasse kann man Positionsdaten zu Adressdaten und umgekehrt ermitteln (Google Service)
Notwendige Rechte / Permissions
ACCESS_FINE_LOCATION permission für GPS, ansonsten ACCESS_COARSE_LOCATION permission
Beispiel 1 - locationapi.simple
Android Location API - Simple Location
Location Snippets
User zum aktivieren von GPS auffordern
Hinweis: Es ist nicht möglich GPS nur über den Code zu starten.
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Check if enabled and if not send user to the GSP settings
// Better solution would be to display a dialog (AlarmDialog) and suggesting to
// go to the settings
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Simulation auf dem Simulator
Auf dem Emulierten Device GoogleMaps aktivieren falls das aktivieren des GPS Simulators nicht klappt (Bugs). Man kann die DDMS Perspective von Eclipse nehmen um Geopositionen zu senden.
Window > Open Perspective > Other > DDMS
Über das Terinal und Telnet kann man auch Positionen schicken
telnet localhost 5554 geo fix 13.12 49.23
Probleme bei der Positionsabfrage umgehen
Quelle: http://stackoverflow.com/questions/2021176/how-can-i-check-the-current-status-of-the-gps-receiver
onStatusChanged() doesn't get called on Eclair and Froyo. It does get called on 1.6 though. Simply counting all available satellites is, of course, useless. Checking if any of the satellites returns true for usedInFix() isn't very helpful also. The system apparently loses the fix but still continue to report that there are several sats that are used in it. So, the only working solution we have, and the one we actually use in our application, is the following. Let's say we have this simple class that implements the GpsStatus.Listener:
private class MyGPSListener implements GpsStatus.Listener {
public void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
if (mLastLocation != null)
isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;
if (isGPSFix) { // A fix has been acquired.
// Do something.
} else { // The fix has been lost.
// Do something.
}
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
// Do something.
isGPSFix = true;
break;
}
}
}
OK, now in onLocationChanged() you should add the following:
@Override
public void onLocationChanged(Location location) {
if (location == null) return;
mLastLocationMillis = SystemClock.elapsedRealtime();
// Do something.
mLastLocation = location;
}
And that's it. Basically, this is the line that does it all:
isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;
You can tweak the milliseconds value of course, but I would suggest to set it around 3-5 seconds.
This actually works and though I haven't seen the source code that implements the standard GPS icon, this comes close to replicating its behavior. It could even outdo it lol. Hope this helps someone.