You are on page 1of 8

LIVE FOLDERS:-

Live folders have been introduced in Android 1.5 and let you display any source of data on the Home screen without forcing
the user to launch an application. A live folder is simply a real-time view of a ContentProvider. As such, a live folder can be
used to display all your contacts, your bookmarks, your email, your playlists etc. The possibilities are endless! Android 1.5
ships with a few stock live folders to display your contacts.

Another way you can make content-rich applications more readily available to users is with the use of live folders.
Introduced in Android 1.5 (API Level 3), a live folder is a special type of folder that the user can place in various areas such
as the Home screen and, when clicked, displays its content by querying an application that acts as a content provider. Each
piece of data in the folder can be paired with an intent. You could also think of it as a folder of shortcuts into your
application. For example, a music application might allow the user to create live folders for favorite music. Similarly, a to-do
list application might include support for a live folder of the day’s tasks. Finally, a game might have a live folder for saved
game points. When the user clicks on an item, the application launches to play the appropriate song, show the appropriate
to-do list item, or start the game at that save point.

To enable in-application live folder creation within your application, you need

 An application with data exposed as a content provider


 An application that acts as a content type handler for the type of data that is exposed in the live folder
 To implement an Activity class to handle live folder creation
 To update the application’s content provider interface to handle live folder queries
 To configure the application’s Android manifest file for live folders
Sd cards:-

There are two ways to use an SD card with your device. In the past, Android has traditionally used all SD cards as portable
storage. You can remove the SD card from the device and plug it into your computer or another device and transfer files, like
videos, music, and photos, back and forth. Your Android device will continue working properly if you remove it.

Starting with Android 6.0 Marshmallow, though, some phones can use SD cards as internal storage as well. In this
case, your Android device “adopts” the SD card as part of its internal pool. It’ll be treated as part of your internal storage, and
Android can install apps to it and save app data to it. In fact, since it’s considered internal storage, any type of app can be
installed to the SD card–including apps that provide widgets and background processes. Unlike older versions of Android, it
doesn’t matter whether the developer has disabled the “move to SD card” permission or not.

However, when you use an SD card as internal storage, Android formats the SD card in such a way that no other device can
read it. Android also expects the adopted SD card to always be present, and won’t work quite right if you remove it. This
method is useful if your phone doesn’t come with very much space to begin with, and you want more space for your apps
and files.

Xmlprsing:-

XML stands for Extensible Mark-up Language.XML is a very popular format and commonly used for sharing data
on the internet. This chapter explains how to parse the XML file and extract necessary information from it.

XML - Parsing

In the next step, we will create XMLPullParser object , but in order to create that we will first create XmlPullParserFactory
object and then call its newPullParser() method to create XMLPullParser. Its syntax is given below −
private XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
private XmlPullParser myparser = xmlFactoryObject.newPullParser();
The next step involves specifying the file for XmlPullParser that contains XML. It could be a file or could be a Stream. In
our case it is a stream.Its syntax is given below −
myparser.setInput(stream, null);
The last step is to parse the XML. An XML file consist of events, Name, Text, AttributesValue e.t.c. So XMLPullParser has
a separate function for parsing each of the component of XML file. Its syntax is given below −

int event = myParser.getEventType();


while (event != XmlPullParser.END_DOCUMENT) {
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;

case XmlPullParser.END_TAG:
if(name.equals("temperature")){
temperature = myParser.getAttributeValue(null,"value");
}
break;
}
event = myParser.next();
}

The method getEventType returns the type of event that happens. e.g: Document start , tag start e.t.c. The
method getName returns the name of the tag and since we are only interested in temperature , so we just check in
conditional statement that if we got a temperature tag , we call the method getAttributeValue to return us the value of
temperature tag.
EXAMPLE:-

<?xml version="1.0"?>
<records>
<employee>
<name>Sairamkrishna</name>
<surname>Mammahe</surname>
<salary>50000</salary>
</employee>

<employee>
<name>Gopal </name>
<surname>Varma</surname>
<salary>60000</salary>
</employee>

<employee>
<name>Raja</name>
<surname>Hr</surname>
<salary>70000</salary>
</employee>
</records>
JSON PRSING:- JSON stands for JavaScript Object Notation. It is structured, light weight, human readable and easy to parse. It’s a
best alternative to XML when our android app needs to interchange data from server. XML parsing is very complex as compare
to JSON parsing.
JSON is shorter, quicker and easier way to interchange data from server. JSON is great success and most of the API available support
JSON format.
Android Provide us four different classes to manipulate JSON data. These classes are JSONObject, JSONArray, JSONStringer and
JSONTokenizer.
Below we define some important methods of JSONObject parsing which are mainly used for parsing the data from JSONObject.
1. get(String name): This method is used to get the value from JSONObject. It returns the value of object type. We pass the String type
key and it returns the value of Object type if exists otherwise it throws JSONException.
2. getString(String name): This method is used to get the String type value from JSONObject. We pass the String type key and it
returns the value in String type if exists otherwise it throws JSONException.
3. length(): This method is used to get the number of name/value mappings in this object.
4. keys(): This method is used to get the iterator of String names in the Object.
EXAMPLE

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="abhiandroid.com.jsonparsingexample.MainActivity">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Name"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="@+id/salary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:text="Salary"
android:textColor="#000"
android:textSize="20sp" />
</RelativeLayout>
package abhiandroid.com.jsonparsingexample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
String JSON_STRING = "{\"employee\":{\"name\":\"Abhishek Saini\",\"salary\":65000}}";
String name, salary;
TextView employeeName, employeeSalary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the reference of TextView's
employeeName = (TextView) findViewById(R.id.name);
employeeSalary = (TextView) findViewById(R.id.salary);
try {
// get JSONObject from JSON file
JSONObject obj = new JSONObject(JSON_STRING);
// fetch JSONObject named employee
JSONObject employee = obj.getJSONObject("employee");
// get employee name and salary
name = employee.getString("name");
salary = employee.getString("salary");
// set employee name and salary in TextView's
employeeName.setText("Name: "+name);
employeeSalary.setText("Salary: "+salary);
} catch (JSONException e) {e.printStackTrace();}
}
}

GOOGLE MAP:-
By using Google Maps Android API we can integrate google maps in android applications to show the location details on map based on
our requirements.

To use google maps in our android applications we need to install Google Play Services SDK in our Android Studio because google
made Google Mas API as a part of Google Play Services SDK.

To install Google Play Services, open Android Studio  Go to Tools menu  Android  click SDK Manager, then new window will
open in that select SDK Tools tab  Select Google Play Services  click OK
Once we are done with Google Play Services installation in android studio, now we will see how to integrate google map in android app
with examples.
Android Google Maps API Example
Following is the example of adding or integrating a google map in android application.

Create a new android application using android studio and give names as GoogleMapExample
Now we need to select the form factors which we need for our app. In case if you're not sure what you need, just
select Phone and Tablet and then click Next
Now select the Google Maps Activity in 'Add an activity to Mobile' dialog and click Next
Customize the activity by entering activity name, layout name and title as prompted. In case if default values are fine, then
click Finish

Once project created, Android Studio will open google_maps_api.xml and MapsActivity.java files in the editor.
The google_maps_api.xml file will contains an instructions to generate a Google Maps API key to access Google Maps
servers. Copy the link provided in the google_maps_api.xml
Create a Project in Google Console
Copy and paste the console URL in browser and it will take you to Google API Console like as shown below. Follow the
instructions to create a new project on Google API Console
Once we click on continue it will create a project and Google Maps Android API will be enabled. Now we need to create an
API key to call the API for that click on Create API Key

Now copy the API Key, go back to android studio and paste the API key into the <string> element
in google_maps_api.xml file like as shown below.

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">A


IzaSyCKPTaBv41DKqr9qxMPWOQAsqp0Q4NHMER</string>

Activity_maps.xml
By default, the XML file (activity_maps.xml) that defines the app's layout is at res/layout/ contains the following
code.

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tutlane.googlemapexample.MapsActivity" />

AndroidManifest.xml
Our application manifest file (AndroidManifest.xml) will contain the code like as shown below with required
our Google Maps API Key and permissions.

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.googlemapexample">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

