100
Managing Projects Projects act as containers for storing things such as code and resource files. The SDK tools expect your projects to follow a specific structure so it can compile and package your application correctly, so it is highly recommended that you create them with Eclipse and ADT or with the android tool on the command line. There are three types of projects, and they all share the same general structure but differ in function: Android Projects An Android project is the container for your application's source code, resource files, and files such as the Ant build and Android Manifest file. An application project is the main type of project and the contents are eventually built into an .apk file that you install on a device. Test Projects These projects contain code to test your application projects and are built into applications that run on a device. Library Projects These projects contain shareable Android source code and resources that you can reference in Android projects. This is useful when you have common code that you want to reuse. Library projects cannot be installed onto a device, however, they are pulled into the .apk file at build time. When you use the Android development tools to create a new project, the essential files and folders will be created for you. There are only a handful of files and folders generated for you, and some of them depend on whether you use the Eclipse plugin or the android tool to generate your project. As your application grows in complexity, you might require new kinds of resources, directories, and files. ANDROID PROJECTS Android projects are the projects that eventually get built into an .apk file that you install onto a device. They contain things such as application source code and resource files. Some are generated for you by default, while others should be created if required. The following directories and files comprise an Android project: src/ Contains your stub Activity file, which is stored at src/your/package/namespace/ActivityName.java. All other source code files (such as .java or .aidl files) go here as well. bin/ Output directory of the build. This is where you can find the final .apk file and other compiled resources. Page 1 of 100

Android Training

Embed Size (px)

DESCRIPTION

Excersire Android

Citation preview

