Categories: AndroidJava

Fungsi berguna di android studio java

Bismillahirrohmaanirrohiim…

Di bawah ini adalah beberapa fungsi yang berguna untuk program java di android studio.

Unik ID berupa string
public String getId() {
    Random r = new Random();
    long unixTime = System.currentTimeMillis();// / 1000L;
    int rmin = 1;
    int rmax = 9999;

    String unikid = Long.toString(unixTime) + Integer.toString(r.nextInt(rmax - rmin) + rmin);

    return unikid;
}
Unik ID berupa integer
public int getIdInt() {
    Random r = new Random();
    int idx = (int)System.currentTimeMillis();
    int rmin = 1;
    int rmax = 9999;

    int unikid = idx + (r.nextInt(rmax - rmin) + rmin);

    return unikid;
}
Mendapatkan info device
public String getDeviceInfoStr() {
    String devinfo = "";
    devinfo += "|SDK: " + String.valueOf(Build.VERSION.SDK_INT);
    devinfo += "|VERSION: " + Build.VERSION.RELEASE;
    devinfo += "|BRAND: " + Build.BRAND;
    devinfo += "|MANUFACTURER: " + Build.MANUFACTURER;
    devinfo += "|MODEL: " + Build.MODEL;
    devinfo += "|ID: " + Build.ID;
    devinfo += "|SERIAL: " + Build.SERIAL;
    /*
    Log.i("TAGG", "SERIAL: " + Build.SERIAL);
    Log.i("TAGG","MODEL: " + Build.MODEL);
    Log.i("TAGG","ID: " + Build.ID);
    Log.i("TAGG","Manufacture: " + Build.MANUFACTURER);
    Log.i("TAGG","PRODUCT: " + Build.PRODUCT);
    Log.i("TAGG","type: " + Build.TYPE);
    Log.i("TAGG","user: " + Build.USER);
    Log.i("TAGG","BASE: " + Build.VERSION_CODES.BASE);
    Log.i("TAGG","INCREMENTAL " + Build.VERSION.INCREMENTAL);
    Log.i("TAGG","SDK  " + Build.VERSION.SDK_INT);
    Log.i("TAGG","BOARD: " + Build.BOARD);
    Log.i("TAGG","BOOTLOADER: " + Build.BOOTLOADER);
    Log.i("TAGG","BRAND " + Build.BRAND);
    Log.i("TAGG","HOST " + Build.HOST);
    Log.i("TAGG","FINGERPRINT: "+Build.FINGERPRINT);
    Log.i("TAGG","Version Code: " + Build.VERSION.RELEASE);
    Log.i("TAGG","DEVICE: " + Build.DEVICE);
    Log.i("TAGG","DISPLAY: " + Build.DISPLAY);
    Log.i("TAGG","HARDWARE: " + Build.HARDWARE);
    Log.i("TAGG","TAGS: " + Build.TAGS);

     */    return devinfo;
}
Mendapatkan tanggal sekarang
public String dateNow(String formatdate) {
    //return "yyyyMMdd"
    SimpleDateFormat sdf = new SimpleDateFormat(formatdate);
    return sdf.format(new Date());
}
public String getCurrentDate() {
    Calendar cal = Calendar.getInstance();

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formattedDate = df.format(cal.getTime());

    return formattedDate;
}
Merubah format tanggal di android
String getdateFmt = '2020-9-3';//tgl 3 sept 2020

