Archive | others RSS for this section

How to know/find the version of jdk/java which is installed in linux/ubuntu system

Open Terminal and type

java -version

or

javac -version

Advertisements

how to know find the path of java/jdk in ubuntu/linux

 /usr/lib/jvm  is the path where the jdk is located.

How to calculate distance between any two geo points (latitudes and longitudes) in android


/**
 * @param fromLatitude
 * @param fromLongitude
 * @param toLatitude
 * @param toLongitude
 * @return distance between the two latitudes and longitudes in kms.
 */
 public static double calculateDistance(double fromLatitude,double fromLongitude,double toLatitude,double toLongitude)
 {

 float results[] = new float[1];

try {
 Location.distanceBetween(fromLatitude,fromLongitude, toLatitude, toLongitude, results);
 } catch (Exception e) {
 if (e != null)
 e.printStackTrace();
 }

 int dist = (int) results[0];
 if(dist<=0)
 return 0D;

 DecimalFormat decimalFormat = new DecimalFormat("#.##");
 results[0]/=1000D;
 String distance = decimalFormat.format(results[0]);
 double d = Double.parseDouble(distance);
 return d;
 }

How to generate android map api key in windows and ubuntu via command line/tool

In UBUNTU
Step 1 : Open Command prompt

Step 2 : Type the following command keytool -list -keystore < path of .keystore file >

For path of the .keystore in Eclipse IDE Go to Window --> Preferences --> Android --> Build

Here you will find Default debug keystore : field which contains the path of the .keystore file

Eg: C:\Program Files\Java\jre6\bin > keytool -list -keystore C:\Users\home\.android\debug.keystore Press Enter

Step 3 : Now it will ask for password and the prompt message will be as follows

Enter keystore password :

If you are using the Default debug keystore then the password you have to enter is android.If you are using your own custom debug keystore, enter the password which you have entered at the time of creating it.

Step 4 : After entering the password, Press Enter your output should look like this

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

androiddebugkey, 9 Feb, 2012, PrivateKeyEntry,
Certificate fingerprint (MD5): 5E:DA:50:A7:0C:D8:1E:78:F0:6D:39:35:5A:CD:62:AB

Step 5 : Copy the Certificate fingerprint (MD5) and Generate Map Api Key 
Step 6 : Now you got your map api key

Eg: map api key --> 0pmXvUobtthEaNCGH1DNKzWIOFl3UIcDEQaXDkA

Step 7 : Use this in your MapView to show Google Map inside your application

Note : While generating map api key always make sure that you are using the same .keystore file which you are using to export the application or generate the .apk file. If you using differnet .kesytore files for generating map api key and generating .apk file then Google Map will not be displayed in your application.

 

In WINDOWS

Step 1 : Open Command prompt and Go to the path where jre is installed

Eg: cd C:\Program Files\Java\jre6\bin Press Enter

Step 2 : Type the following command keytool -list -keystore < path of .keystore file >

For path of the .keystore in Eclipse IDE Go to Window --> Preferences --> Android --> Build

Here you will find Default debug keystore : field which contains the path of the .keystore file

Eg: C:\Program Files\Java\jre6\bin > keytool -list -keystore C:\Users\home\.android\debug.keystore

Step 3 : Now it will ask for password and the prompt message will be as follows

Enter keystore password :

If you are using the Default debug keystore then the password you have to enter is android.If you are using your own custom debug keystore, enter the password which you have entered at the time of creating it.

Step 4 : After entering the password, Press Enter your output should look like this

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

androiddebugkey, 9 Feb, 2012, PrivateKeyEntry,
Certificate fingerprint (MD5): 5E:DA:50:A7:0C:D8:1E:78:F0:6D:39:35:5A:CD:62:AB
Step 5 : Copy the Certificate fingerprint (MD5) and Generate Map Api Key
Step 6 : Now you got your map api key

Eg: map api key --> 0pmXvUobtthEaNCGH1DNKzWIOFl3UIcDEQaXDkA

