Tuesday, January 19, 2016

android - How to enable disable location service

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#cfd4ca"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:padding="25sp"
        android:fontFamily="sans-serif-condensed"
        android:textStyle="bold"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:text="Enable Location Service"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Request window feature action bar
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Change the action bar color
        getSupportActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#FF246AB4"))
        );

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mButton = (Button) findViewById(R.id.btn);
        mTextView = (TextView) findViewById(R.id.tv);

        // Check the location service status
        if(isLocationEnabled()){
            mTextView.setText("Location Service Is Enabled.");
            mTextView.setTextColor(Color.BLUE);
            mButton.setText("Disable Location Service");
        }else {
            mTextView.setText("Location Service Is Disabled.");
            mTextView.setTextColor(Color.RED);
            mButton.setText("Enable Location Service");
        }

        // Set a click listener for button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(locationIntent);
            }
        });
    }

    // Better way to check location service status
    protected boolean isLocationEnabled(){
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
            // check location state for api version 19 or greater
            int locationMode = Settings.Secure.getInt(
                    mContext.getContentResolver(),
                    Settings.Secure.LOCATION_MODE,
                    0
            );

            return locationMode != Settings.Secure.LOCATION_MODE_OFF;
        }else{
            String locationProviders = Settings.Secure.getString(
                    mContext.getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED
                );
            return !TextUtils.isEmpty(locationProviders);
        }
    }

    @Override
    public void onResume(){
        super.onResume();
        // Check the location service status
        if(isLocationEnabled()){
            mTextView.setText("Location Service Is Enabled.");
            mTextView.setTextColor(Color.BLUE);
            mButton.setText("Disable Location Service");
        }else {
            mTextView.setText("Location Service Is Disabled.");
            mTextView.setTextColor(Color.RED);
            mButton.setText("Enable Location Service");
        }
    }
}
AnidroidManifest.xml [Permission]

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