• Home
  • Android Snippets
 
 

Android Snippets

74 Items
  • Android Snippets
    Reverse Geocoding

    Here is a full example code using a Thread and a Handler to get the Geocoder answer without blocking the UI. Geocoder call procedure, can be located in a Helper class

    0
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Open twitter via Intent

    This code allows you to open user twitter feed by twitter app, if it exist, otherwise open in standard android app (ex. browser).

    0
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Simplifying android log

    I am trying to discover some simple and efficient way to use Android logger. It give us deep information about error: L.e("Test mesage"); com.yourpackage.ClassName: [Method] Test mesage Stackoverflow pros/cons

    0
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Scale Camera BitMap

    Scale BitMap

    0
    0
    0
    Favorite
    Grab It
  • Android Snippets
    How to pass a dynamic URL to a Webview Activity

    I don't really know if this way is correct, but it's simple and works (:

    5
    0
    0
    Favorite
    Grab It
  • Android Snippets
    HttpGet request in AsyncTask

    HttpGet request in a separate thread via AsyncTask

    4
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Generate random UUID and store it

    How to generate a random UUID and store it in shared prefs. You can also use the backup service to store it in the cloud so if your user changes its device, the UUID would be still valid. Taken from Reto Meier's talk http://www.youtube.com/watch?v=PwC1OlJo5VM&feature=player_embedded#!

    4
    0
    0
    Favorite
    Grab It
  • Android Snippets
    How to load data

    For example load data, parse it as xml, json or etc. and get list of urls

    3
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Android update checker

    Recently I've started to develop a few Android applications with some friends, and as soon as I started to make updates I faced a problem: how can I tell my beta testers to update to the latest test version? So I wrote UpdateChecker. It doesn't use the market, just a publicly accessible text file for version check. For more details you can check it here: http://blog.fdev.eu/updatechecker/

    2
    0
    0
    Favorite
    Grab It
  • Android Snippets
    getting next value in android using SQLite

    Getting error while displayin data.

    2
    0
    0
    Favorite
    Grab It
  • Android Snippets
    SHA-1 Hash Function

    SHA-1 Hash function 160-bit http://en.wikipedia.org/wiki/SHA-1 Code pieced together from stackoverflow.com

    2
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Horizontal Progress Bar

    To get an horizontal progress bar, that is not very clear in the docummentation, use this native style.

    2
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Play single or multiple sounds in any class

    The method checks to see if an instance already exists and creates one if it does not before returning the instance.You should also notice that the method is static which all fields and methods have to be for the singleton pattern to work.With this Method any and all classes within the app can access the direct instance should they need it. Now in order to use this Sound Manager in your apps it first needs to be initialised.So in your main Activity use the following code to set it up. //Create, Initialise and then load the Sound manager SoundManager.getInstance(); SoundManager.initSounds(this); SoundManager.loadSounds(); From now on all that needs to be done to play a sound is to call SoundManager.PlaySound from any part of your code. Make sure to use the clean-up function when the app is destroyed to release the instance.

    1
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Latitude and Longitude of a place

    One can know the latitude and longitude of a place if know the place name.

    1
    0
    0
    Favorite
    Grab It
  • Android Snippets
    SmartWatch gallery of pictures

    The SonyEricsson SmartWatch is a device which connects by BlueTooth to an Android phone and acts as an extra display. It contains an accelerometer. The snippet here shows how to change the picture at the touch of the screen, and also to capture and send in the accelerometer readings. This is an early version of my project which will hopefully stimulate others to post their efforts.

    1
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Connecting to MySQL database

    The most spread method to connect to a remote MySQL database from an android device, is to put some kind of service into the middle. Since MySQL is usually used together with PHP, the easiest and most obvious way to write a PHP script to manage the database and run this script using HTTP protocol from the android system. mysql logo We can code the data in JSON format, between Android and PHP with the easy to use built in JSON functions in both languages. I present some sample code, which selects data from a database depending on a given condition and creates a log message on the android side with the received data. Lets suppose that we have a MySQL database named PeopleData, and a table int created, with the following SQL: CREATE TABLE `people` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 100 ) NOT NULL , `sex` BOOL NOT NULL DEFAULT '1', `birthyear` INT NOT NULL ) We want to get all the data of the people, who were born after a specified year. The PHP code will be very simple: - connect to the database - run an SQL query, with a WHERE block depending on data from POST/GET values - output it in JSON format For example we will have this functionality in the getAllPeopleBornAfter.php file: '".$_REQUEST['year']."'"); while($e=mysql_fetch_assoc($q)) $output[]=$e; print(json_encode($output)); mysql_close(); ?> The Android part is only a bit more complicated: -use a HttpPost to get the data, sending the year value -convert response to string -parse JSON data, and use it as you want String result = ""; //the year data to send ArrayList nameValuePairs = new ArrayList(); nameValuePairs.add(new BasicNameValuePair("year","1980")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://example.com/getAllPeopleBornAfter.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } //parse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i Of course it is possible to use HTTPS and send password to access data, or do more complex data processing on either side, or write more general code, which does not include this much predefined parameters in the database accessing query.

    1
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Get Request JSON

    HTTP Get request for json

    1
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Login Request, receive token

    Login Request, receive token

    1
    0
    0
    Favorite
    Grab It
  • Android Snippets
    String List Join

    A simple method like Apache Commons Lang StringUtils.join()

    1
    0
    0
    Favorite
    Grab It
  • Android Snippets
    Array concatenate

    These two methods allow you to make one array from two, to concatenate them. A major difficulty with Java is the use of null to represent a new or empty array. The second app allows you to pick out just the part of an array after 'start', a common requirement. Obviously replace 'double' with the type you are using. returns an array or null.

    0
    0
    0
    Favorite
    Grab It

-