Getting android application version programatically

Here we go:

private String getVersionName(Context context,Class class_name)
    {
        try
        {
            PackageInfo pinfo;
            ComponentName comp = new ComponentName(context,class_name);
            pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
            return pinfo.versionName;
        }
        catch (Exception e)
        {
            return null;
        }
    }


Happy coding:-)



To find IMEI no of your Android Device, programatically




To test the  IMEI no of your Android device:

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        String deviceID =telephonyManager.getDeviceId();
Add below permission in the manifest file.

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

Finding IP address of your Android device programatically

 You can find more about finding IP address of Android device programatically



public String getLocalIpAddress()
  {
          try {
              for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                  NetworkInterface intf = en.nextElement();
                  for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                      InetAddress inetAddress = enumIpAddr.nextElement();
                      if (!inetAddress.isLoopbackAddress()) {
                          return inetAddress.getHostAddress().toString();
                      }
                  }
              }
          } catch (Exception ex) {
              Log.e("IP Address", ex.toString());
          }
          return null;
      }

Add below permission in the manifest file.
   <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Happy coding :-)

Whether a point lies inside a polygon- 2D

In this post, you can just find the java version of Determining if a point lies on the interior of a polygon .Specifically for 2D.



private class Polygon
      {
            private Point [] vertices;
           
            private boolean isInsidePolygon(Point [] vert,Point p)
            {
                  /*
* Make sure you have initialized vertices[] with valid *polygon
*/
                  int counter = 0;
                  int i;
                  double xid;
                  Point p1,p2;

                  p1 = vertices[0];
                    for (i=1;i<=vertices.length;i++)
                    {
                      p2 = vertices[i % vertices.length];
                      if (p.y > getMinimum(p1.y,p2.y))
                      {
                        if (p.y <= getMaximum(p1.y,p2.y))
                        {
                          if (p.x <= getMaximum(p1.x,p2.x))
                          {
                            if (p1.y != p2.y)
                            {
                  xid = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
                              if (p1.x == p2.x || p.x <= xid)
                              {  
                                    counter++;                                                              }
                            }
                          }
                        }
                      }
                      p1 = p2;
                    }

                    if (counter % 2 == 0)
                    {
                          //IF even, => outside
                      return false;
                    }
                    else
                    {
                          //IF odd, => inside
                      return true;
                    }
            }

           
           
      private int getMinimum(int i,int j)
      {
            return((i<j?i:j));
      }
         
      private int getMaximum(int i,int j)
      {
            return((i>j?i:j));
      }
                        
}



Instantiate 'Polygon'  class and initiate 'vertices' variable with valid instance of 'Point' class. 

You know how to do the rest..

Happy coding :-)

Playing audio sound using SoundPool in Android : best suit for click effects

If you want to listen sound for a button click corresponding to as many times the user
clicks, specifically when the user clicks/taps continuosly with very short span, then follow below steps :-)

Implement SoundPool and AudioManager classes instead of traditional MediaPlayer. Sample is below:






import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class MusicPlayer {

            public static SoundPool soundPoolObj;

            private static int soundPoolObjectID;

            public static AudioManager audioManager;

            private static boolean resourceReleased;

                  /*
                  * I know you are good enough to catch this, still....,
                  *This is the constructor which would be called
*preferably during starting of your app, I mean, before you start any associated music.
                  *
*Note that, You have to pass context and resource id for music to be played.
                  *
* You may use streaming as well, Since I have not tried any, I am not posting about it:-)
                  */

                  public MusicPlayer(Context context,int musicResourceID)
                  {
                        resourceReleased=false;

soundPoolObj=new SoundPool(1/*maximum number of simultaneous streams */,
AudioManager.STREAM_MUSIC,0);                              
audioManager=(AudioManager)context.getSystemService( Context.AUDIO_SERVICE);            

soundPoolObjectID=soundPoolObj.load(context, musicResourceID, 1/*priority*/);

                  }

                  /*
                   * Invoke this method to play the corresponding music..
* make sure you have instantiated this class with proper parameters
                   *
                   */
                  public static void playMusic()
                  {
                        if((!resourceReleased))
                        {
/*Make sure sound duration is small for click effect..*/
int streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                              soundPoolObj.play(soundPoolObjectID/*soundID*/,
streamVolume/*leftVolume*/, streamVolume/*rightVolume*/,
1, 0/*loop: '0' for no looping and '-1' for looping forever*/, 1f);
                             
                              /*
                              *if you wanna pause a long background music,
                              *try: soundPoolObj.pause(soundPoolObjectID);
* and soundPoolObj.resume(soundPoolObjectID); for resuming
                              */

                        }    
                       
                  }


                  /*
                  *Called when you are quitting/ no longer need   music
                  */
                  public static void stopMusic()
                  {          
                        //Finally..
                        soundPoolObj.stop(soundPoolObjectID);
                        //Release  the associated resource
                        MusicPlayer.soundPoolObj.release();

                        resourceReleased=true;
                  }

}




This is an utility class and needs support from a class which implements Activity.

Happy coding :-)