Step 7 : Use this in your MapView to show Google Map inside your application

 


Behavior of your application when using map api key 

 Scenario 1 (Map Api Key is unique for a system)

-->  You have generated map api key in System  A from command line

---> You are using the map api key in a project which is in System B.

---> If you run/install/export the application(which is in System B) in device then

Google Map will be not be displayed in the device 

Reason : MD5 signature will be private and unique to a particular system. So you cannot run/install/export an application from a System B by using a map api key of System A(which is private to System A).

Scenario 2(Map api key depends on the kesytore)

-->  You have generated map api key from command line using android's default debug.keystore

---> You are using the map api key in a project

---> You have exported the project and generated a .apk file using your custom debug keystore instead of android's default debug.keystore

---> If you install the .apk file in any device

Google Map will not be displayed.

Reason : The keystore which you are using while generating map api key should be same as the one which you are using to export your application or generate the .apk file. If you using differnet .kesytore files for generating map api key and generating .apk file then Google Map will not be displayed in that application.

Please be careful and check these scenario's while uploading your application to market/ playstore and also while submitting your application for Testing .

 

How to generate a random number with in a range in android/java

/**
 * @param min
 * @param max
 * @return a random number between range [min,max) <br/>
 * Refer <a href="">How to generate a random number with in a specific range</a>
 */
 public static long generateRandomNumber(long min,long max)
 {
 long randomNumber = 0L;

 if(min==max)
 return min;

 if(min>max)
 return min;

 if((max-min)<=1)
 return min;

 if((max-min)==2)
 return (min+1);

 randomNumber = min + (int)(Math.random() * ((max - min) + 1));
 return randomNumber;
 }

How to get SDK version programmatically in android


/**

* @return <b>SDK Version</b> of the device <br/>
 * For <b>T-Mobile</b> SDK Version is 1.6, <b>Motorolla
 * Milestone</b> SDK Version is 2.1, <b>Samsung Nexus</b> SDK
 * Version is 2.3.6
 */
 public static String getSDKVersion() {
 return Build.VERSION.RELEASE;
 }

How to convert length/distance from one unit to another unit like miles,kilometers,metres,centimeters. etc …


/**
 * @param l length in double which you want to convert
 * @param from Units of length <b>l</b> from which you want to convert
 * @param to Units to which you want to convert your length
 * @return length in double after conversion <br/><br/>
 *
 * @see <code>double d = 0;<br/>
 * d = convertLength(10,Units.KILOMETRE,Units.METRE); <br/>
 * // converts 10 from KILOMETRE to METRE<br/>
 * System.out.println(d); // d=10000<br/><br/>
 * d = convertLength(1,Units.METRE,Units.KILOMETRE); <br/>
 * // converts 1 from METRE to KILOMETRE<br/>
 * System.out.println(d); // d=0.001 <br/></code>
 */
 public static double convertLength(double l,Units from,Units to)
 {
 if( (from==to) || (l<=0) )
 {
 return l;
 }
 else
 {
 double from_length__in_meter = from.length;
 double to_length_in_meter = to.length;

 double metre_len = (1/to_length_in_meter);

 double d = l * from_length__in_meter * metre_len;

 if(d<10e-5||d>10e5)
 {
 DecimalFormat decimalFormat = new DecimalFormat("0.00E00");
 String s = decimalFormat.format(d);
 d = Double.parseDouble(s);
 return d;
 }
 else
 {
 DecimalFormat decimalFormat = new DecimalFormat("0.00");
 String s = decimalFormat.format(d);
 d = Double.parseDouble(s);
 return d;
 }
 }
 }

The Units enum as follows


