안드로이드 개발 메뉴얼

2010.05.23 11:57

가을의 곰을... 조회 수:8033

Android Development Tutorial

Lars Vogel

Version 2.5

10.05.2010

Revision History
Revision 0.1 04.07.2009 Lars Vogel
Created
Revision 0.2 08.07.2009 Lars Vogel
First working example
Revision 0.3 30.08.2009 Lars Vogel
Improved description
Revision 0.4 30.10.2009 Lars Vogel
Updated to use Android 2.0
Revision 0.5 01.11.2009 Lars Vogel
Started XML support
Revision 0.6 02.11.2009 Lars Vogel
Added the android log view
Revision 0.7 03.11.2009 Lars Vogel
Access to the shell access, uninstall android application
Revision 0.8 04.11.2009 Lars Vogel
Networking
Revision 0.9 05.11.2009 Lars Vogel
Added the usage of SharedPreferences
Revision 1.0 06.11.2009 Lars Vogel
Usage of ContentProvider
Revision 1.1 10.11.2009 Lars Vogel
Assign handler to button via XML (thanks to Jason Arora for the tip)
Revision 1.2 21.11.2009 Lars Vogel
Emulator console added
Revision 1.3 22.11.2009 Lars Vogel
Authorization mentioned
Revision 1.4 25.11.2009 Lars Vogel
Google API, location API, MapView
Revision 1.5 13.12.2009 Lars Vogel
Fixed text for button assignment
Revision 1.6 30.12.2009 Lars Vogel
Fixed typo
Revision 1.7 13.01.2010 Lars Vogel
Updated screenshot
Revision 1.8 08.02.2010 Lars Vogel
Updated to Android 2.1
Revision 1.9 09.02.2010 Lars Vogel
Added info how to deploy on real device
Revision 2.0 14.02.2010 Lars Vogel
how to add menus, how to work with preferences
Revision 2.1 22.02.2010 Lars Vogel
Added missing class "Preferences"
Revision 2.2 29.03.2010 Lars Vogel
Fixed content provider
Revision 2.3 30.03.2010 Lars Vogel
General rework
Revision 2.4 08.05.2010 Lars Vogel
Fixed typo
Revision 2.5 10.05.2010 Lars Vogel
Fixed typo

Development with Android and Eclipse

This article describes how to create Android applications with Eclipse.

The article is based on Eclipse 3.5 and Android 2.1.


Table of Contents

1. Android Development
1.1. Android Operation System
1.2. Android Application
2. Installation
2.1. Eclipse
2.2. Android
2.3. Configuration
2.4. Device
3. Your first Android project
3.1. Create Project
3.2. Add UI Elements
3.3. Create and use attributes
3.4. Code your applicatioin
3.5. Define the button handler
3.6. Start Project
3.7. Using the phone menue
4. Menu and Preferences
4.1. Project
4.2. Add a menu
4.3. Using preferences
4.4. Run
5. ContentProvider
5.1. Overview
5.2. Create Contacts
5.3. Create contacts on your emulator
5.4. Example
6. Networking
6.1. Networking
6.2. Proxy
6.3. Permissions
6.4. Example
7. Important views
7.1. Log
7.2. File explorer
8. Shell
8.1. Opening the Shell
8.2. Emulator Console
8.3. Uninstall an application
9. Location API
9.1. Device with Google API
9.2. Project and Permissions
9.3. Google Map library
9.4. Layout
9.5. Activity
9.6. Run and Test
10. Deploy your application on a real device
11. Thank you
12. Questions and Discussion
13. Links and Literature
13.1. Source Code
13.2. Android Resources
13.3. vogella Resources
<script type="text/javascript"> // ord=Math.random()*10000000000000000; document.write('<script type="text/javascript" src="http://ad.doubleclick.net/adj/vogella_upv/;sz=728x90;ord=' + ord + '?"><\/script>'); // </script> <script type="text/javascript" src="http://ad.doubleclick.net/adj/vogella_upv/;sz=728x90;ord=2267608719220231.5?"></script> <script type="text/javascript" src="http://ad.adserverplus.com/st?ad_type=ad&ad_size=728x90&section=967202"></script> <script type="text/javascript" src="http://ad.adserverplus.com/imp?Z=728x90&s=967202&_salt=2521216353&B=10&u=http%3A%2F%2Fwww.vogella.de%2Farticles%2FAndroid%2Farticle.html&r=1"></script>

