Showing posts with label GPS. Show all posts
Showing posts with label GPS. Show all posts

Sunday, 17 November 2013

Turn On GPS Programmatically

Hi Friends,

Here i am going to show you how we can start GPS pro-grammatically.

Example:






Codes :

activity_main.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="20dp"
            android:text="Turn on GPS Programmatically"
            android:textStyle="bold" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="50dp"
            android:text="Start GPS" />

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

</LinearLayout>

MainActivity.java

package in.androiddevelopmentanddiscussion.turningongpsprogrammatically;

import com.example.turningongpsprogrammatically.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button start,stop;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

start = (Button)findViewById(R.id.button1);
stop = (Button)findViewById(R.id.button2);

start.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
       intent.putExtra("enabled", true);
       sendBroadcast(intent);
}
});     
        
stop.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
       intent.putExtra("enabled", false);
       sendBroadcast(intent);

}
});

}

@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;
}


}

Manifest

Add below permissions in Manifest

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


Download full project from here



Check Android Apps on Google Play

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

 

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