Monday, July 30, 2012

Settings.ACTION_LOCATION_SOURCE_SETTINGS vs. Settings.ACTION_SECURITY_SETTINGS

As described in my old post, we can "Start Location setting if GPS disabled" by start activity with intent of "Settings.ACTION_SECURITY_SETTINGS".

Thanks for Brian comments:

It is better to use Settings.ACTION_LOCATION_SOURCE_SETTINGS instead of Settings.ACTION_SECURITY_SETTINGS.

The reason is that some phones (such as HTC Desire) have moved the GPS settings out of the Security page. Using the suggested intent gets the user to the GPS settings wherever they are.



Set GPS

package com.AndroidGPS;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidGPS extends Activity {
 
 TextView textGpsStatus;
 Button buttonSetGPS_ACTION_LOCATION_SOURCE_SETTINGS;
 Button buttonSetGPS_ACTION_SECURITY_SETTINGS;
 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      textGpsStatus = (TextView)findViewById(R.id.gpsstatus);
    
      buttonSetGPS_ACTION_LOCATION_SOURCE_SETTINGS
       = (Button)findViewById(R.id.setgps_ACTION_LOCATION_SOURCE_SETTINGS);  
    
      buttonSetGPS_ACTION_LOCATION_SOURCE_SETTINGS.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
       startActivity(intent);
   }});
    
      buttonSetGPS_ACTION_SECURITY_SETTINGS
   = (Button)findViewById(R.id.setgps_ACTION_SECURITY_SETTINGS);  

      buttonSetGPS_ACTION_SECURITY_SETTINGS.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
      startActivity(intent);
  }});

  }

  @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  displayGpsStatus();
 }

 private void displayGpsStatus(){
   ContentResolver contentResolver = getBaseContext().getContentResolver();
      boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
      if(gpsStatus){
       textGpsStatus.setText("GPS: ON");
      }else{
       textGpsStatus.setText("GPS: OFF");
      }
  }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<TextView
  android:id="@+id/gpsstatus"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
<Button
  android:id="@+id/setgps_ACTION_LOCATION_SOURCE_SETTINGS"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Turn On/Off GPS Using ACTION_LOCATION_SOURCE_SETTINGS"
  />
<Button
  android:id="@+id/setgps_ACTION_SECURITY_SETTINGS"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Turn On/Off GPS Using ACTION_SECURITY_SETTINGS"
  />
</LinearLayout>

No comments:

Post a Comment