Search This Blog

Thursday, May 23, 2013

Validate the String for the Email


Here is the method for checking the String is email address or not.


private boolean checkemail(String emailstr) {
// TODO Auto-generated method stub
boolean isValid = false;
   String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
   CharSequence inputStr = emailstr;
   Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
   Matcher matcher = pattern.matcher(inputStr);
   if (matcher.matches()) {
       isValid = true;
   }
   return isValid;

}

For validate the String use following:

if(checkemail(emailstr)){
  // Here do with the Email string
}else{
  System.out.println("email String is not valid");
}

Thursday, May 9, 2013

Get the number of times app is used




 private SharedPreferences mPref;
public static final String PREFS_NAME = "YourPrefName";
int c;

in onCreate() write the following



  mPref = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
c = mPref.getInt("numRun", 0);
Log.i("TAG", "Number of count of open App:" + c);
c++;
mPref.edit().putInt("numRun", c).commit();
Log.i("TAG", "Number of count after commit of open App:" + c);