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());
    }

 

Baca Juga:   AlarmManager android studio berjalan di background

Kemungkinan posting ini akan terus diupdate seiring perkembangan perkodingan.

Terimakasih semoga berguna.

Bagikan

You May Also Like

About the Author: rasupe

Leave a Reply

Your email address will not be published. Required fields are marked *