Tuesday, 5 November 2013

GPS / Network Example

If you are developing any location or map based application, you have to make your app more smarter by finding user’s location automatically. For this you need to integrate GPS or Network module in your application. GPS is more accurate with Network. Here i am going to explain you how we can work on GPS and Network.

Example:



If GPS not activate then it will show you alert to activate your GPS



After GPS activation press back button to come back to your activity, whenever GPS get Longitude & Latitude it prompt a toast with Longitude & Latitude information 



If we press Network button it will show you current network tower location



 Code:

activity_mail.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="130dp"
        android:text="Location by GPS"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button2"
        android:layout_width="145dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="GPS" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:text="Location by Network"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="148dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Network" />

</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.gpsexample;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button network, gps;
LocationManager lm;
LocationListener ll;
Location location;
final Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gps = (Button)findViewById(R.id.button2);
network = (Button)findViewById(R.id.button1);
lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
gps.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
});
network.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
}
});
ll = new LocationListener() {

@Override
public void onLocationChanged(Location loc) {
location = loc;
Toast.makeText(getApplicationContext(),"Latitude :"+String.valueOf(location.getLatitude())+"\nLongitude :"+String.valueOf(location.getLongitude()), Toast.LENGTH_LONG).show();
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
alertbox();
}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
}
public void alertbox()
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Alert");
alertDialogBuilder
.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
   startActivity(myIntent);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

}
}


Manifest

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


Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en