Android App Development

siliconindia

Android App Development

Android App Development

G E T T I N G S T A R T E D


� Activity

� Intent

� Service

� View

Architecture Overview


Activity

� It’s a single screen with user interface

�� User interface is associated with a View

� Activity is an independent entity

� Activity can start other activities


Activity – Code 1

public class MainActivity extends Activity {

}

@Override

public void onCreate(Bundle savedInstanceState) {

}

super.onCreate(savedInstanceState);


View


View

� Layouts are most common ViewGroups.

� View represents how a UI component would be

rendered

� Layout will have details about how to organize the

child views.


View

� The layouts normally are defined in XML files. Although they can be

directly initialized as Java objects. They look something like this :










View


Activity

public class MainActivity extends Activity {

}

@Override

public void onCreate(Bundle savedInstanceState) {

}

super.onCreate(savedInstanceState);

setContentView(R.layout.main);


public class MainActivity extends Activity {

}

Activity

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

TextView textView = (TextView)findViewById(R.id.text);

Button button = (Button)findViewById(R.id.okbutton);

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// put the required logic

}

});


Service

� A service is an application component that runs in

the background

� Unlike Activity, a Service don’t have user interface

� Ideally suited for background job while you do other

stuff on phone

� Examples: Listening music, Email syncing, Movie

download etc


Service

public class OurFirstService extends IntentService {

}

@Override

protected void onHandleIntent(Intent intent) {

}


Intent

� Intents are way of communication between Activities

and Services

� There are 2 types of intents: Explicit & Implicit


Explicit Intent

� They carry exact information of the component

� Ideally suited for intra application activities and

services


public class MainActivity extends Activity {

}

Explicit Intent - Example

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

TextView textView = (TextView)findViewById(R.id.text);

Button button = (Button)findViewById(R.id.okbutton);

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent i = new Intent();

i.setClass(MainActivity.this, EmailList.class);

startActivity(i);

}

});


Intent i = new Intent();

Explicit Intent - Example

i.setClass(MainActivity.this, EmailUpdateService.class);

startService(i);


� There is no specific target

Implicit Intent

Android system figures out best entity to handle

intent at the runtime

� That best entity can be an activity, service or

broadcast receiver


Question

What are possible scenarios of Implicit Intents ?


Answer

� Sending emails on internet connect

� Working of custom Sms apps

� Opening & editing of docs/files

� Theme change by user


Question

How Android system figures out the best entity for a

implicit intent?


Intent Resolution

� Information is defined in an implicit intent

�� Intent filters can be defined on entities with

necessary information

Android system matches above 2 information and

makes selection


Implicit Intent – details

� Action (Ex: ACTION_CALL, ACTION_EDIT)

� Data (data URI & MIME type)

� Category (Ex: CATEGORY_BROWSABLE,

CATEGORY_LAUNCHER)

� Extras


Intent filter

There aspects are defined in Intent filter, which are

consulted at time of Intent resolution

� Action (necessary)

� Category

� Data


Implicit Intent - Broadcast

Intent feedReceiveNewsIntent = new Intent();

feedReceiveNewsIntent.setAction("com.example.project.FEED_RECEIVED");

feedReceiveNewsIntent.addCategory("com.example.project.NEWS_SERVICE");

feedReceiveNewsIntent.putExtra("feed title", "Reuters");

feedReceiveNewsIntent.putExtra("feed location", "/data/feedfiles/reuters.xml");

getApplicationContext().sendBroadcast(feedReceiveNewsIntent);

Intent feedReceiveAdminIntent = new Intent();

feedReceiveAdminIntent.setAction("com.example.project.FEED_RECEIVED");

feedReceiveAdminIntent.addCategory("com.example.project.ADMIN_NOTIFY_S

ERVICE");

feedReceiveNewsIntent.putExtra("feed title", "Reuters");

getApplicationContext().sendBroadcast(feedReceiveAdminIntent);


Intent Filter




Pending Intent

PendingIntents are intents that your app gives to

other apps to perform action on your behalf (with the

same permissions and identity)


AndroidManifest.xml

� Gateway between App & Android System

� Lists all the Activities, Services

� Intent filters

App icon and app title

� User permissions


AndroidManifest.xml


AndroidManifest.xml


AndroidManifest.xml


AlarmManager

� Intended for the cases when your app code needs to be

run on specific times, even when your app is not active

� Normally deals with Pending Intents


AlarmManager - SampleCode

AlarmManager mgr =

context.getSystemService(Context.ALARM_SERVICE);

Intent i = new Intent(context, OnAlarmReceiver.class);

PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

mgr.set(AlarmManager.RTC, startTime, pi);

mgr.setRepeating(AlarmManager.RTC, startTime, interval, pi);


SharedPreferences

� Inbuilt datastructure (Map) like persistence api

�� Useful for basic level persistence

� Saves time & effort compared to dealing with

text/xml files


SharedPreferences – Sample Code

SharedPreferences settings = context.getSharedPreferences("settings", 0);

boolean isLoggedIn = settings.getBoolean(“IsLoggedIn”, false);

String password = settings.getString(“PasswordValue”, Null);

SharedPreferences.Editor editor = settings.edit();

editor.putBoolean(“IsLoggedIn”, AdminScreen.isOpened());

editor.putString(“PasswordValue”, generatePassword());

editor.commit();


SQLite database support

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_NAME = “mydb.db";

public DataBaseHelper(Context context) {

}

super(context, DB_NAME, null, 1);

@Override

public void onCreate(SQLiteDatabase db) {}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}


SQLite database support

DataBaseHelper myDbHelper = new DataBaseHelper(this);

SQLiteDatabase myDB = myDbHelper.getReadableDatabase();

String selectQuery = “SELECT * FROM TABLE BOOKS”;

Cursor selectQueryResult = myDB.rawQuery(selectQuery , null);

while(cursor.moveToNext()) {

}

selectQueryResult.getString(bookNameColumnIndex);

selectQueryResult.getInt(bookPriceColumIndex);


Webview


webview = (WebView) findViewById(R.id.webview);

webview.getSettings().setJavaScriptEnabled(true);

webview.loadUrl("http://www.facebook.com");


Webview

private class HelloWebViewClient extends WebViewClient {

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);

return true;

}

}

webview.setWebViewClient(new HelloWebViewClient());


Android is not just mobile OS !


Then ?

• GPS

• Accelerometer

• Orientation sensor

• Internet

• Camera

• Audio & Video

Input/Output

• Operating System


And Cloud Computing….


Android & Open Accessory API

Makes easy for Android app to interact with handset

hardwore components : Sensors, Gyroscope,

Speakers, Memory, Touchscreen, Camera


Android & Cloud Services

� Object recognition (Google Goggles)

� Mapping & Navigation system service (sense of

location and turn by turn directions)

� Audio related services (voice recognition, optical

character recognition, text to speech)


Android & ROS

� ROS – Robot Operating System. Provides libraries &

tools to create robot applications.

� ROS team recently released ‘rosjava’ a pure Java based

open-source implementation which is Android

compatible.

� ROS can be used for writing apps in Android that can

work with motors, navigation, actuators


And Result….


Questions


Sachin Sharma

Co-founder, XStudio Technologies

Follow me at: gplus.to/sachinsharma

More magazines by this user
Similar magazines