Pages

July 25, 2012

Hello to Tumblr; 'The Thinking Logs'

Hello !

I have decided to switch main blogging to Tumblr platform.
Hence i moved onto tumblr blog 'The Thinking Logshttp://patel.piyush.co

Note: Some of the technical articles related to android, will be re-blogged here too!!

Why I love tumblr:

  • It lets me post Quotes! i love quotes, images, and text.
  • Beautiful themes
  • Awesome social media integration
  • Ubercool mobile clients, for iOS and Android, awesome mobile apps!
Say hello to, http://patel.piyush.co


June 1, 2012

Example : Android Network connectivity check

    /**
     * Checks Internet connectivity
     * @return
     */
    public boolean isNetworkAvailable() {
           Context context = getApplicationContext();
           ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
           if (connectivity == null) {
              Log.e(tag,"network not available");
           } else {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null) {
                 for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                       return true;
                    }
                 }
              }
           }
           return false;
        }
Permission:

Example : Android Menu/Selected snippet.

    final int MENU_ADD=0;
    final int MENU_DELETE=1;
    final int MENU_VIEW=2;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        
        menu.add(0, MENU_ADD, 0, "Add").setIcon(R.drawable.ic_launcher);
        menu.add(0, MENU_DELETE, 0, "Delete");
        menu.add(0, MENU_VIEW, 0, "View");
        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        
        switch (item.getItemId()) {
        case MENU_ADD:
            Log.v("menu","Add clicked");
            return true;
            
        case MENU_DELETE:
            Log.v("menu","View clicked");
            return true;
            
        case MENU_VIEW:
            Log.v("menu","View clicked");
            return true;
        default:
            break;
        }
        
        return true;
    }

May 31, 2012

Example: Shared Preference Getter/Setter


import android.content.Context;
import android.content.SharedPreferences;

public class Util {

 private static String tag="util";
 private static String KP_PREF="kp_pref";
 private SharedPreferences mSharedPref;
 
 public static int getIntValue(Context ctx,String key,int defval)
 {
  SharedPreferences pref = ctx.getSharedPreferences(KP_PREF,Context.MODE_PRIVATE);
  int usageCount = pref.getInt(key, defval);
  return usageCount;
 }
 
 public static void putIntValue(Context ctx,String key,int defval)
 {
  SharedPreferences pref = ctx.getSharedPreferences(KP_PREF,Context.MODE_PRIVATE);
  pref.edit().putInt(key,defval).commit();  
 }
 
}