Tugas Kelompok
Packagedouble net.zenconsult.android.model
Publiclass location {
Private String identifier;
Privatedouble latitude;
Privatedouble longitude;
Public location () {
}
Publicdouble getLattitude() {
}
Publicdouble setLattitude(double latitude){
Return latitude;
}
Publicdouble setlattitude(double latitude){
This.latitude=latitude;
Publicdoble setLattitude (){
Return longitude
}
Publicvoid set identifier(double longitude){
This.longitude=longitude}
}
Publicvoid setidentifier(string identifier) {
Return identifier;
})
Public string to string (){
String Builder ret=newStringbuilder();
Ret.append(getindefier());
Ret.append(string.valueof(getlatitude()));
Ret.append(string.valueof(getlongitude()));
Return ret.toString();
}
Publicbyte[]getbyte(){
Return tostring ().getbyte ()
}
}
Package net.zencosult.android.model;
Publicclass contact {
Private String firstname;
Private String lastname;
Private String address1;
Private String email2;
Private String phone;
Public contact
}
Public string
Return firstName;
Publicvoid setfirstName(stringfirstName) {
This.firstname=firstName;
}
Public string getAddres(string address)1(){
Return addres1;
}
Publicvoid SetAddres(string address){
ReturnAddres2;
}
publicvoidbsetAddress2(string adress2) {
}
Public string getEmail(){
}
Publicvoid setEmail (string email) {
This.email=email;
}
Public string getPhone(){
Return phone;
}
Public string to string(){
stringBuilder ret=new stringBuilder ();
ret.append(getFirstName)+”|”);
ret.append(getAddress1()+”|”);
ret.append(getAddress2()”|”);
ret.append(getEmail()”|”);
ret.append(getPhone ()”|”);
return ret.to string ();
}
Publicbyte[]getbytes(){
Return to string().getbytes()
}
The location and contct classes are standart classes designed to hold data specific to each thpe.eachof then contain tostring()and getbytes()methods that return the entire contents of the class either a string or an array of bytes
If we wre to manually add a contact object,then we would most likely use code similar to what is shown in listing 2-4
Listing2-4.code that adds a new contact object
Final contact = new contact ();
Contact.setFirstName(“sheran”);
Contact.setLastName(“gunasakera”);
Contact.setAddress1(“ “);
Contact.setAddress2(“ “);
Contact.setEmail (sheran@zencounsult.net);
Contact.setPhone(12120031337”);
Note it is always good practice to use the getExternalfile Dir () method to find the location of of the SD Card on an Android device.because Android can run on a large number of device with different specifications,the location of the SD Card directory may not always be in/sdcard.the getexternalDir () method will query the operating system for the correct location of the SD Card and return the location to you
let's take it a line at a time begining with the contructor for the saveContact () method; public static void save contact (context context, contact contact )
if ( isReadWrite()){
try {
the preceding snippet expect a contect object and a contact object.each application on Android has its own context. A context object holds application-specific c;asses,methods,and resoursces thet can be shared among all the classes withnin an application. for example,a context object will contain information about the location of the SD Card directory.to access it,you have to invoke the context.getExternalFilesDir() metho.after the method accepts the parameters,it will check to see if the SD Card on the device in mounted and if it is writeable.the isReadWrite () method will execute and return a true or false valur to indatece this:
file outputfile =new file (context.getExternalDir( null),contact.getfirstName ())
this code creates a file object that points to the location of the SD Card directory.we use the first name of the contact object as the file name:
file outputStream outputStream= new file outputStream(output file);
outputstream.write (contact.getbytes ());
outputStream.close ();
using this code,we create a file outputStream that points to the location of our file object.next,we write the contents object to the output stream using the getBytes ()
method to return an array of bytes.finally we close the fileoutputStream.
when this image is mounted by navigating to android/data/net.zenconsult.android/files,the newly created contact file name is visible (see figure 2-3
if we open the file up in a text editor,we can see the plain text data that was saved from the application (see figure 2-4)
Vocabularies
Similiar : mirip
Visible : terlihat
Resources : sumber daya
Parameters : parameter
Snippet : memotong
Method : metode
Mounted : menjulang
Stream : aliran
Navigate : navigasi
Simulator : simulasi
|
Pada kelas
lokasi dan kon tak merupakan kelas standar yang dibuat untuk memegang spesifik
data pada setiap tipe.tiap dari mereka yang mengandung metode string () dan
getbytes () yang mengembalikan seluruh konten dari kelas tersebut menjadi salah
satu dari string atau susunan dari bytes
Jika kita
menambahkan secara manual objek kontak baru berasumsi untuk saat kode data yang
sejenis dengan apa yang ditampilkan pada data 2-4
Data 2-4 kode
yang menambahka objek kontak baru berasumsi untuk saat kode pada data 2-4
dikatakan saat pengguna mengisi dalam layar untuk menambahkan objek kontak baru
ke aplikasi.daripada terlihat nilai hardcode,anda akan menggunakan cara get
tect () dari setiap objek edit text yang ditampilkan pada layar utama.
Jika anda
mengeksekusi kode save controller,save contact (get application context
().contact) pada simulasi android anda,save controller akan mengambil kreasi
terbaru pada kontak dan took didalamnya pada sumber media external kembali
kedata 2-1
Catatan : ini
selalu menjadi latihan yang baik untuk mendapatkan eksternal files Dir metode
untuk mencari lokasi SD Card diperangkat android.karena android bisa
menjalankan banyak perangkat dengan spesifikasi yang berbeda,lokasi dari
petunjuk SD Card tidak selalu didalam card.metode untuk mendapatkan external
files Dir akan mempertanyakan sistem operasi untuk lokasi yang tepat an SD Card
dan mengembalikan lokasi kepada anda.
Mari ambil waktu sjenak\,dimulai dengan
membina untuk metode save contact : tulis ulang
Potongan sebelumnya mengira objek konteks dan kontak objek setiap applikasi di
android memiliki konteksnya sendiri.konteks objjek mengandung kelas applikasi
spesifik,metode,dan sumber yang bias akan berisi informasi tentang lokasi dari
petunjuk SD Card. untuk mengaksesnya,anda harus menggunakan konteks getExternal
file Dir.setelah metode diterima parameter,itu dicek untuk melihat jika SD Card
diperangkat menjulang dan jika itu biasa ditulis,metode isreadwrite akan
menjalankan dan diberi tahu nilai benar atau salah untuk mengetahui :
file outputfile........
Kode ini membuat berkas objek yang
menunjukkan lokasi dari sd card.kita menggunakan nama pertama dari kontak
objek sebagai nama data.
file outputfile.....
Menggunakan kode ini kita membuat file
output stream yang menunjukkan lokasi data objek kita.selanjutnya,kita menulis
konten dan kontak ke aliran keluar yang menngunakan metode the getbytes untuk
mengembalika susunan bytes.akhirnya kita menutup file output system.ketika
eksekusi selesai,kita harus punya data dengan nama "sheran" tertulis
dipetunjuk SD Card diperangkat ,ssaya menguat simulasi android di
mac OS X Snow leorpard.walaupun ketika saya menavigasikan lokasi dari
simulator,saya bisa melihat layar menunjukkan gambar 2-2
Ketika foto ini menjulang oleh navigasi ke android/data/net.zen-consult.android/files,kreasi terbaru menata kontak tidak terlihat
Jika kita buka data pada pengedit text kita dapat melihat teks yang telah disimpan dari aplikasi
Ketika foto ini menjulang oleh navigasi ke android/data/net.zen-consult.android/files,kreasi terbaru menata kontak tidak terlihat
Jika kita buka data pada pengedit text kita dapat melihat teks yang telah disimpan dari aplikasi
No comments:
Post a Comment