Está en la página 1de 15

Dng Hong Anh D09CN5 Bi tp mn : K thut ha Nghin cu h iu hnh Android

1. Cc thnh phn v vng i ca ng dng Android Activity: hiu mt cch n gin th Activity l nn ca 1 ng dng. Khi khi ng 1 ng dng Android no th bao gi cng c 1 main Activity c gi,hin th mn hnh giao din ca ng dng cho php ngi dng tng tc. Service: thnh phn chy n trong Android. Service s dng update d liu,a ra cc cnh bo (Notification) v khng bao gi hin th cho ngi dng thy. Content Provider: kho d liu chia s. Content Provider c s dng qun l v chia s d liu gia cc ng dng. Intent : nn tng truyn ti cc thng bo. Intent c s dng gi cc thng bo i nhm khi to 1 Activity hay Service thc hin cng vic bn mong mun. VD: khi m 1 trang web, bn gi 1 intent i to 1 activity mi hin th trang web . Broadcast Receiver : thnh phn thu nhn cc Intent bn ngoi gi ti. VD: bn vit 1 chng trnh thay th cho phn gi in mc nh ca Android, khi bn cn 1 BR nhn bit cc Intent l cc cuc gi ti. Notification: a ra cc cnh bo m khng lm cho cc Activity phi ngng hot ng. Actitvity l thnh phn quan trng nht v ng vai tr chnh trong xy dng ng dng Android. H iu hnh Android qun l Activity theo dng stack: khi mt Activity mi c khi to, n s c xp ln u ca stack v tr thnh running activity, cc Activity trc s b tm dng v ch hot ng tr li khi Activity mi c gii phng.

Activity bao gm 4 state: - active (running): Activity ang hin th trn mn hnh (foreground). - paused: Activity vn hin th (visible) nhng khng th tng tc (lost focus). VD: mt activity mi xut hin hin th giao din ln trn activity c, nhng giao din ny nh hn giao din ca activity c, do ta vn thy c 1 phn giao din ca activity c nhng li khng th tng tc vi n. - stop: Activity b thay th hon ton bi Activity mi s tin n trng thi stop - killed: Khi h thng b thiu b nh, n s gii phng cc tin trnh theo nguyn tc u tin. Cc Activity trng thi stop hoc paused cng c th b gii phng v khi n c hin th li th cc Activity ny phi khi ng li hon ton v phc hi li trng thi trc .

Vng i ca Activity: - Entire lifetime: T phng thc onCreate( ) cho ti onDestroy( ) - Visible liftetime: T phng thc onStart( ) cho ti onStop( ) - Foreground lifetime: T phng thc onResume( ) cho ti onPause( ) Khi xy dng Actitvity cho ng dng cn phi vit li phng thc onCreate( ) thc hin qu trnh khi to. Cc phng thc khc c cn vit li hay khng ty vo yu cu lp trnh. 2. Tm hiu Android thng qua nhng v d n gin S dng phn mm Eclipse c tch hp Android SDK v Google API vit ng dng trn Adroid My o Android

2.1 V d v Hello World - To mt Project mi : Project name: HelloWorld (tn project hin tr trn eclipse cng s l th mc ch ng dng trong workspace) Application name: Hello World (tn ng dng s hin th trn in thoi) Package name: org.multiuni.android.hellowrold (tn ca package s cha source code, tng t trong java) Create activity: HelloWorldActivity (to mt lp con ca lp Activity, dng hin th mt mn hnh y l mn hnh HelloWorld ca chng ta) Min SDK version: 4 (tc SDK 1.6, y chng ta khai bo l ng dng ny c th p ng c phin bn SDK c nht l phin bn no) - Cu trc mt Project :

Th mc src cha source code ng dng. Gm cc package v cc class. Th mc gen cha cc file t ng pht sinh (m thng gp nht l R.class) Th mc res cha cc resource dng trong ng dng (thng qua ID).drawable: th mc cha cc hnh nh lm icon hoc ti nguyn cho giao din... layout: cha cc file xml thit k giao din.values: cha cc gi tr s dng trong ng dng c bn nh ngha, nh cc dng k t (string), cc mu (color), cc themes... Th mc assets cha cc resource file m ng dng cn dng (di dng file) File Manifest l file khai bo thng tin v ng vi h thng (nh ng dng gm nhng mn hnh no, c service no xin cc quyn g, phin bn bao nhiu, dng t SDK phin bn no) Ngoi ra cn c file th vin. - Vit code trong file HelloAndroidActivity.java : public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); } }

Chy chng trnh :

