What Is Android
What Is Android
What is Android ?
- Native Apps
[ Java - Android , swift- iPhone]
- Hybrid Apps [ PhoneGap,Kony,Ionic,Titanium…]
[ HTML + CSS + Java Script = Android & iPhone &
Windows]
- low performance
- complex UI is difficult to design.
- difficult to access the device native components
[ camera , gps , bluetooth ….]
- there is no official framework for developing
Hybrid Apps.
- Support is less.
- immediately we can‟t get the latest updates.
Why Android?
Features :
- Open Source [complete source code is exposed public,
people can see the code & modify code according to their requirement ].
- Application framework is providing infrastructures for the
application developer.
e.g.: WifiManager , LocationManager, SensorManager,BTManager.
- DVM[ Dalvik Virtual Machine] is optimised for mobile
devices to work on low power, low memory & low RAM, it is a
customised JVM.
.java >> JDK >> .class >> DVM >> .dex >> DVM >> 010101
http://www.igaptechnologies.com Page 1
Android Notes iGAP Technologies
Android Components :
Core Android : [ 25 - sessions ]
- Activity
- Service
- Broadcast Receiver
- Content Provider
Activity :
Service :
Broadcast Receiver :
Content Provider :
- GoogleMaps
- Google Places
- Google Cloud Messaging [GCM]
10,000 +
- SMS [ 16 ps = 1600 ]
- Call
- E-Mail
- Notifications [ no cost, no limit, 4kb, 4-
weeks , 10 times faster
than SMS]
- Android Material Design[ Recycler View, CardView, FAB,
SnakBar, Navigable Drawer, ToolBar…]
http://www.igaptechnologies.com Page 3
Android Notes iGAP Technologies
- Android Firebase
- Graphics Programming
- Graphics Programming with Cocos-2D
Android Environment :
- Eclipse + ADT plugin
- ADT Bundle
- Android Studio
Hello World
- app
- java
- pkg_name
- *.java
- res
- drawable >> *.png
- layout >> UI.xml
- values >> strings.xml[I18N]
- mipmap >> app icon
- raw >> *.pdf,.mp3,.mp4,.txt..
- menu >> menu.xml
- manifests
- AndroidManifest.xml
- Manifest.xml provides complete description
[ activities , services , permissions,features,
app icon, initial screen…] about the application
Android platform before installing the application
and before start the application it reads
Manifest.xml]
- R.java :
- R is termed as resource.
- R.java is a reference to the res folder.
- R.java is an abstraction between the java file
http://www.igaptechnologies.com Page 4
Android Notes iGAP Technologies
eg : R.drawable.sample
R.layout.main
R.id.uname
R.raw.test
.apk :
- apk is termed as Android application package.
- it is a installation file in Android platform.
- .apk consists .dex,res,assets,lib and Manifest.xml
- the entire project will be convert into a single
executable file called .apk.
- gradle scripts
XML :
- XML is termed as Extensible as Extensible
Markup Language.
HTML : Hypertext Markup Language.
Markup Language :
- enclosing the data with in tags is called as
Markup Language.
Eg : <employees>
<employee>
<id>123</id>
<name>Mahesh</name>
<desig>TL</desig>
http://www.igaptechnologies.com Page 5
Android Notes iGAP Technologies
</employee>
</employees>
XML :
- XML is interoperable [ platform independent,
technology independent]
- XML is used to transfer the data between
multiple technologies.
- XML is used as textual database.
- XML is used as deployment descriptor.
[ web.xml , Manifest.xml, web-config.xml…]
- by using XML we can create user-defined tags.
Rules :
- every XML element must be properly nested.
- every XML file should have only one root
element.
- tag name shouldn‟t contain space.
- tag name shouldn‟t start with number or
special characters except (_ and $).
- use entities to represent the following values
( > - > < - < & - & „ - &aps; )
UI Group :
- Relative Layout
LinearLayout :
- LinearLayout is one of the UI group which is used to arrange the
UI components in a vertical | horizontal format one after another.
syntax :
<LinearLayout
xmlns:android=“xsd_location”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical|horizontal”>
// UI components
</LinearLayout>
- match_parent
- fill_parent
- wrap_content
- px
- dp [sp]
- Foreground
- Pause
- Background
- following are the major methods in Activity class, which will
participate in Activity life cycle.
3). there should be a link between XML & Java, use the following
method to set the XML file to Java.
setContentView(R.layout.xml_file);
android:onClick=“method_name”
}
- to get the UI component from XML to Activity, we have to configure
an id for the UI component, use the following attribute to
specify the id.
android:id=“@+id/id_name”
findViewById(R.id.id_name);
EditText et1=(EditText)findViewById(R.id.et1);
TextView tv1=(TextView) findViewById(R.id.tv1);
tv1.setText(et1.getText());
}
Java :
- to configure the click event using java, configure the
following listener to the UI component.
http://www.igaptechnologies.com Page 9
Android Notes iGAP Technologies
Button b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// logic
}
});
Intents :
Implicit Intents :
Explicit Intents :
syntax :
Intent i=new Intent(context,Activity_name.class);
startActivity(i);
http://www.igaptechnologies.com Page 10
Android Notes iGAP Technologies
Intent i=getPackageManager().
getLauchIntentForPackage(“package_name”);
startActivity(i);
setAction() setType()
setData() putExtra()
setComponent() getExtra()
<activity android:name=“package_name.class_name”/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
syntax :
XML : <AutoCompleteTextView
android:id=“@+id/actv1”
……………/>
Java :
AutoCompleteTextView
actv=(AutoCompleteTextView)
findViewById(R.id.actv1);
<string-array name=“array_name”>
<item> value1 </item>
<item> value2 </item>
<item> value3 </item>
<item> value4 </item>
</string-array>
- use the following method in java , to get the XML configured values
into Activity.
String[]
values=getResources().getStringArray(R.array.array_name);
http://www.igaptechnologies.com Page 12
Android Notes iGAP Technologies
(context,xml_file,values);
actv.setAdapter(adapter);
actv.setThreshold(int);
Spinner :
XML : <Spinner
android:id=“@+id/sp1”
…………………… />
Java :
Spinner sp1=(Spinner)findViewById(R.id.sp1);
same like ACTV for presenting the values we have to configure the
values in XML/Java.
- if the values are configured in XML, use the following attribute to set
the values.
android:entries=“@array/array_name”
http://www.igaptechnologies.com Page 13
Android Notes iGAP Technologies
sp.setAdapter(adapter_obj);
- Spinner is having item selected event, to get this event configure the
following listener.
- sp.setOnItemSelectedListener(new OnItemSelectedListener(){
onItemSelected(){ };
doNothingSelected() { };
});
Toast :
- Toast is one of the notification method in Android which is
used to display the text on the screen for few seconds.
syntax :
Toast.makeText(context,message,duration).show();
ListView :
syntax :
XML : <ListView
android:id=“@+id/lview1”
……………/>
Java :
ListView
lview=(ListView)findViewById(R.id.lview1);
same like ACTV & Spinner for presenting the values configure the
values in XML / Java.
http://www.igaptechnologies.com Page 14
Android Notes iGAP Technologies
<uses-permission android:name=“android.permission.
READ_EXTERNAL_STORAGE”/>
LayoutInflater :
- LayoutInflater class is used to convert the Xml UI into View
object.
LayoutInflater inflater=LayoutInflater.from(context);
http://www.igaptechnologies.com Page 15
Android Notes iGAP Technologies
Gallery :
syntax :
<Gallery
android:id=“@+id/gal1”
……………… />
WebView :
syntax :
xml : <WebView
android:id=“@+id/wview1”
……………./>
java :
WebView
wview=(WebView)findViewById(R.id.wview1);
- call browser
- integrate the browser in our application.
- display static .html files.
- we can provide a communication between HTML UI and
Android Activity.
http://www.igaptechnologies.com Page 16
Android Notes iGAP Technologies
permission :
<uses-permission
android:name=“android.permission.INTERNET”/>
wview.setWebViewClient(new WebViewClient());
- by default web view will not enable Javascript and zoom controls, to
enable java script and zoom controls set the following methods to web
view component.
wview.getSettings().setJavaScriptEnable(true);
wview.getSettings().setBuiltinZoomControls(true);
- to display html files place the .html file in assets folder, use the
following code to display .html files.
wview.loadUrl(“file:///android_asset/file_name.html”);
http://www.igaptechnologies.com Page 17
Android Notes iGAP Technologies
- wview.addJavaScriptInterface(class_object,”interface_name”);
- which method you are calling from Java Script, add the following
annotation on top the method.
@JavaScriptInterface
fragments :
<fragment <FrameLayout
android:id=“@+id/frag1” (or)
android:id=“@+id/fram1”
……………….. />
………………. />
http://www.igaptechnologies.com Page 18
Android Notes iGAP Technologies
Storage Methods :
- Shared Preferences
-
- by using SPF we can maintain huge amount of data, but for every
value we have to specify a unique key its difficult to assign and
remember the keys thats why SPF is preferred to maintain limited data
(e.g. Pattern lock key, Alarm time, login credentials …) , if you want to
maintain the huge amount of data Android is preferred to use
SQLiteDatabase.
http://www.igaptechnologies.com Page 19
Android Notes iGAP Technologies
SQLite Database :
- SQLiteDatabase is used to maintain structure data in
Android.
- SQLiteDatabase class is used to manage SQLiteDB,
openOrCreateDatabase(db_name,mode,cursor_factory) method is used
to get the object of SQLiteDB.
SQLiteDatabase dBase=openOrCreateDatabase(db_name,
mode,cursor_factory);
- query(); // read
- insert();
- update();
- delete();
- apart from the builtin methods we can execute the native sql
statements by using the following methods.
execSQL(sql_query); // C I U D
rawQuery(sql_query); // R
- by using SQLite browser we can explore the data from SQLite DB file.
Android Telephony :
- SMS
- Calls
- Emails
http://www.igaptechnologies.com Page 20
Android Notes iGAP Technologies
- Builtin Activity
- Java Mail API
SMS :
- android.telephony.SmsManager class is used to send text
messages in Android.
SmsManager sManager=SmsManager.getDefault();
sManager.sendTextMessage(rec_num,send_num,message,
sent_intent,
delevery_intent);
permission :
<uses-permission
android:name=“android.permission.SEND_SMS”/>
Call :
permission :
<uses-permission android:name=“android.permission.
CALL_PHONE” />
http://www.igaptechnologies.com Page 21
Android Notes iGAP Technologies
permissions :
INTERNET
Attachment functionality :
http://www.igaptechnologies.com Page 22
Android Notes iGAP Technologies
AsyncTask :
http://www.igaptechnologies.com Page 23
Android Notes iGAP Technologies
StrictMode.ThreadPolicy policy=
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Android Media :
- MediaPlayer
- VideoView
- AudioRecording
- VideoRecording
- Camera & Gallery
MediaPlayer :
http://www.igaptechnologies.com Page 24
Android Notes iGAP Technologies
MediaPlayer mPlayer=
MediaPlayer.create(context,R.raw.audio_file);
storage | network :
Handler :
VideoView :
xml :
<VideoView
android:id=“@+id/vview1”
……………………../>
java :
VideoView
vview=(VideoView)findViewById(R.id.vview1);
vview.setVideoPath(file_url | network_url);
vview.start();
vview.setMediaController(new MediaController(this));
Audio Recording :
AMR_NB);
http://www.igaptechnologies.com Page 26
Android Notes iGAP Technologies
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.
AMR_NB);
recorder.setOutputFile(“file_loc/file_name.amr”);
recorder.prepare();
recorder.start();
permissions :
RECORD_AUDIO
WRITE_EXTERNAL_STORAGE
Note :
- android is not providing any api support to pause record
an audio , to provide pause functionality maintain the temp files and
combine all these temp files into a single file when user selects stop.
Video Recording :
(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(profile);
recorder.setOutputFile(“file_loc/file_name.mp4”);
by using the above properties we can record video but while we are
recording video we have to show the video preview to the user to show
the video preview we use an advanced UI component called
SurfaceView.
http://www.igaptechnologies.com Page 27
Android Notes iGAP Technologies
<SurfaceView
android:id=“@+id/sview”
……………../>
SurfaceHolder sHolder=sview.getHolder();
recorder.setPreviewDisplay(sHolder.getSurface());
recorder.prepare()
recorder.start();
permissions :
CAMERA
RECORD_AUDIO
WRITE_EXTERNAL_STORAGE
Camera & Gallery :
Camera :
Intent i=new
Intent(“android.media.action.IMAGE_CAPTURE”);
startActivityForResult(i,request_code(123));
Gallery :
- Gallery is one of the builtin Activity in Android use
implicit intents to call builtin Activity.
i.setType(“image/*”);
startActivityForResult(i,request_code(124));
Service :
http://www.igaptechnologies.com Page 29
Android Notes iGAP Technologies
Broadcast Receiver :
- Broadcast Receiver is registered to get the system announcements.
e.g. : charger connected/disconnected , screen on/off,
making/receiving call, headset plugin , battery
low…. etc..
Content Provider :
(context,xml_file,cursor_obj,from,to);
ui_comp.setAdapter(adapter);
- from : String[]{DB_COLUMN1 , DB_COLUMN2}
- to : int[] {R.id.ui_comp1,R.id.ui_comp2}
permissions :
http://www.igaptechnologies.com Page 31
Android Notes iGAP Technologies
<uses-permission android:name=“android.permission.
READ_CONTACTS”/>
<uses-permission android:name=“android.permission.
READ_CALLOG”/>
Builtin Services :
- Notification Service
- Location Service
- Sensor Service
- Wifi Service
- Bluetooth Service
- Vibrator
Notification Service :
- every android device is having a notification bar we can
display notifications on Notification bar using Notification Service.
- getSystemService(Context.NOTIFICATION_SERVICE)
method is used to get the builtin notification service into Activity,
application framework is providing a class called NotificationManager
to manage notification service.
NotificationManager nManager=(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompact.Builder builder=new
NotificationCompact.Builder();
http://www.igaptechnologies.com Page 32
Android Notes iGAP Technologies
Location Service :
- getSystemService(Context.LOCATION_SERVICE) method
is used to get the builtin location service into Activity application
framework is providing a class called LocationManager to manage
location service.
LocationManager lManager=(LocationManager)
getSystemService(Context.LOCATION_SERVICE);
http://www.igaptechnologies.com Page 33
Android Notes iGAP Technologies
Sensor Service :
getSystemService(Context.SENSOR_SERVICE)
method is used to get the builtin sensor service into Activity , application
framework is providing a class called SensorManager to manage
SensorService.
SensorManager sManager=(SensorManager)
getSystemService(Context.SENSOR_SERVICE);
sManager.registerListener(new SensorListener(){
public void onSensorChanged(float[] values){
}
public void onAccuracyChanged(int accuracy){
}
},SENSOR_TYPE);
Wifi Service :
- getSystemService(Context.WIFI_SERVICE) method is
used to get the builtin wifi service into Activity, application framework
is providing a class called WifiManager to manage wifi service.
WifiManager wManager=(WifiManager)
http://www.igaptechnologies.com Page 34
Android Notes iGAP Technologies
getSystemService(Context.WIFI_SERVICE);
List<ScanResult> results=wManager.getScanResults();
for(ScanResult result:results){
result.SSID ; // name
result.frequency; // signal strength
}
- wManager.getConfiguredNetworks() method is used to get the list of
connected wifi devices .
List<WifiConfiguration> list=wManager.getConfiguredNetworks();
for(WifiConfiguration config:list)
{
config.SSID; // name
config.status; // connected / not-connected
}
permissions :
ACCESS_WIFI_STATE
CHANGE_WIFI_STATE
Vibrate :
Vibrator vib=(Vibrator)getSystemService(Context.VIBRATOR_
SERVICE);
vib.vibrate(milli_seconds);
http://www.igaptechnologies.com Page 35
Android Notes iGAP Technologies
permissions :
VIBRATE
Bluetooth :
In android there are different ways to manage BluetoothService
one of the best approach is manage BT service using BluetoothAdapter.
BluetoothAdapter bAdapter=BluetoothAdapter.getDefault();
data.getParcableExtra(BluetoothDevice.EXTRA_DEVICE);
device.getName();
device.getAddress();
}
}
http://www.igaptechnologies.com Page 36
Android Notes iGAP Technologies
permissions :
BLUETOOTH
BLUETOOTH_ADMIN
Dialogs :
- Custom Dialog
- Alert Dialog
- DatePicker
- TimePicker
- Progress Dialog [ Indeterminent /
determinent ]
Custom Dialog :
- by using custom dialog we can create our own UI & display
the UI on dialog.
Button b=(Button)d.findViewById(R.id.XXXX);
AlertDialog :
http://www.igaptechnologies.com Page 37
Android Notes iGAP Technologies
DialogInterface.OnClickListener listener=
new
DialogInterface.OnClickListener(){
public void onClick(DialogInterface di,int what)
{
// what will tell the button type.
}
};
DatePickerDialog :
http://www.igaptechnologies.com Page 38
Android Notes iGAP Technologies
DatePickerDialog.OnDateSetListener listener=
new DatePickerDialog.OnDateSetListener() {
public void onDateChanged(int year,int month,int day)
{
// logic when date changed..
}
}
TimePickerDialog :
TimePickerDialog is a child of android.app.Dialog which is
used to get time as input from the user.
TimePickerDialog.OnTimeSetListener listener=
new TimePickerDialog.OnTimeSetListener()
{
public void onTimeChanged(int hour,int minute)
{
// logic when time changed..
}
}
ProgressDialog :
There are 2 types of Progress dialogs.
- Indeterminent
- Determinant
http://www.igaptechnologies.com Page 39
Android Notes iGAP Technologies
- if you can‟t calculate the time, how much time it will take to
complete a specific task use Indeterminent progress dialog, if you can
calculate the time use determinant progress dialog.
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//
Determinent
pDialog.show();
WebServices :
Google APIs :
- Google Maps
- Google Places
- Google Cloud Messaging
- Firebase
- Material Design
http://www.igaptechnologies.com Page 40
Android Notes iGAP Technologies
Graphics :
- Graphics Programming
- “ “ + Cocos - 2D
Material Design :
- RecyclerView + CardView
( ListView , Gallery , GridView )
Custom Adapter
</android.support.v7.widget.RecyclerView>
- get the RecyclerView component from activity xml to java.
RecyclerView rview=(RecyclerView)findViewById(R.id.rview1);
(context,num_columns); // GridView
- rView.setLayoutManager(lManager | gManager);
RecyclerView.Adapter<ViewHolder class_obj>
rview.setAdapter(new MyAdapter());
http://www.igaptechnologies.com Page 42
Android Notes iGAP Technologies
FloatingActionButton [FAB] :
- create a project add design library as a dependency library.
- in Activity XML, create root UI group as CoordinatorLayout.
- create a FAB in Activity XML.
<android.support.design.widget.CoordinatorLayout
android:id="@+id/crl1"
>
<android.support.design.widget.FloatingActionButton
android:id=“@+id/fab1”
/>
</android.support.design.widget.CoordinatorLayout>
FAB fab=(FAB)findViewById(R.id.fab);
- SnackBar :
- SnackBar is similar to Toast message to display message
on the screen for few seconds.
SnackBar s=SnackBar.make(coordinatorlayout_id,message,
SnackBar.LENGTH_LONG);
s.show();
Menu :
- for displaying the menu options configure the menu items in the
following XML.
http://www.igaptechnologies.com Page 43
Android Notes iGAP Technologies
<menu>
<item
android:title=“text”
android:icon=“@drawable/image_name”/>
<item
android:title=“text”
android:icon=“@drawable/image_name”/>
</menu>
- onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.xml_file,menu);
} // used for displaying menu options.
- onOptionsItemSelected(MenuItem item){
// method will be called by selecting menu item.
}
http://www.igaptechnologies.com Page 44