Tuesday, March 12, 2019

Android开发笔记-ch3.4.11~15 Location, Weather and News, Barcode, dataBase, Miracast, Serial Port, USB, FTDI


3.4.11 Location, Weather and News

For getting weather info, most app is using OpenWeatherMap API, like in this and this. On Android, must use littlecase appid like: http://api.openweathermap.org/data/2.5/weather?q=Toronto&units=metric&appid=376a1bfbe17ace36d2e534bfd0a0d5f0. For 5 days forecast: api.openweathermap.org/data/2.5/forecast?q={city name}. I used to have issue to load the custom font under assets at runtime. Ref to this SO and this SO. Logcat shows “native typeface cannot be made”. For my case it turns out to be corrupted font file. The font file is a xml file and can be viewed with font viewer: https://github.com/erikflowers/weather-icons/tree/master/font.
Weather info can also be got from yahoo RSS. Refer to this post. =>might need credentials now.
For news feed, refer to this so, this and google news rss. Some sample code is wrong doing to get http content with in OnCreate. Should do the web access in runnable/thread instead, just as above link for yahoo weather.
For current location, refer to this(Google Play API) and this(Android Location API). As mentioned in 2nd link: The Google Location Services API, part of Google Play Services, provides a more powerful, high-level framework that automatically handles location providers, user movement, and location accuracy.It also handles location update scheduling based on power consumption parameters you provide. The good with Android Location API is no need to link the GooglePlay lib. Note for the Android one, better to request a location update instead of just get last known location, as if other App update the location, the last known location may be old.
Problem with some device such as TV box is there is no location provider. Checking code like this:
Criteria criteria = new Criteria();
criteria.setAccuracy( Criteria.ACCURACY_COARSE );
String provider = locManager.getBestProvider( criteria, true );
if ( provider == null ) { Log.e(TAG, "No location provider found!" ); }
else locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);} //Register callback
Several online IP lookup:

3.4.12 Generate Barcode

Refer to this SO. Get ZXing-2.1.zip from google code.

3.4.13 dataBase

Refer to SQLiteDatabase. As here mentioned Android db is slow and the database remains consistent under any circumstances. So following sequence is very necessary: BeginTransaction();SetTransactionSuccessful(); EndTransaction();

3.4.14 Miracast

Refer to this SO and this for auto connection.

3.4.15 Serial Port, USB, FTDI

Here is FTDI link of sample code., include UARTTest and UARTLoopback.
Seeing error 'cannot locate tcgetattr', refer to this SO. Lot of change between API19 and 21. Use low level target API to solve it.

1 Comment:

Janik Stoiber said...

This one tool iplocation.io, will also be very useful for you to get public IP.

Post a Comment