MapsActivity.java
By default, the Java file (MapsActivity.java) that defines the maps activity will contain the following code.

package com.tutlane.googlemapexample;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {


private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be
used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in hyderabad and move the camera
LatLng hyderadbad = new LatLng(17, 78);
mMap.addMarker(new MarkerOptions().position(hyderadbad).title("Tutlane in India"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(hyderadbad));
}
}
Generally, during the launch of our activity, onCreate() callback method will be called by android framework to get
the required layout for an activity.

Android Google Map Types


The Google Maps Android API provides a map in different types such as Normal, Hybrid, Satellite, Terrain and None.

Map
Type Description

Normal Typical road map. Shows roads, some features built by humans, and important natural features like rivers. Road
and feature labels are also visible.

Hybrid Satellite photograph data with road maps added. Road and feature labels are also visible.

Satellite Satellite photograph data. Road and feature labels are not visible.

Terrian Topographic data. The map includes colors, contour lines and labels, and perspective shading. Some roads and
labels are also visible.

None No tiles. The map will be rendered as an empty grid with no tiles loaded.

In android, we can change the type of a map by calling the GoogleMap object’s setMapType() method, by passing
the type of constants defined in GoogleMap.

LOCATION BASED SERVICES:-

Location Based Services In Android – The Concept


We have something known as ‘Location Based Services‘ in Android. Location Based Services are provided by Android
through its location framework. The framework provides a location API which consists of certain classes and interface.
These classes and interface are the key components which allow us to develop Location Based Application in Android.

Classes and Interfaces of Location Based Services:

LocationManager – This class helps to get access to the location service of the system.
LocationListener – This interface acts as the listener which receives notification from the location manager when the location changes or
the location provider is disabled or enabled.
Location – This is the class which represents the geographic location returned at a particular time.

These android built-in components have made it easier for us to get the user location. In this blog post, we will walk through
the steps that need to be followed to retrieve user location.

Step 1
An instance of the LocationManager needs to be created as the first step in this process. This is the main class through
which the application gets access to the location services in Android. A reference to this system service can be obtained by
calling getSystemService() method.

LocationManagerlocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Step 2
Next step is to identify the location provider you would like to use for retrieving location details.
The location providers actually provide the location data in Android. Android exposes two main location providers:

1. GPS Location Provider:


This location provider provides location data with the highest accuracy (~2 m – 20m). Location updates are provided
through satellites. However there are some drawbacks with this provider which are explained as below:

A. It is significantly slower than the network provider to get the initial connection setup with the satellites. This initial
connection, also called Time to First Fix (TTFF) can be extremely slow but once the connection is established, location
updates are quite fast.

B. GPS provider also drains out the battery very first. So, it is very important to judiciously use this provider. Special care
should be taken to ensure that this is turned off when you are not using it.

C. The third draw back comes from the fact that GPS uses radio signal which is obstructed by solid objects. So, GPS
provider will not work if you are inside a building or in a basement.

2. Network Provider:
This provider uses Wi-Fi hotspots and cell tower to approximate a location. The provider retrieves an approximate location
by querying the Google location server using the IDs of the Wi-Fi hotspots and cell towers. Location accuracy is between
100 – 1000 m. The location accuracy is directly proportional to the number of Wi-Fi hotspots and cell towers available in the
area.

You can check the availability of the providers using the below piece of code:

isGpsEnabled = mLocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = mLocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

Step 3
The next step is to register a location listener with the location manager. The registration can be done
by calling the method requestLocationUpdates((String provider, long minTime, float minDistance,
LocationListener listener).

Following are the parameters passed to this method:


provider:
the name of the provider with which to register. This can be either GPS or Network provider.

minTime:
minimum time interval between location updates, in milliseconds.

minDistance:
minimum distance between location updates, in meters.

listener:
a LocationListener whose onLocationChanged(Location) method will be called for each location update.

The Location Manager calls the onLocationChanged() of the listener when the mobile has moved a
distance greater than the minDistance or the time since last location update is more than minTime.

You might also like