2.2 V d v xy dng GoogleMap vi API - To Project : HelloGoogleMaps - M file AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dac" android:versionCode="1" android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> Truy cp internet ly bn <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="com.google.android.maps" /> Khai bo th vin Maps <activity android:name=".HelloGoogleMapsActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

File main.xml to giao din chng trnh :


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

<com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="0AbJcoE-unDG2ZlerV4RWh_ed6xfqK3WdtCwWLQ" (key map ca tng my) /> </LinearLayout>

File HelloGoogleMapActivity :
package com.dac; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import android.os.Bundle;

public class HelloGoogleMapsActivity extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true);// To thanh zoomIn, zoomOut } @Override protected boolean isLocationDisplayed() { // TODO Auto-generated method stub return false; }

protected boolean isRouteDisplayed() { return false; } }

Kt qu chng trnh :

M rng chng trnh, nh du mt s im trn bn : Xy dng thm lp HelloItemizedOverlay :


public class HelloItemizedOverlay extends ItemizedOverlay { private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); public ArrayList<OverlayItem> getmOverlays() { return mOverlays; } public void setmOverlays(ArrayList<OverlayItem> mOverlays) { this.mOverlays = mOverlays; } Context mContext;

public HelloItemizedOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } public HelloItemizedOverlay(Drawable defaultMarker, Context context) { super(boundCenterBottom(defaultMarker)); mContext = context; } @Override protected OverlayItem createItem(int i) { return mOverlays.get(i); } @Override public int size() { return mOverlays.size(); } public void addItem(GeoPoint p, String title, String snippet){ OverlayItem newItem = new OverlayItem(p, title, snippet); mOverlays.add(newItem); populate(); } @Override protected boolean onTap(int index) { OverlayItem item = mOverlays.get(index); AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.show(); return true; } }

Trong HelloGoogleMapsActivity :
public class HelloGoogleMapsActivity extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true); List<Overlay> mapOverlays = mapView.getOverlays();

Drawable drawable = this.getResources().getDrawable(android.R.drawable.star_big_on); HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this); GeoPoint point = new GeoPoint(35410000, 139460000); OverlayItem overlayitem = new OverlayItem(point, "Sekai, konichiwa!", "I'm in Japan!"); itemizedoverlay.addOverlay(overlayitem); mapOverlays.add(itemizedoverlay); @Override protected boolean isLocationDisplayed() { // TODO Auto-generated method stub return false; } protected boolean isRouteDisplayed() { return false; } }

Kt qu :

Xem bn di dng v tinh v giao thng : File giao din :


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btnZoomIn" android:layout_width="161dp" android:layout_height="wrap_content" android:text="+" /> <Button android:id="@+id/btnZoomOut" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="-" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btnSat" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.20" android:text="Ve Tinh" /> <Button android:id="@+id/btnNormal" android:layout_width="152dp" android:layout_height="wrap_content" android:layout_weight="0.02" android:text="Giao Thong" /> </LinearLayout> <com.google.android.maps.MapView android:id="@+id/MapView" android:layout_width="fill_parent" android:layout_height="match_parent"

android:layout_weight="2.11" android:apiKey="0AbJcoE-unDG2ZlerV4RWh_ed6xfqK3WdtCwWLQ" android:clickable="true" > </com.google.android.maps.MapView> </LinearLayout>

File java : package com.dac;

import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;

import com.google.android.maps.*;

public class TestMap2Activity extends MapActivity { OnClickListener o = new OnClickListener() { // Bt s kin cho cc nt @SuppressWarnings("deprecation") public void onClick(View v) { // TODO Auto-generated method stub MapView mv = (MapView)findViewById(R.id.MapView); switch (v.getId()) { case R.id.btnZoomIn: mv.getController().zoomIn();

break; case R.id.btnZoomOut: mv.getController().zoomOut(); break; case R.id.btnSat: mv.setSatellite(true); break;

case R.id.btnNormal: mv.setSatellite(false); break; } mv.postInvalidateDelayed(2000); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnZoomIn = (Button)findViewById(R.id.btnZoomIn); Button btnZoomOut = (Button)findViewById(R.id.btnZoomOut); Button btnSat = (Button)findViewById(R.id.btnSat);

Button btnNormal = (Button)findViewById(R.id.btnNormal); btnNormal.setOnClickListener(o); btnSat.setOnClickListener(o); btnZoomIn.setOnClickListener(o); btnZoomOut.setOnClickListener(o); }

@Override protected boolean isLocationDisplayed() { // TODO Auto-generated method stub return false; } protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } } Kt qu :

También podría gustarte