Wednesday, November 21, 2012

Mengecek External SDCard atau "External SDCard"

Cara ini berfungsi pada device - device samsung


    public static ArrayList<String> getExternalSDCard()
    {
    String s = "";
    ArrayList<String> result = new ArrayList<String>();
   
    try
    {
   
    Process process = new ProcessBuilder().command("mount").redirectErrorStream(true).start();
    process.waitFor();

    InputStream is = process.getInputStream();
    byte[] buffer = new byte[1024];
    while (is.read(buffer) != -1)
    {
       s = s + new String(buffer);
    }
    is.close();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }

    String[] lines = s.split("\n");    
    for(int i=0; i<lines.length; i++)
    {
    //path[0]
    if(-1 != lines[i].indexOf("/mnt") && -1 != lines[i].indexOf("vfat") )
    {
       String[] blocks = lines[i].split("\\s");
       Log.i(" SDCARD ","ketemu fvat :" + lines[i]);
       for(int j=0; j<blocks.length; j++)
       {
           if(-1 != blocks[j].indexOf("/") && -1==blocks[j].indexOf("/dev"))
           {
               try{
                File f = new File(blocks[j]);
                if(f.exists()){
                Log.i(" SDCARD","DITAMBAHKAN " + blocks[j]);
                result.add(blocks[j]);
                }
               }catch(Exception e){
               
               }
           }
       }
    }
    else
    {
    Log.d("SDCARD","Not an SDCard / External Storage" + lines[i]);
    }
    }

   
    //try to find fuse file system in /mnt/sdcard
    for(int i=0; i<lines.length; i++)
    {
    //path[0]
    if(-1 != lines[i].indexOf("/mnt/sdcard") && -1 != lines[i].indexOf("fuse rw") )
    {
       String[] blocks = lines[i].split("\\s");
       Log.i(" SDCARD ","found fuse rw :" + lines[i]);
       for(int j=0; j<blocks.length; j++)
       {
           if(-1 != blocks[j].indexOf("/") && -1==blocks[j].indexOf("/dev"))
           {
               try{
                File f = new File(blocks[j]);
                if(f.exists()){
                Log.i(" SDCARD ","DITAMBAHKAN " + blocks[j]);
                result.add(blocks[j]);
                }
               }catch(Exception e){
               
               }
           }
       }
    }
    else
    {
    Log.d(" SDCARD ","Not an SDCard / External Storage" + lines[i]);
    }
    }
   
   
    return result;
    }

Mengecek Apakah Sebuah Method ada dalam Class



Berikut ini adalah contoh kode untuk mengecek apakah sebuah method ada dalam class.  Dalam hal ini, yang dicari adalah setRotation yang belum disupport oleh widget ImageView di android 2.2 dan sudah disupport oleh android 3.0

for(Method m:  ImageView.getClass().getMethods() )
{
String mymethod = "setRotation";
if(mymethod.equalsIgnoreCase(m.getName()))
{
holder.imgListItem.setRotation( (float) item.rotation);
}
}


Tuesday, October 2, 2012

Mengecek Apakah Network Available


public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />