Check GPS status in Android Studio

Objective:
To check GPS status

Function:
When the button is pressed, small pop up will be displayed to inform the status of GPS. GPS satellites has to be selected by user manually. You cannot enable the GPS directly in your code for various reasons such as battery drain and privacy.

IDE:
Android Studio 1.0.2

Source Code:
The project can be downloaded from here.


Result:




Explanation:

1. As usual, we need to add permission to AndroidManifest.xml




2. Then we need to commit the Fragment in MainActivity.java.

/*way of adding fragment with java*/
        //FragmentOne is the class which inflate the fragment_one.xml and return the view
        FragmentOne myFragment = new FragmentOne();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        //'container' is android id in activity_main.xml
        //add android:id="@+id/container" in activity_main.xml
        transaction.add(R.id.container, myFragment, "main_layout_container");
        transaction.commit();

3. In FragmentOne.java, we first setup button to display
//create Button 'button' and link with button id from fragment_one.xml
        Button button = (Button) view.findViewById(R.id.button);

4. In FragmentOne.java, we implement OnClickListener to detect the pressing of the button.Also take note that Object of gpsHelperClass is created with Context which will be used in LocationManager.

/*define OnClickListener for button here*/
        button.setOnClickListener(new View.OnClickListener() {
            //create object and pass context to it.
            GpsHelperClass gpsHelperClass = new GpsHelperClass(context);

            public void onClick(View v) {
                //get boolean result and display small pop up
                if (gpsHelperClass.isGpsReadyToUse()) {
                    Toast.makeText(context, "GPS is enable",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "GPS is not enable",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

5. In GpsHelperClass.java, the context passed from FragmentOne.java is first used in constructor.
    //at constructor, context is passed from FragmentOne.java and context will be used by GpsHelperClass throughout.
    public GpsHelperClass(Context context) {
        this.context = context;
    }

6. In GpsHelperClass.java, we first setup LocationManger and return the GPS status back
 public boolean isGpsReadyToUse() {
        //this.context.getSystemService is required if we want to use in separate class
        locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE);
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

Complete Code:


Content of MainActivity.java
package com.example.osdevlab.simpletutorial;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;


public class MainActivity extends ActionBarActivity {

    public static Context context;


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

        /*way of adding fragment with java*/
        //FragmentOne is the class which inflate the fragment_one.xml and return the view
        FragmentOne myFragment = new FragmentOne();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        //'container' is android id in activity_main.xml
        //add android:id="@+id/container" in activity_main.xml
        transaction.add(R.id.container, myFragment, "main_layout_container");
        transaction.commit();
    }


}

Content of FragmentOne.java
package com.example.osdevlab.simpletutorial;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by osdevlab on 12/29/14.
 */
public class FragmentOne extends Fragment {

    Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout with fragment_one.xml
        View view = inflater.inflate(R.layout.fragment_one, container, false);

        //create Button 'button' and link with button id from fragment_one.xml
        Button button = (Button) view.findViewById(R.id.button);

        //returns the Activity the Fragment is currently associated with
        //In Fragment, this step requires to pass context to other class.
        context = getActivity();

        /*define OnClickListener for button here*/
        button.setOnClickListener(new View.OnClickListener() {
            //create object and pass context to it.
            GpsHelperClass gpsHelperClass = new GpsHelperClass(context);

            public void onClick(View v) {
                //get boolean result and display small pop up
                if (gpsHelperClass.isGpsReadyToUse()) {
                    Toast.makeText(context, "GPS is enable",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "GPS is not enable",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        return view;
    }
}

Content of GpsHelperClass.java
package com.example.osdevlab.simpletutorial;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

/**
 * Created by osdevlab on 1/1/15.
 */

//we need to first implements LocationListener
//Four functions will be provided by IDE when implements LocationListener and we have to override those functions.
//For now, we don't need to override those functions yet.

public class GpsHelperClass implements LocationListener {

    protected LocationManager locationManager;
    Context context;

    //at constructor, context is passed from FragmentOne.java and context will be used by GpsHelperClass throughout.
    public GpsHelperClass(Context context) {
        this.context = context;
    }

    public boolean isGpsReadyToUse() {
        //this.context.getSystemService is required if we want to use in separate class
        locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE);
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    @Override
    public void onLocationChanged(Location location) {

    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

No comments:

Post a Comment