public enum Units
{
 ANGSTORM(1e-10),
 ASTRONOMICAL_UNIT(1495978714.64e2),
 BARLEYCORN(8.46e-3),
 BOHR(5.3e-11),
 CABLE_LENGTH(185.2),
 CENTIMETRE(0.01),
 CHAIN(20.11684),
 CUBIT(0.5),
 ELL(1.143),
 FATHOM(1.8288),
 FERMI(1e-15),
 FINGER(0.022225),
 FOOT(0.304799735),
 FRENCH(0.0003),
 FURLONG(201.168),
 HAND(0.1016),
 INCH(0.0254),
 KILOMETRE(1000),
 LEAGUE(4828.032),
 LIGHT_YEAR(9.4607304725808e15),
 LIGHT_DAY(2.59020683712e13),
 LIGHT_HOUR(1.0792528488e12),
 LIGHT_MINUTE(1.798754748e10),
 LIGHT_SECOND(299792458),
 LINE(0.002116),
 LINK_GUNTERS_SURVEYORS(0.201168),
 LINK_RAMSDENS_ENGINEERS(0.3048),
 METRE(1),
 MICKEY(1.27e-4),
 MICRON(1e-6),
 MIL(2.54e-5),
 MILE(1609.344),
 MILLIMETRE(1e-3),
 NAIL(0.05715),
 NANOMETER(1e-9),
 NAUTICAL_LEAGUE(5556),
 NAUTICAL_MILE(1853),
 PACE(0.762),
 PALM(0.0762),
 PARSEC(30.857e15),
 POINT(0.000351450),
 QUARTER(0.2286),
 ROD(5.0292),
 ROPE(6.096),
 SPAN(0.2286),
 SPAT(1e12),
 STICK(0.0508),
 PICOMETRE(1e-12),
 X_UNIT(1.002e-13),
 YARD(0.9144);

 Units(double value)
 {
 this.length = value;
 }

 public double length;

}

How to check whether sim card exists or not in android


/**
 * @return true if SIM card exists
 * false if SIM card is locked or doesn't exists <br/><br/>
 * <b>Note:</b> This method requires permissions <b> "android.permission.READ_PHONE_STATE" </b>
 */
 public boolean isSimExists()
 {
 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
 int SIM_STATE = telephonyManager.getSimState();

 if(SIM_STATE == TelephonyManager.SIM_STATE_READY)
 return true;
 else
 {
 switch(SIM_STATE)
 {
 case TelephonyManager.SIM_STATE_ABSENT: //SimState = "No Sim Found!";
 break;
 case TelephonyManager.SIM_STATE_NETWORK_LOCKED: //SimState = "Network Locked!";
 break;
 case TelephonyManager.SIM_STATE_PIN_REQUIRED: //SimState = "PIN Required to access SIM!";
 break;
 case TelephonyManager.SIM_STATE_PUK_REQUIRED: //SimState = "PUK Required to access SIM!"; // Personal Unblocking Code
 break;
 case TelephonyManager.SIM_STATE_UNKNOWN: //SimState = "Unknown SIM State!";
 break;
 }
 return false;
 }
 }

How to set font style to a TextView in android


public static void setFont(String fontName, TextView tv, Activity activity)

{
 if(fontName!=null)
 {
 if(!fontName.equals(""))
 {
 Typeface tf = Typeface.createFromAsset(activity.getAssets(), fontName);
 tv.setTypeface(tf);
 }
 }
 }

How to get list of all Activities inside an Application / Project in android


public String[] getListOfActivitiesInApplication()
 {
 String activityArray[] = new String[0];

 Vector<String> vector = new Vector<String>();
 try
 {
 PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES);
 ActivityInfo activities[] = packageInfo.activities;

 for (int i = 0; i < activities.length; i++)
 {
 ActivityInfo activityInfo = activities[0];
 String className = activityInfo.name;
 if(className!=null)
 {
 vector.add(className);
 }
 }
 }
 catch (Exception e)
 {
 e.printStackTrace();
 vector.clear();
 }

 int size = vector.size();
 if(size>0)
 {
 activityArray = new String[size];
 vector.toArray(activityArray);
 }
 return activityArray;
 }