Android – Location Manger

The LocationManager Service is offered by the android.location package. There are three types of location providers:
1. GPS
2. Network – Use cell-phone towers or Wi-Fi networks
3. The Passive provider and it is like a location update sniffer

Here is the example:

public class MyApplication extends Application implements LocationListener {

private static MyApplication instance;

public void onCreate() {
  super.onCreate();
  instance = this;
  LocationManager locMgr = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this);

}

public static Context getContext() {
  return instance;
}

public void onLocationChanged(Location location) {
   Log.i("Debug"," OnLocation Changed"+ location.getLatitude() + " " + location.getLongitude());

}

public void onProviderDisabled(String provider) {

}

public void onProviderEnabled(String provider) {

}

public void onStatusChanged(String provider, int status, Bundle extras) {

}

Singleton Design Pattern in Java

Singleton in Java

Singleton is the design pattern that instantiate the class to one object.

Here is the example in java

public class JavaProject {

	public static void main(String args[]){

		System.out.println("IN main class");

		MYClass a = MYClass.mySingleFactory();
		MYClass b = MYClass.mySingleFactory();

		MYClass c = new MYClass(); //Avoid doing this if the class is to be used as Singleton, because this operation returns a new instance of MyClass

		a.setI(1);
		b.setI(2);
		c.setI(3);

		a.printI();
		b.printI();
		c.printI();
		a.printI();
		MYClass.mySingleFactory().printI(); //another way to call the singleton obj
	}
}

public class MYClass {

	private int i;
	static MYClass inst;

	public static MYClass mySingleFactory(){
		if (inst == null){
			inst = new MYClass();
		}
		return inst;
	}

	public void SingletonTest() {

		System.out.println("SingletonTest constructor called");

	}

	public void printI(){
		System.out.println("Value of I is: " + i);
	}

	public void setI(int i_){
		i = i_;
	}
}

Output:
IN main class
Value of I is: 2
Value of I is: 2
Value of I is: 2
Value of I is: 3
Value of I is: 2