SimpleDateFormat fromUser = new SimpleDateFormat("yyyy-M-d");
SimpleDateFormat myFormat = new SimpleDateFormat("dd MMMM yyyy");
try {
    getdateFmt = myFormat.format(fromUser.parse(getdate));
} catch (ParseException e) {
    e.printStackTrace();
}
Sleep di android studio java
Handler handler = new Handler(); 
handler.postDelayed(new Runnable() {
     @Override 
     public void run() { 
          //perintah di sini 
     } 
}, 1000);//1 detik
Menambah textview secara dinamis dengan java
private void addTextView() {
    //llObj = linear layout obj
    int numChild = llObj.getChildCount();

    TextView textView = new TextView(this);
    textView.setText("tambahan baru. Jum Child: "+numChild);
    llObj.addView(textView, 0);
    //0 adalah index, jadi posisi paling atas (prepend). kl dikosongi nanti textview akan ditaruh di paling bawah

    if (numChild>= 10) {
        //hapus bari paling bawah, shg tdk memanjang unlimited
        llResponse.removeViewAt(10);
    }
}
Extract json menjadi teks
String options = '{"data1":"value1","data2":"value2"}';
try {
    JSONObject jo = new JSONObject(options);
    String data1= jo.getString("data1");
} catch (JSONException e) {
    //e.printStackTrace();
    //e.getMessage()
}
Extract json multidimensi untuk mendapatkan iterasi key value
String options = '{"nama":"rudi","kendaraan":{"satu":"mobil","dua":"motor"}}';
try {
    JSONObject jo = new JSONObject(options);
    String data1= jo.getString("nama");
} catch (JSONException e) {
    //e.printStackTrace();
    //e.getMessage()
}

//ada dua cara

//cara pertama
JSONObject pp = null;
HashMap<String, Object> map = new HashMap<>();

try {
    pp = jo.getJSONObject("kendaraan");
    //String value1 = pp.getString("satu");//ini bisa diambil kalau kunci sudah diketahui, hasilnya=>mobil
    Iterator<String> iter = pp.keys();
    while (iter.hasNext()) {
        String key = iter.next();
        map.put(key, pp.get(key));
    }
} catch (JSONException e) {
    e.printStackTrace();
}

//cara kedua

JSONArray pparr = null;
try {
    pparr = jo.getJSONArray("post_params");
    for (int i=0;i<pparr.length();i++) {
        JSONObject jsonObj = pparr.getJSONObject(i);
        String k = jsonObj.keys().next();
        String v = jsonObj.getString(k);
    }
} catch (JSONException e) {
    e.printStackTrace();
}
Mendapatkan key dan value HashMap
Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    for (Map.Entry<String, String> entry : map.entrySet()) {
        System.out.println(entry.getKey() + " = " + entry.getValue());
    }

 

Kemungkinan posting ini akan terus diupdate seiring perkembangan perkodingan.

Terimakasih semoga berguna.

Bagikan
rasupe

Share
Published by
rasupe

Recent Posts

CPU usage tinggi pada server

Bismillaahirrohmaanirrohiim... Untuk mendeteksi cpu usage tinggi memang perkara rumit, adakalanya karena codingan bermasalah, akses ke…

2 months ago

Membersihkan log mongodb yang besar

Bismillaahirrohmaanirrohiim... Jika tidak dirawat dan dicek berkala, log mongodb akan semakin membesar. Bahkan sampai menghabiskan…

2 months ago

Cara mengakses gps di HP dengan javascript

Bismillaahirrohmaanirrohiim... Jika kita membuat website yang digunakan untuk memantau lokasi HP client, maka kita memerlukan…

5 months ago

Menghitung jarak dua titik koordinat dengan php dan javascript

Bismillaahirrohmaanirrohiim... Kali ini kita akan mencoba menerapkan rumus trigonometeri dari formula haversine, yang digunakan untuk…

5 months ago

Kemasan cleo botol kecil sering bikin tangan terluka

Bismillaahirrohmaanirrohiim... Disini saya akan berbagi pengalaman tentang susahnya membuka tutup botol cleo kecil ukuran 220ml.…

5 months ago

Solusi InsCek pada meteran PLN

Bismillaahirrohmaanirrohiim... Kali ini saya akan berbagi pengalaman tentang mati lampu yang disebabkan oleh permasalahan arus…

5 months ago