Managing ProjectsProjects act as containers for storing things such as code and resource files. The SDK tools expect your projects to follow a specific structure so it can compile and package your application correctly, so it is highly recommended that you create them with clipse and !DT or with the android tool on the command line. There are three types of projects, and they all share the same general structure "ut differ in function#Android Projects!n !ndroid project is the container for your application$s source code, resource files, and files such asthe !nt "uild and !ndroid Manifest file. !n application project is the main type of project and the contents are e%entually "uilt into an .apk file that you install on a de%ice.Test ProjectsThese projects contain code to test your application projects and are "uilt into applications that run ona de%ice.Library ProjectsThese projects contain sharea"le !ndroid source code and resources that you can reference in !ndroid projects. This is useful when you ha%e common code that you want to reuse. &i"rary projects cannot "e installed onto a de%ice, howe%er, they are pulled into the .apk file at "uild time.'hen you use the !ndroid de%elopment tools to create a new project, the essential files and folders will "e created for you. There are only a handful of files and folders generated for you, and some of them depend on whether you use the clipse plugin or the android tool to generate your project. !s your application grows incomplexity, you might re(uire new kinds of resources, directories, and files.!)D*+,D P*+-.TS!ndroid projects are the projects that e%entually get "uilt into an .apk file that you install onto a de%ice. They contain things such as application source code and resource files. Some are generated for you "y default, while others should "e created if re(uired. The following directories and files comprise an !ndroid project#src/.ontains your stu" !cti%ity file, which is stored at src/your/package/namespace/ActivityName.java. !ll other source code files /such as .java or .aidl files0 go here as well.bin/+utput directory of the "uild. This is where you can find the final .apk file and other compiled resources.jni/.ontains nati%e code sources de%eloped using the !ndroid )DK. 1or more information, see the !ndroid )DKdocumentation.gen/.ontains the -a%a files generated "y !DT, such as your R.java file and interfaces created from !,D& files.assets/This is empty. 2ou can use it to store raw asset files. 1iles that you sa%e here are compiled into an .apk file as3is, and the original filename is preser%ed. 2ou can na%igate this directory in the same way as a Page 1 of 91typical file system using 4*,s and read files as a stream of "ytes using the AssetManager. 1or example, this is a good location for textures and game data.res/.ontains application resources, such as drawa"le files, layout files, and string %alues. See !pplication *esources for more information.anim/1or 5M& files that are compiled into animation o"jects. See the !nimation resource type.color/1or 5M& files that descri"e colors. See the .olor 6alues resource type.drawable/1or "itmap files /P)7, -P7, or 7,10, 83Patch image files, and 5M& files that descri"e Drawa"le shapes or Drawa"le o"jects that contain multiple states /normal, pressed, or focused0. See the Drawa"le resource type.layout/5M& files that are compiled into screen layouts /or part of a screen0. See the &ayout resource type.menu/1or 5M& files that define application menus. See the Menus resource type.raw/1or ar"itrary raw asset files. Sa%ing asset files here instead of in the assets/ directory only differs in the waythat you access them. These files are processed "y aapt and must "e referenced from the applicationusing a resource identifier in the R class. 1or example, this is a good place for media, such as MP9 or+gg files.values/1or 5M& files that are compiled into many kinds of resource. 4nlike other resources in the res/ directory, resources written to 5M& files in this folder are not referenced "y the file name. ,nstead, the 5M& element type controls how the resources is defined within them are placed into the R class.xml/1or miscellaneous 5M& files that configure application components. 1or example, an 5M& file that defines aPreferenceScreen, AppWidgetProviderInfo, or Searcha"ility Metadata. See !pplication *esources for more information a"out configuring these application components.libs/.ontains pri%ate li"raries.AndroidManifest.xmlThe control file that descri"es the nature of the application and each of its components. 1or instance, it descri"es# certain (ualities a"out the acti%ities, ser%ices, intent recei%ers, and content pro%iders: whatpermissions are re(uested: what external li"raries are needed: what de%ice features are re(uired, what !P, &e%els are supported or re(uired: and others. See the !ndroidManifest.xml documentation for more informationproject.propertiesThis file contains project settings, such as the "uild target. This file is integral to the project, so maintain it in asource re%ision control system. To edit project properties in clipse, right3click the project folder and selectProperties.local.properties.ustomi;a"le computer3specific properties for the "uild system. ,f you use !nt to "uild the project, this contains the path to the SDK installation. estroy;< call"ack, so it$s important you use on(top;< to release resources that might leak memory.!lthough the on%ause;< method is called "efore on(top;estroy;< method for your Activity. irectory;irectory;< for files that are shared orget3xternal"iles>ir;< for files that are pri%ate to your app, it$s important that you use directory names pro%ided "y !P, constants like >@R39TDRG0%@9TER3(. These directory names ensure that the files are treated properly "y the system. 1or instance, files sa%ed in >@R39TDRG0R@F/TDF3( are categori;ed "y the system media scanner as ringtones instead of music.M4*2 1* SP!.,f you know ahead of time how much data you$re sa%ing, you can find out whether sufficient space is a%aila"le without causing an @D3xception "y calling get"ree(pace;< or getTotal(pace; * +entryid+Bpublic static final (tring 9D1EMF0FAM30T@T13 * +title+Bpublic static final (tring 9D1EMF0FAM30(E8T@T13 * +subtitle+B...??.*!T ! D!T! 5 + @FT3/3R %R@MARG Q3GA+ 5"eed3ntry.9D1EMF0FAM303FTRG0@> 5 T3CT0TG%3 5 9DMMA0(3% 5"eed3ntry.9D1EMF0FAM30T@T13 5 T3CT0TG%3 5 9DMMA0(3% 5... // Any ot$er options for t$e 9R3AT3 command+ 313T303FTR@3( *+>RD% TA813 @" 3C@(T( + 5 "eed3ntry.TA8130FAM3B-ust like files that you sa%e on the de%ice$s internal storage, !ndroid stores your data"ase in pri%ate disk space that$s associated application. 2our data is secure, "ecause "y default this area is not accessi"le to other applications.! useful set of !P,s is a%aila"le in the (T1iteDpen&elper class. 'hen you use this class to o"tain references to your data"ase, the system performs the potentially long3running operations of creating and updating the data"ase only when needed and not during app startup. !ll you need to do is call getKritable>atabase;< orgetReadable>atabase;atabase;< in a "ackground thread, such as with AsyncTask or @ntent(ervice.To use (T1iteDpen&elper, create a su"class that o%errides the on9reate;b&elper extends (T1iteDpen&elper =// @f you c$ange t$e database sc$emaA you must increment t$e database version.public static final int >ATA8A(30)3R(@DF * -Bpublic static final (tring >ATA8A(30FAM3 * +"eedReader.db+Bpublic "eedReader>b&elper;9ontext context< =super;contextA >ATA8A(30FAM3A nullA >ATA8A(30)3R(@DFatabase db< =db.exec(T1;(T109R3AT303FTR@3(atabase dbA int old)ersionA int new)ersion< =// T$is database is only a cac$e for online dataA so its upgrade policy is// to simply to discard t$e data and start overdb.exec(T1;(T10>313T303FTR@3(atabase dbA int old)ersionA int new)ersion< =onEpgrade;dbA old)ersionA new)ersionb&elper;get9ontext;A idatabase;efine a projection t$at specifies w$ic$ columns from t$e database// you will actually use after t$is Nuery.(tringUV projection * ="eed3ntry.0@>A"eed3ntry.9D1EMF0FAM30T@T13A"eed3ntry.9D1EMF0FAM30E%>AT3>A...?BPage $+ of 91// &ow you want t$e results sorted in t$e resulting 9ursor(tring sortDrder *"eed3ntry.9D1EMF0FAM30E%>AT3> 5 + >3(9+B9ursor c * db.Nuery;"eed3ntry.TA8130FAM3A// T$e table to NueryprojectionA // T$e columns to returnselectionA// T$e columns for t$e K&3R3 clauseselectionArgsA// T$e values for t$e K&3R3 clausenullA // donMt group t$e rowsnullA // donMt filter by row groupssortDrder // T$e sort order(tring selection * "eed3ntry.9D1EMF0FAM303FTRG0@> 5 + 1@Q3 6+B(tringUV selectionArgs * = (tring.valueDf;row@d< ?Bint count * db.update;"eedReader>b&elper."eed3ntry.TA8130FAM3AvaluesAselectionAselectionArgs