1 / 27

Mekanisme penyimpan data pada Android

Mekanisme penyimpan data pada Android. Mobile computing 23/24 Mei 2013. Pada contoh berikut ditampilkan metode penyimpana data dengan preference yaitu tentang pengaturan ukuran font. Settingan terakhir menentukan tampilan ukuran font setelh aplikasi dijalankan kembali

tadita
Download Presentation

Mekanisme penyimpan data pada Android

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mekanisme penyimpan data pada Android Mobile computing 23/24 Mei 2013

  2. Pada contoh berikut ditampilkan metode penyimpana data dengan preference yaitu tentang pengaturan ukuran font. Settingan terakhir menentukan tampilan ukuran font setelh aplikasi dijalankan kembali (project=projsharedpreference)

  3. Komponen UI <?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" > <SeekBar android:id="@+id/SeekBar01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnSave" android:text="Save" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

  4. //---menyimpan nilai dalam EditText view ke preferences--- editor.putFloat(FONT_SIZE_KEY, editText.getTextSize()); editor.putString(TEXT_VALUE_KEY, editText.getText().toString()); //---menyimpan nilai-- editor.commit(); //---menampilkan pesan yang disimpan--- Toast.makeText(getBaseContext(), "Ukuran teks berhasil disimpan!", Toast.LENGTH_SHORT).show(); } });

  5. //---load SharedPreferences object--- // SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE); prefs = getPreferences(MODE_PRIVATE); //tidakmemerlukannama preference, nama preference adalah activity nya //---set ukuran huruf TextView ke nilai sebelumnya--- float fontSize = prefs.getFloat(FONT_SIZE_KEY, 12); //---inisiasi SeekBar and EditText--- seekBar.setProgress((int) fontSize); editText.setText(prefs.getString(TEXT_VALUE_KEY, "")); editText.setTextSize(seekBar.getProgress()); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBarseekBar, int progress, boolean fromUser) { //---merubah ukuran teks pada EditText--- editText.setTextSize(progress); } }); } }

  6. Dimana menampilkan bagian setting yang menyimpan perubahan data tersebut? Open perspective  DDMS

  7. <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="textvalue">menyimpan informasi dengan preference </string> <float name="fontsize" value="22.0" /> </map>

  8. Persistance File Metode penyimpanan data dengan File (projPersistanceFile)

  9. Penyimpanan data Menampilkan data

  10. Dimanakah fie disimpan?

  11. public void onClick(View v) { String str = textBox.getText().toString(); try { FileOutputStream fOut = openFileOutput("fileteks.txt", MODE_WORLD_READABLE); OutputStreamWriter osw = new OutputStreamWriter(fOut); //---menuliskan string dalam file --- osw.write(str); osw.flush(); osw.close(); //---menampilkan message penyimpanan file dengan Toast--- Toast.makeText(getBaseContext(), "File telah berhasil disimpan!", Toast.LENGTH_SHORT).show(); //---membersihkan EditText--- textBox.setText(""); }

  12. public void onClick(View v) { try { FileInputStream fIn = openFileInput("fileteks.txt"); InputStreamReader isr = new InputStreamReader(fIn); char[] inputBuffer = new char[READ_BLOCK_SIZE]; String s = ""; int charRead; while ((charRead = isr.read(inputBuffer))>0) • { • //---merubah karakter chars menjadi String--- • String readString = • String.copyValueOf(inputBuffer, 0, • charRead); • s += readString; • inputBuffer = new char[READ_BLOCK_SIZE]; • } • //---set EditText menjadi text agar bisa dibaca --- • textBox.setText(s); • Toast.makeText(getBaseContext(), • "File loaded successfully!", • Toast.LENGTH_SHORT).show(); • } • catch (IOException ioe) { • ioe.printStackTrace(); • } • } • }); • } • }

  13. <?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="wrap_content" android:text="masukkan kalimat yang akan disimpan" /> <EditText android:id="@+id/txtText1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnSave" android:text="Save" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnLoad" android:text="Load" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

  14. SQLite dan Android Metode penyimpanan data dengan database android SQLite  open source database yang di “tanamkan” ke android. SQLite mendukung standar relasional database seperti sintaks SQL, transaksi. Dengan sqlite android tidak memerlukan database lainnya untuk penyimpanan data terstruktur

  15. server Akses jaringan Perangkat Android database Penyimpanan melalui database internet

  16. Tugas kelompok Buatlah kelompok maksimal 4 orang Carilah jurnal atau paper tentang topik - context awareness application - mobile computing - next generation mobile technology 3. Tugas dipresentaskan tanggal 14 Juni 2013

More Related