Monday 28 October 2013

Android-GPS-Listener-Example

          Android-GPS-Listener-Example             

In Android, The location of Device (Phone)  we can get by Two way.
1. Using Network Provider
2. Using GPS.
Network Provider depend upon your SIM, while GPS depend upon Gps Hardware device.
Now I am going to explain how to create an application that show your device position.

Simple Android Example:

Create an Android Application project named LocationExample.                         


step1: Code for layout xml file: 


    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   
        android:id="@+id/textView1"
        android:layout_width="266dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        
        android:text="My Position"
        android:textAppearance="?android:attr/textAppearanceLarge" />

   

step 2 : code for Activity source java file.

package com.example.locationexample;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView txt1;
LocationManager locationManager ;
LocationListener locationListener;
private Button btn1,btn2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
       
        txt1 = (TextView)findViewById(R.id.textView1);
        btn1 = (Button)findViewById(R.id.button1);
        btn2 = (Button)findViewById(R.id.button2);
        
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
       locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
              
              txt1.setText(String.valueOf(location.getLatitude())+","+String.valueOf(location.getLongitude()));
         Toast.makeText(MainActivity.this,"New Location detected"
        ,Toast.LENGTH_SHORT).show();
            }

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

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {
            Toast.makeText(MainActivity.this,"GPS/Use Wireless network is not enabled" ,Toast.LENGTH_SHORT).show();
           
           
            }
          };

        // Register the listener with the Location Manager to receive location updates
          
          btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
});
        
          btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
});

    }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Step 3: you have to add Three permission   in manifest file by this way..
1.  android.permission.ACCESS_COARSE_LOCATION




2. android.permission.ACCESS_FINE_LOCATION 
3. android.permission.INTERNET  permission


uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
    uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
    uses-permission android:name="android.permission.INTERNET"
step4: Now your application is ready for run. Just Test it.

Open DDMS tool by this way:-
Windows-> Show Views -> other->Android->ShowView->Emulator Control.




Open an Android Google API Emulator.
Now Test your Application.






No comments:

Today's Pageviews