Posts

Showing posts from March, 2015

Shared Preferences

----- Initialization : ----- SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode Editor editor = pref.edit(); ----- Storing Data : ----- editor.putBoolean("key_name", true); // Storing boolean - true/false editor.putString("key_name", "string value"); // Storing string editor.putInt("key_name", "int value"); // Storing integer editor.putFloat("key_name", "float value"); // Storing float editor.putLong("key_name", "long value"); // Storing long editor.commit(); // commit changes ----- Retrieving Data : ----- // returns stored preference value // If value is not present return second param value - In this case null pref.getString("key_name", null); // getting String pref.getInt("key_name", null); // getting Integer pref.getFloat("key_name", null); // getting Float pref.getLong("key_name", null)

Working with Xmpp in Android

// Create variable for XMPPConnection. private XMPPConnection xmppConnection = null; // Create packet listener to get reply from server. private MyPacketListener myPacketListener = null; // connect to XMPP server private void connectToXmpp() {     new Thread(new Runnable()     {         public void run()         {             // Create a connection             // pass server url , port number and service.             // server url and service are same.             ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(                     "23.239.201.132", 5222,    "23.239.201.132");             xmppConnection = new XMPPConnection(connectionConfiguration);             connectionConfiguration.setSASLAuthenticationEnabled(true);             try             {                 xmppConnection.connect();                 Log.i("Tag", "XMPP " + "CONNECTED TO " + xmppConnection.getHost());                 try              

Linking eclipse to YouWave

- Open cmd - Write command in cmd "adb connect localhost:5558" for linking eclipse to YouWave

Copy Database

- This code is use for copy application database to sdcard. - This solution is use when you are testing android app in mobile. import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileUtility {     // Status reply 1 when file copy successfully     // Status reply 0 when file copy unsuccessfully     private static int status = 0;         // copyFile() pass source and destination as argumenr.     public static int copyFile(File src, File dst) throws IOException     {         try         {             InputStream in = new FileInputStream(src);             OutputStream out = new FileOutputStream(dst);             // Transfer bytes from in to out             byte[] buf = new byte[1024];             int len;             while ((len = in.read(buf)) > 0)             {                 out.write(buf, 0, len);             }             in.close();            

Log Message

- It helps to filter Log Text. - Log Message class for Android. - You can use this class to assign common Tag Name. import android.util.Log; public class LogMessage {     public static void d(String message)     {         Log.d("TAG :", message);     }     public static void d(int message)     {         Log.d("TAG :", message+"");     }     public static void e(String message)     {         Log.e("TAG :", message);     }     public static void e(int message)     {         Log.e("TAG :", message+"");     }     public static void i(String message)     {         Log.e("TAG :", message);     }     public static void i(int message)     {         Log.e("TAG :", message+"");     } }

Data Time class

- Data Time class use with Android and Java. - This class is use for get current date time. import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Locale; /*  * Created By : Punit Solanki  */ public class DateTime {     private static SimpleDateFormat mDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);         /* Gives current mobile TIME */     public static String getTime()     {         Calendar c = Calendar.getInstance();         int hour = c.get(Calendar.HOUR_OF_DAY);         int min = c.get(Calendar.MINUTE);         int sec = c.get(Calendar.SECOND);         String s_time = (String.valueOf(hour).length() == 1 ? "0"                 + String.valueOf(hour) : String.valueOf(hour))                 + ":"                 + (String.valueOf(min).length() == 1 ? "0"                         + String.valueOf(min) : String.valueOf(min))