1. Android Development

1.1. Android Operation System

Android is an operating system based on Linux with a Java programming interface. It provides tools, e.g. a compiler, debugger and a device emulator as well as its own Java Virtual machine (Dalvik). Android is created by the Open Handset Alliance which is lead by Google.

Android uses a special Java virtual machine (Dalvik) which is based on the Apache Harmony Java implementation. Dalvik uses special bytecode there you cannot run standard Java program on Android but you have to use the Android compiler to create this special byte-code.

Android supports 2-D and 3-D graphics using the OpenGL libraries and supports data storage in a SQLLite database.

For development Google provides the Android Development Tools (ADT) for Eclipse to develop Android applications.

1.2. Android Application

An Android application consists out of the following parts:

  • Activity - A screen in the Android application

  • Intent / Broadcast Receiver - allow the application to request and / or provide services from other application. For example the application call ask via an intent for a contact application. Application register themself via an IntentFilter

  • Services - Background activities without UI

  • Content Provider - provides data to applications, Android contains a SQLLite DB which can serve as data provider

  • An Android application is described the file "AndroidManifest.xml". This files contains all activities application and the required permissions for the application. For example if the application requires network access it must be specified here. "AndroidManifest.xml" can be thought as the deployment descriptor for an Android application.

    2. Installation

    The following assume that you have already Eclipse installed. For details please see Eclipse Tutorial

    You need to install the "Android development Tools" (ADT) for Eclipse and the base Android SDK. Afterwards you can install different Android versions via the ADT. After the installation you configure a device which will be used to emulate your real device.

    2.1. Eclipse

    Use the update manager of Eclipse to install all available plugins for the Android Development Tools (ADT) from the URL https://dl-ssl.google.com/android/eclipse/ . See Using the Eclipse update manager for details on how to use the update manager and how to install new plugins.

    Tip

    The Eclipse Android SDK does not provide the Android (Java) source code in Eclipse. Please join me in starring at bug Make Android Source available in Eclipse - Bug report .

    2.2. Android

    Download the Android SDK from the Android homepage under Android SDK download .

    The download contains a zip file which you can extract to any place in your file system, e.g. I placed it under "c:\android-sdk-windows" .

    2.3. Configuration

    In Eclipse open the Preferences dialog via Windows -> Preferences. Select Android and maintain the installation path of the Android SDK.

    Tip

    If you maintain the location the Android plugin will remind you frequently (and for every workspace). Join me in starring at Bug 3210 to get this improved.

    Select now Window -> Android SDK and AVD Manager from the menu.

    Select available packages and select the latest version of the SDK.

    Press "Install selected" and confirm the license for all package.

    After the installation restart Eclipse.

    2.4. Device

    You need to define a device which can be used for emulation. Press the device manager button, press "New" and maintain the following.

    Press "Create AVD".

    To test if you setup is correct, eelect your device and press "Start".

    After (a long time) your device should be started.

    Tip

    You can use the perspective "DDMS" to monitor your device.

    <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script>google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);</script> <iframe style="POSITION: absolute; TOP: 0px; LEFT: 0px" id="google_ads_frame2" height="90" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3851443674951530&output=html&h=90&slotname=8639282357&w=728&lmt=1274474832&flash=10.0.45.2&url=http%3A%2F%2Fwww.vogella.de%2Farticles%2FAndroid%2Farticle.html&dt=1274583031031&shv=r20100505&prev_slotnames=3391820284&correlator=1274583028609&frm=0&ga_vid=567174394.1274583028&ga_sid=1274583028&ga_hid=1776702759&ga_fc=1&u_tz=540&u_his=0&u_java=1&u_h=800&u_w=1280&u_ah=770&u_aw=1280&u_cd=32&u_nplug=0&u_nmime=0&biw=608&bih=378&eid=33895100&ref=http%3A%2F%2Fwww.google.co.kr%2Fsearch%3Fq%3Dandroid%2Bprogramming%26hl%3Dko%26newwindow%3D1%26complete%3D1%26prmd%3Divnlb%26start%3D10%26sa%3DN&fu=0&ifi=2&dtd=47&xpc=MOk6H0oL1h&p=http%3A//www.vogella.de" frameBorder="0" width="728" allowTransparency name="google_ads_frame" marginWidth="0" scrolling="no"></iframe>

    3. Your first Android project

    3.1. Create Project

    . Select File -> New -> Other -> Android -> Android Project and create the Android project "de.vogella.android.first" Maintain the following.

    Tip

    I think this wizard should have the option to add the project to an existing working set. Please stare at Android New Project Wizard should have the option to add to Working set to get this functionality.

    Press "Finish".

    This should create the following directory structure.

    "R.java" is a generated class which contains the text and the UI elements. Please do not try to modify this class manually.

    3.2. Add UI Elements

    Select "layout/main.xml" and open the editor via double-click. The result should look like the following.

    From the "Views" bar, drag in an "EditText" and three "Buttons". The result should look like the following.

    Tip

    Check the file "R.java". It will contain your new elements.

    3.3. Create and use attributes

    Select "values/string.xml" and press "Add".

    Select "Color" and maintain "white" as the name and "#FFFFFF" as the value.

    Go back to "main.xml", select the complete widget and use the Properties view to set the background to this attribute.

    3.4. Code your applicatioin

    Change your code in "Hello.java" to the following.

    				
    package de.vogella.android.first;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    
    public class Hello extends Activity {
    
    	private EditText text;
    
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main); // bind the layout to the activity
    		text = (EditText) findViewById(R.id.EditText01);
    		text.setText("No button pressed");
    
    	}
    
    	// Will be connected with the buttons via XML
    	public void myClickHandler(View view) {
    		switch (view.getId()) {
    		case R.id.Button01:
    			text.setText("Button 1 was clicked");
    			break;
    		case R.id.Button02:
    			text.setText("Button 2 was clicked");
    			break;
    
    		case R.id.Button03:
    			text.setText("Button 3 was clicked");
    			break;
    		}
    	}
    }
    
    			

    Tip

    The next chapter will connect the handler methods with the buttons via XML.

    3.5. Define the button handler

    Open again "main.xml" and select your first button. Via the property view assign the method "myClickHandler" to the "on Click" property of the first button.

    Assign "myClickHandler" also to the other buttons and switch to the "main.xml" tab.

    The resulting "main.xml" file should look like the following.

    				
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/white">
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    <EditText android:text="@+id/EditText01" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
    <Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myClickHandler"></Button>
    <Button android:text="@+id/Button02" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myClickHandler"></Button>
    <Button android:text="@+id/Button03" android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myClickHandler"></Button>
    </LinearLayout>
    
    			

    3.6. Start Project

    To start the Android Application, select your project, right click on it, Run-As-> Android Application

    Tip

    Be patient, the emulator is sometimes very slow.

    You should get the following result.

    3.7. Using the phone menue

    If you press the Home button you can also select your application.

    <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script>google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);</script> <iframe style="POSITION: absolute; TOP: 0px; LEFT: 0px" id="google_ads_frame3" height="15" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3851443674951530&output=html&h=15&slotname=3607372071&w=728&lmt=1274474832&flash=10.0.45.2&url=http%3A%2F%2Fwww.vogella.de%2Farticles%2FAndroid%2Farticle.html&dt=1274583031109&shv=r20100505&prev_slotnames=3391820284%2C8639282357&correlator=1274583028609&frm=0&ga_vid=567174394.1274583028&ga_sid=1274583028&ga_hid=1776702759&ga_fc=1&u_tz=540&u_his=0&u_java=1&u_h=800&u_w=1280&u_ah=770&u_aw=1280&u_cd=32&u_nplug=0&u_nmime=0&biw=608&bih=378&eid=33895100&ref=http%3A%2F%2Fwww.google.co.kr%2Fsearch%3Fq%3Dandroid%2Bprogramming%26hl%3Dko%26newwindow%3D1%26complete%3D1%26prmd%3Divnlb%26start%3D10%26sa%3DN&fu=0&ifi=3&dtd=47&xpc=BTeQTrPLFB&p=http%3A//www.vogella.de" frameBorder="0" width="728" allowTransparency name="google_ads_frame" marginWidth="0" scrolling="no"></iframe>

    4. Menu and Preferences

    4.1. Project

    We want now to create an application which displays a menu. If the user select the menu he should be able to maintain preferences.

    Create a project "de.vogella.android.preferences" with the activity "HelloPreferences".

    4.2. Add a menu

    Select your project, right click on it and select New -> Other -> Android -> "Android XML File".

    Press Add and select "Item". Maintain the following values.

    Change your class "HelloPreferences" to the following.

    				
    package de.vogella.android.preferences;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuInflater;
    
    public class HelloPreferences extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
        	MenuInflater inflater = getMenuInflater();
        	inflater.inflate(R.menu.menu, menu);
        	return true;
        }
    }
    			

    If you run your application and press "Menu" on the emulator. Your menu should alread be displayed.

    4.3. Using preferences

    Create another "Android XML File" this time "preferences.xml".

    Press Add and add two "EditTextPreferences": "User" and "Password".

    Create the class "Preferences" which will load the "preference.xml".

    				
    package de.vogella.android.preferences;
    
    import android.os.Bundle;
    import android.preference.PreferenceActivity;
    
    public class Preferences extends PreferenceActivity {
    
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    	    super.onCreate(savedInstanceState);
    	    addPreferencesFromResource(R.xml.preferences);
    	}
    	
    	
    
    }
    
    			

    Select "AndroidManifest.xml" and the tab "Application". Add the activity "Preferences".

    To use the preferences add a button to your main.xml with the id "@+id/Button01" and change the coding of HelloPreferences to the following.

    				
    package de.vogella.android.preferences;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class HelloPreferences extends Activity {
    	SharedPreferences preferences;
    
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		Button button = (Button) findViewById(R.id.Button01);
    		// Initialize preferences
    		preferences = PreferenceManager.getDefaultSharedPreferences(this);
    		
    		button.setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				String username = preferences.getString("username", "n/a");
    				String password = preferences.getString("password", "n/a");
    				Toast.makeText(HelloPreferences.this,
    						"You maintained user: " + username + " and password: " + password,
    						Toast.LENGTH_LONG).show();
    
    			}
    		});
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		MenuInflater inflater = getMenuInflater();
    		inflater.inflate(R.menu.menu, menu);
    		return true;
    	}
    
    	// This method is called once the menu is selected
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		switch (item.getItemId()) {
    		// We have only one menu option
    		case R.id.preferences:
    			// Launch Preference activity
    			Intent i = new Intent(HelloPreferences.this, Preferences.class);
    			startActivity(i);
    			// A toast is a view containing a quick little message for the user.
    			Toast.makeText(HelloPreferences.this,
    					"Here you can maintain your user credentials.",
    					Toast.LENGTH_LONG).show();
    			break;
    
    		}
    		return true;
    	}
    }
    			

    4.4. Run

    Run your application. Select your menu "Preferences". You should be able to maintain your user settings and if you press the button in your main activities the maintained valued should be displayed in a small message windows (Toast).

    <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script>google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);</script> <iframe style="POSITION: absolute; TOP: 0px; LEFT: 0px" id="google_ads_frame4" height="15" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3851443674951530&output=html&h=15&slotname=9752573896&w=728&lmt=1274474832&flash=10.0.45.2&url=http%3A%2F%2Fwww.vogella.de%2Farticles%2FAndroid%2Farticle.html&dt=1274583031187&shv=r20100505&prev_slotnames=3391820284%2C8639282357%2C3607372071&correlator=1274583028609&frm=0&ga_vid=567174394.1274583028&ga_sid=1274583028&ga_hid=1776702759&ga_fc=1&u_tz=540&u_his=0&u_java=1&u_h=800&u_w=1280&u_ah=770&u_aw=1280&u_cd=32&u_nplug=0&u_nmime=0&biw=608&bih=378&eid=33895100&ref=http%3A%2F%2Fwww.google.co.kr%2Fsearch%3Fq%3Dandroid%2Bprogramming%26hl%3Dko%26newwindow%3D1%26complete%3D1%26prmd%3Divnlb%26start%3D10%26sa%3DN&fu=0&ifi=4&dtd=16&xpc=A5pC9gyjbT&p=http%3A//www.vogella.de" frameBorder="0" width="728" allowTransparency name="google_ads_frame" marginWidth="0" scrolling="no"></iframe>

    5. ContentProvider

    5.1. Overview

    ContentProvider are used to provide data from an application to another. ContentProvider do not store the data but provide the interface for other applications to access the data.

    The following example will use an existing context provider from "Contacts".

    5.2. Create Contacts

    Start the contacts application and create a few contacts.

    5.3. Create contacts on your emulator

    Use the Android emulator and create a view contacts.

    Press Menu and select "New Contact".

    As a result you should have a few new contacts.

    5.4. Example

    Create a new Android project "de.vogella.android.contentprovider" with the activity "ContactsView".

    Rename the id of the the existing TextView from the example wizard to "contactview". Delete the default text. Also change the layout_height to "fill_parent".

    The resulting main.xml should look like the following.

    				
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:orientation="vertical" android:layout_width="fill_parent"
    	android:layout_height="fill_parent">
    	<TextView android:layout_width="fill_parent"
    		android:layout_height="fill_parent" android:id="@+id/contactview" />
    </LinearLayout>
    
    			

    In AndroidManifest.xml add the User Permission that the application can use "android.permission.READ_CONTACTS".

    Change the coding of the activity.

    				
    package de.vogella.android.contentprovider;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.widget.TextView;
    
    public class ContactsView extends Activity {
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		TextView contactView = (TextView) findViewById(R.id.contactview);
    
    		Cursor cursor = getContacts();
    
    		while (cursor.moveToNext()) {
    
    			String displayName = cursor.getString(cursor
    					.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
    			contactView.append("Name: ");
    			contactView.append(displayName);
    			contactView.append("\n");
    		}
    	}
    
    	private Cursor getContacts() {
    		// Run query
    		Uri uri = ContactsContract.Contacts.CONTENT_URI;
    		String[] projection = new String[] { ContactsContract.Contacts._ID,
    				ContactsContract.Contacts.DISPLAY_NAME };
    		String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
    				+ ("1") + "'";
    		String[] selectionArgs = null;
    		String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
    				+ " COLLATE LOCALIZED ASC";
    
    		return managedQuery(uri, projection, selection, selectionArgs,
    				sortOrder);
    	}
    
    }
    			

    6. Networking

    6.1. Networking

    Android allows to access the network via the the java.net.URL class.

    Tip

    You can also read XML, e.g. RSS feeds. Unfortunately Android does not have a Stax parser included in it SDK. Vote for Android should have a Stax parser to get support. Currently you have to use the android specific class XmlPullParser.

    6.2. Proxy

    To set the proxy you can use the class Settings. For example you could add the following line to your onCreate method in your activity.

    				
    Settings.System.putString(getContentResolver(), Settings.System.HTTP_PROXY, "myproxy:8080");
    			

    Tip

    It seems that DNS resolving doesn't work behind a proxy. See Bug 2764

    6.3. Permissions

    You also have to give your application the right to change the settings "android.permission.WRITE_SETTINGS" in "AndroidManifest.xml".

    6.4. Example

    Create the project "de.vogella.android.network.html". Add the following elements to your activity:

    • EditText with the ID "address"

    • TextView with the ID "pagetext"

    • Button with the ID "ReadWebPage"

    • Create the following code to read a webpage and show the HTML code in the TextView.

      This example also demonstrate the usage of Android preferences to store user data. The URL which the user has typed is stored in the preferences in the method onPause(). This method is called whenever the Activity is send into the background.

      경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
      어린이용이며, 설치가 필요없는 브라우저 게임입니다.
      https://s1004games.com

      				
      package de.vogella.android.network.html;
      
      import java.io.BufferedReader;
      import java.io.InputStreamReader;
      import java.net.URL;
      
      import android.app.Activity;
      import android.content.SharedPreferences;
      import android.content.SharedPreferences.Editor;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.EditText;
      import android.widget.TextView;
      
      public class ReadWebpage extends Activity {
      	private static final String PREFERENCES = "PREFERENCES";
      	private static final String URL = "url";
      	private String lastUrl;
      	private EditText urlText;
      	private TextView textView;
      
      	/** Called when the activity is first created. */
      
      	@Override
      	public void onCreate(Bundle savedInstanceState) {
      		super.onCreate(savedInstanceState);
      		setContentView(R.layout.main);
      
      		urlText = (EditText) findViewById(R.id.address);
      		textView = (TextView) findViewById(R.id.pagetext);
      
      		loadPreferences();
      		urlText.setText(lastUrl);
      	}
      
      	
      	/**
      	 * Demonstrates loading of preferences The last value in the URL string will
      	 * be loaded
      	 */
      	private void loadPreferences() {
      		SharedPreferences preferences = getSharedPreferences(PREFERENCES,
      				Activity.MODE_PRIVATE);
      		// Set this to the Google Homepage
      		lastUrl = preferences.getString(URL, "http://209.85.229.147");
      	}
      
      	@Override
      	protected void onPause() {
      		super.onPause();
      		SharedPreferences preferences = getSharedPreferences(PREFERENCES,
      				Activity.MODE_PRIVATE);
      		Editor preferenceEditor = preferences.edit();
      		preferenceEditor.putString(URL, urlText.getText().toString());
      		// You have to commit otherwise the changes will not be remembered
      		preferenceEditor.commit();
      	}
      	
      	// Will be connected with the buttons via XML
      	public void myClickHandler(View view) {
      		switch (view.getId()) {
      		case R.id.ReadWebPage:
      			try {
      				textView.setText("");
      				// Perform action on click
      				URL url = new URL(urlText.getText().toString());
      				// Get the response
      				BufferedReader rd = new BufferedReader(
      						new InputStreamReader(url.openStream()));
      				String line = "";
      				while ((line = rd.readLine()) != null) {
      					textView.append(line);
      				}
      			}
      
      			catch (Exception e) {
      				System.out.println("Nay, did not work");
      				textView.setText(e.getMessage());
      			}
      			break;
      		}
      	}
      
      }
      
      			

      Assign the handler "buttonHandler" to the button in the property "on Click". via your XML.

      7. Important views

      7.1. Log

      You can see the log (including System.out.print() statements) via the LogCat view.

      7.2. File explorer

      The file explorer allows to see the files on the android simulator.

      8. Shell

      8.1. Opening the Shell

      You can access your Android emulator also via the console. Open a shell, switch to your "android-sdk" installation directory into the folder "tools".

      Start the shell via the command "adb shell".

      8.2. Emulator Console

      The emulator console lets you dynamically access your simulated device. Use "telnet localhost 5554" to conntect to your simulated device. To exit the console session, use the command "quit" or "exit".

      For example you can set your geolocation in the emulator via "geo fix -121.45356 46.51119 4392"

      For more information on the emulator console please see Emulator Console manual

      8.3. Uninstall an application

      You can uninstall an android application via the shell. Switch the the data/app directory (cd /data/app) and simply delete your android application.

      9. Location API

      The location API allow you to determine your current location. The following requires that you have installed the Googles API (see installation) and a valid Google map API key. Go to Obtaining a Maps API Key to get one.

      9.1. Device with Google API

      Create a new device which supports the Google API's. During device creation select the target Google API's in the version of your SDK.

      9.2. Project and Permissions

      Create a new project "de.vogella.android.locationapi". Make sure to select the Google API

      Add the following permissions to your application in Android.xml.

      • INTERNET

      • ACCESS_FINE_LOCATION

      • ACCESS_COARSE_LOCATION

      • Tip

        The maintenance of "Uses permissions" should be enhanced. Please stare at Bug: Permissions should support field assists to get this improved.

        9.3. Google Map library

        You need to add the Google maps library to your application. Open Android.xml, tab Application and add a "Uses library".

        9.4. Layout

        Define your view layout like the following.

        				
        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mainlayout"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        
            <com.google.android.maps.MapView
                android:id="@+id/mapview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:clickable="true"
                android:apiKey="Your Maps API Key"
            />
        
        </RelativeLayout>
        
        			

        Tip

        Replace "Your Maps API Key" with your Google API key.

        9.5. Activity

        Create the following activity. This activity use an LocationListner to update the map with the current location.

        				
        package de.vogella.android.locationapi;
        
        import android.content.Context;
        import android.location.Location;
        import android.location.LocationListener;
        import android.location.LocationManager;
        import android.os.Bundle;
        import android.widget.RelativeLayout;
        import android.widget.ZoomControls;
        
        import com.google.android.maps.GeoPoint;
        import com.google.android.maps.MapActivity;
        import com.google.android.maps.MapController;
        import com.google.android.maps.MapView;
        
        public class CurrentLocation extends MapActivity {
        
        	private MapController mapController;
        	private MapView mapView;
        	private LocationManager locationManager;
        
        	public void onCreate(Bundle bundle) {
        		super.onCreate(bundle);
        		setContentView(R.layout.main); // bind the layout to the activity
        
        		// create a map view
        		RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
        		mapView = (MapView) findViewById(R.id.mapview);
        		ZoomControls mZoom = (ZoomControls) mapView.getZoomControls();
        		linearLayout.addView(mZoom);
        		mapController = mapView.getController();
        		// Zoon 1 is world view
        		
        		mapController.setZoom(14);
        
        		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
        		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
        				0, new GeoUpdateHandler());
        	}
        
        	@Override
        	protected boolean isRouteDisplayed() {
        		return false;
        	}
        
        	public class GeoUpdateHandler implements LocationListener {
        
        		@Override
        		public void onLocationChanged(Location location) {
        			int lat = (int) (location.getLatitude() * 1E6);
        			int lng = (int) (location.getLongitude() * 1E6);
        			GeoPoint point = new GeoPoint(lat, lng);
        			mapController.setCenter(point);
        //			setContentView(mapView);
        		}
        
        		@Override
        		public void onProviderDisabled(String provider) {
        		}
        
        		@Override
        		public void onProviderEnabled(String provider) {
        		}
        
        		@Override
        		public void onStatusChanged(String provider, int status, Bundle extras) {
        		}
        	}
        }
        			

        9.6. Run and Test

        Run and test your application. You should be able to zoon in and out. Use Emulator console to send geo-coordinates to your device for example

        				
        geo fix 13.24 52.31
        			

        Tip

        See also Hello, MapView for an example how to put graphics on the map.

        10. Deploy your application on a real device

        To see how you can deploy your application to a real device please see Developing on a Device . Please note that the Android version you are developing for must be the installed version on your phone.

        To select your phone, select the "Run Configurations", select "Manual" selection and select your device.

        11. Thank you

        Thank you for practicing with this tutorial.

        I maintain this tutorial in my private time. If you like the information please help me by donating or by recommending this tutorial to other people.

        12. Questions and Discussion

        For questions and discussion around this article please use the www.vogella.de Google Group . Also if you note an error in this article please post the error and if possible the correction to the Group.

        Tip

        The following tries to help you in asking good questions: 10 golden rules of asking questions in the OpenSource community .

        13. Links and Literature

        <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script>google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);</script> <iframe style="POSITION: absolute; TOP: 0px; LEFT: 0px" id="google_ads_frame5" height="15" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3851443674951530&output=html&h=15&slotname=9028181258&w=728&lmt=1274474832&flash=10.0.45.2&url=http%3A%2F%2Fwww.vogella.de%2Farticles%2FAndroid%2Farticle.html&dt=1274583031234&shv=r20100505&prev_slotnames=3391820284%2C8639282357%2C3607372071%2C9752573896&correlator=1274583028609&frm=0&ga_vid=567174394.1274583028&ga_sid=1274583028&ga_hid=1776702759&ga_fc=1&u_tz=540&u_his=0&u_java=1&u_h=800&u_w=1280&u_ah=770&u_aw=1280&u_cd=32&u_nplug=0&u_nmime=0&biw=608&bih=378&eid=33895100&ref=http%3A%2F%2Fwww.google.co.kr%2Fsearch%3Fq%3Dandroid%2Bprogramming%26hl%3Dko%26newwindow%3D1%26complete%3D1%26prmd%3Divnlb%26start%3D10%26sa%3DN&fu=0&ifi=5&dtd=62&xpc=rLhtmENpFa&p=http%3A//www.vogella.de" frameBorder="0" width="728" allowTransparency name="google_ads_frame" marginWidth="0" scrolling="no"></iframe>

        13.1. Source Code

        Source Code of Examples

</script>

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED