Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Android by Somenath Mukhopadhyay +91 9748185282 [email protected] [email protected] What is ANDROID • ➔ • - Google's Android is an open-source platform that's currently available on a wide variety of smart phones. - Android is a software stack for mobile devices that includes an operating system, middleware and key applications. Advantages of Android - Full phone software stack including applications - Designed as a platform for software development - Android is open - Android is free - Community support - 100% Java Phone ➔ History of Android July 2005 Google acquired Android Inc. Nov 2007 Open HandSet Alliance formedGoogle, HTC, Intel, Motorola, Qualcomm,T-Mobile Android is the OHA first product Nov 2007 OHA released a preview of the Android OHA ANDROID ARCHITECTURE The software stack is split into Four Layers: • • • • - The The The The application layer application framework libraries and runtime kernel LINUX KERNEL The architecture is based on the Linux2.6 kernel. Android use Linux kernel as its hardware abstraction layer between the hardware and rest of the software. It also provides memory management, process management, a security model, and networking, a lot of core operating system infrastructures that are robust and have been proven over time NATIVE LIBRARIES •The next level up is the native libraries. Everything that you see here in green is written in C and C++. Android Run Time The Android Runtime was designed specifically for Android to meet the needs of running in an embedded environment where you have limited battery, limited memory, limited CPU. The DVM runs something called dex files, DEX and these are byte codes that are the results of converting at build time. Class and JAR Files. Android Run Time •This is in blue, meaning that it's written in the Java programming language. •The core library contains all of the collection classes, utilities, IO, all the utilities and tools that you’ve come to expected to use. Application Framework This is all written in a Java programming language and the application framework is the toolkit that all applications use. •These applications include the ones that come with a phone like the home applications, or the phone application. •It includes applications written by Google, and it includes apps that will be written by you. •So, all apps use the same framework and the same APIs. APPLICATION LAYER • And the final layer on top is Applications. •This is where all the applications get written. •It includes the home application, the contacts application, the browser, and your apps. •And everything at this layer is, again, using the same app framework provided by the layers below. Android Building Blocks These are the most important parts of the Android APIs: AndroidManifest.xml -the control file-tells the system what to do with the top-level components Activities -an object that has a life cycle-is a chunk of code that does some work Views -an object that knows how to draw itself to the screen Intents -a simple message object that represents an "intention" to do something Notifications -is a small icon that appears in the status bar(SMS messages) -for alerting the user Services -is a body of code that runs in the background Android App Development ● Android SDK ● Android Studio ● Eclipse with ADT plugin ● Development in emulator ● Development in Real Device A HelloWorld Application in Android Studio AndroidManifest.xml ➔ Build.gradle ➔ MainActivity.java ➔ AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.som_itsolutions.myapplication" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> build.gradle apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "com.som_itsolutions.myapplication" minSdkVersion 16 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguardrules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.1' } Android Build Process Overview Gradle build system build.gradle – standard name for build file ➔ Comprises of tasks ➔ task compileTask << { ➔ System.out.println "compiling..." } ➔ Type gradle -q tasks Result will be ➔ Other tasks ----------compileTask Gradle Build system To run the previous compileTask type gradle -q compileTask A simple build.gradle file defaultTasks 'buildTask' task compileTask << { System.out.println "compiling..." } task buildTask (dependsOn:compileTask) << { System.out.println "building..." } build.gradle - summary build.gradle is the file where we write the build scripts ➔It consists of several tasks ➔However, we don't have to write all these tasks manually. ➔Just like an IDE, different plugins have been developed for all the build tasks in a specific environment ➔ Gradle build system - plugin Examples of Plug-in Java Plug-in ➔ Android Plug-in ➔ Gradle plug-ins The right plug-in is included through the following text in the build.gradle file ➔ apply plugin: <plugin-name> For Java - apply plugin: "java" ➔ For Android - 'com.android.application' ➔ Gradle & Android Studio ➔ ➔ ➔ Open a Project Press F4 to go to the settings We can add libraries and stuffs like this Android App Basics - Layout - XML ● LinearLayout ● FrameLayout ● RelativeLayout ● TableLayout An App to showcase Layout – A Keypad Dialer ● Example Getting Notification through Toast ● Example Android Activity LifeCycle Android App Basics - Traversing from one Screen to another ● We use Intent to do this ● We can pass data from one activity to another ● ● ● ● One Activity (Screen) launches another activity (Screen) and don't wait for the response One Activity (Screen) launches another activity (Screen) and wait for data sent back from the later Intent Resolution – Explicit Intent Resolution – Implicit Intent Resolution Example – ExperimentationWithIntent Android App Basics – Declaring a Second Activity <activity android:name="com.somitsolutions.training.android.e xperimentationwithintent.SecondActivity" > <intent-filter> <action android:name="com.somitsolutions.training.android.experim entationwithintent.TEST" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> </activity> Android App Basics – Traversing from one activity to other ● Explicit Intent Resolution Intent i = new Intent(getApplicationContext(),SecondActivity.clas s); ● Implicit Intent Resolution Intent i = new Intent(); i.setAction("com.somitsolutions.training.android.e xperimentationwithintent.TEST"); i.setType("text/plain"); Android App Basics – Passing Data from One Activity to Other ● ● Intent i = new Intent(getApplicationContext(),SecondActivity.cl ass); i.putExtra("Key0", "This is the first String Data..."); ● i.putExtra("Key1", 10); ● startActivity(i); Android App Basics – Passing Data from One Activity to Other and waiting for the result ● ● Intent i = new Intent(getApplicationContext(),SecondActivity.cl ass); i.putExtra("Key0", "This is the first String Data..."); ● i.putExtra("Key1", 10); ● startActivityForResult(i, mRequestCode); Callback for The Returned Result protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == mRequestCode){ if (resultCode == RESULT_OK){ String returnValue = data.getStringExtra("Key2"); Toast.makeText(getApplicationContext(), returnValue, Toast.LENGTH_SHORT).show(); } } } Extracting the Data in the Second Activity & Returning the Result String firstStringData = returnIntent.getStringExtra("Key0"); int secondIntData = returnIntent.getIntExtra("Key1", 0); returnIntent.putExtra("Key2", "This is the return result from the called Intent"); setResult(RESULT_OK, returnIntent); Toast.makeText(getApplicationContext(), "First Data:" + firstStringData + "Second Data:"+ Integer.toString(secondIntData), Toast.LENGTH_LONG).show(); finish(); Android UI - CheckBox ● Example Android UI - CheckBox <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/im_not_checked_string" android:textSize="24sp"/> Android UI - CheckBox // Get a reference to the CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox); // Set an OnClickListener on the CheckBox checkbox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Check whether CheckBox is currently checked // Set CheckBox text accordingly if (checkbox.isChecked()) { checkbox.setText("I'm checked"); } else { checkbox.setText("I'm not checked"); } } }); Android UI - Spinner ● Example Android UI - Spinner <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" /> Android UI - Spinner Spinner spinner = (Spinner) findViewById(R.id.spinner); // Create an Adapter that holds a list of colors ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.colors, R.layout.dropdown_item); // Set the Adapter for the spinner spinner.setAdapter(adapter); // Set an setOnItemSelectedListener on the spinner spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText( parent.getContext(), "The color is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); switch(pos){ case 0: view.getRootView().setBackgroundColor(Color.RED); break; ................... ................... Android UI – Spinner – dropdown_item.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="24sp" android:padding="5dp"> </TextView> Android UI – Radio Group ● Example Android UI – Radio Group <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/choice1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/choice_1_string" android:textSize="24sp"/> ..........Other Radio Buttons................ </RadioGroup> Android UI – Radio Group // Define a generic listener for all three RadioButtons in the RadioGroup final OnClickListener radioListener = new OnClickListener() { @Override public void onClick(View v) { RadioButton rb = (RadioButton) v; if(v.equals(choice1Red)){ v.getRootView().setBackgroundColor(Color.RED); } if(v.equals(choice2Green)){ v.getRootView().setBackgroundColor(Color.GREEN); } if(v.equals(choice3Blue)){ v.getRootView().setBackgroundColor(Color.BLUE); } } }; ............................ ........................... // Called when RadioButton choice1 is clicked choice1Red.setOnClickListener(radioListener); Android UI – Alert Dialog ● Example Thank you