Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, 14 May 2011

Android Basics Refresher


Covering the fundamentals of the hottest platform in mobile right now.
Reading through my Android notes today, I realised the quick-read format might be useful to others. Although fellow zoneleader James has been featuring some excellent articleson Android, I'd thought give my take in a simpler format - just in case it triggers your memory.
Android Basics
Every app;
  • runs in its own Linux process
  • shutdown when more resources needed
  • each process has its own VM
  • assigned its own user ID so its data is visible only to that user (and app)
  • can call upon components developed elsewhere to allow for reusability
Activity
An activity is a UI representing a task the user wants to perform, such as entering a phone number into a text-box, or a list of menu items the user must choose from. Applications are made up of many activities. A Java class must extend the Activity class.
Views, or 'widgets', are what make up the components in an activity. As subclasses of View, these represent standard items in a UI such as a button, textbox or Image.
Services, Broadcast Receivers and Content Providers Services
Run in the background. Extend the "Service" base class. The platform will start them by passing Intent to Context.startInstance(). "onStart()" will be called by Android on the instance of a new Service and an Intent passed to it. To get a hold of an established Service (i.e. one that's already running), you can call Context.bindService(); in such a scenario the Service is given an Intent instance and its onBind() method is called.
Broadcast Receivers
Receive and react to broadcast announcements. Examples would be battery is low, or the language has been changed. Extend BroadcastReceiver base class. They do not have a an interface and typically display an icon in the notification area.
Content Provider
Exposes one application's data to another. An extension of ContentProvider, these act on calls from ContentResolver (which other apps invoke) to access data controlled by this provider.
Intents
Activities are started by passing Intent instances to Context.startActivity() or Activity.startActivityForResult().

Enhanced by Zemanta

Android Basic Programming

Mac Desktop Feb 10Image by Aaronage via Flickr
Android is developing applications (software) for Android (mobile) devices.
- Is often referred to as the Open Handset Alliance
- It includes an operating system, middleware and key applications
- The mobile platform is available, for free, as the Android Open Source Project
The Android Web site is a good place to start.
Latest Version: Android 2.3
The Android Dev Guide provides a practical introduction to developing applications for Android.
Using Android
System Requirements
Windows XP (32-bit) or Vista (32- or 64-bit)
Mac OS X 10.4.8 or later (x86 only)
Linux (tested on Linux Ubuntu Dapper Drake)
Note: users should download Eclipse and Android SDK (1.6)
- Begin here by Installing the Android SDK
- Those using Eclipse, make sure to install the Android Development Tools (ADT) Plugin
Using Android SDK
- It provides the tools and APIs needed to begin developing applications.
Using Eclipse
- to edit files and verify that they compile
- used for error checking
- to check directories in search for Java files
Users can highlight and execute a piece of source code by pressing CTRL + SHIFT + D
Users can highlight or select text by pressing ALT + SHIFT + UP ARROW (or DOWN ARROW)
Programming
Android applications are written in the Java programming language, so learning a bit of Java is necessary.
Also, Android supports XML syntax. It's used for designing application screens.
Need help getting started? There's a helpful forum (blog) that users can learn Android Basic Programming. The site: Android Competency Center. It contains Android tutorials and tips.
For users wanting to build graphical user interfaces for the Android cell phone platform, there's DroidDraw.
Android uses the aapt tool into an Android package, an archive file marked by an .apk suffix.
Using the Android Asset Packaging Tool
./aapt (used by Linux and Mac OS)
aapt.exe (used by Windows)
- Is included in the tools directory of the SDK
- It allows you to view, create, and update Zip-compatible archives (zip, jar, apk)
Building the code
Note: To build the files, run make from within your working directory.
$ cd ~/mydroid
$ make
Note: If users are missing the "run-java-tool," try setting the ANDROID_JAVA_HOME env var to $JAVA_HOME
$ export ANDROID_JAVA_HOME=$JAVA_HOME
To Create an AVD (Android Virtual Devices)
android create avd --target 2 --name my_avd
To Select, Add, Delete, and Modify Data - use the content provider URI (CONTENT_URI)
The CONTENT_URI consist of three parts:
The string “content://”
The segment representing the kind of data to retrieve
The specific item of the specified content provider
Importing (examples):
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.preference.ListPreference;
import android.preference.Preference;
import android.util.AttributeSet;
To Ask for a User's Name:
androidid=”@+id/nameMessage”
Examples of how to define the activity entry in the AndroidManifest.xml:
android:name=”DialogActivity”
android:label=”@string/activity_dialog”
android:theme=”@android:style/Theme.Dialog”>
…
</activity>
To Create a Dialog:
MyDialog dialog = new MyDialog(context); dialog.show();
Using AlertDialog:
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setMessage(”Do you play golf?”); dialog.setButton(”Yes”, myOnClickListener); dialog.setButton2(”No”, myOnClickListener);
dialog.show();
To Add a Record:
private void insertRecords(String name, String phoneNo) {
ContentValues values = new ContentValues();
values.put(People.NAME, name);
values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
values.put(People.NUMBER, phoneNo);
}
To Display a Record:
private void displayRecords() {
String columns[] = new String[] { People.NAME, People.NUMBER };
String name = null;
String phoneNo = null;
name = cur.getString(cur.getColumnIndex(People.NAME));
phoneNo = cur.getString(cur.getColumnIndex(People.NUMBER));
}
To Modify a Record:
private void updateRecord(int recNo, String name) {
ContentValues values = new ContentValues();
values.put(People.NAME, name);
}
To Delete a Record:
private void deleteRecords() {
Uri uri = People.CONTENT_URI;
getContentResolver().delete(uri, null, null);
}

Enhanced by Zemanta