GPS

1.构建一个GPS的测试的Activity,实时获取 经度和纬度(可找出附近的人儿) 

public class GPS_testActivity extends Activity
{
 /** Called when the activity is first created. */
 private LinearLayout mainView = null;
 private TextView infoView = null;
 private TextView locationView = null;

 private LocationManager locationManager = null;
 private LocationListener locationListener = null;

 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  mainView = new LinearLayout(this);
  mainView.setOrientation(LinearLayout.VERTICAL);
  infoView = new TextView(this);
  mainView.addView(infoView);
  locationView = new TextView(this);
  mainView.addView(locationView);
  setContentView(mainView);
  locationManager_init();
 }

 /* locationManager初始化 */
 void locationManager_init()
 {
  locationManager = (LocationManager) this
    .getSystemService(GPS_testActivity.LOCATION_SERVICE);
  locationListener_init();
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
    1000, 0, locationListener);
 }

 /* locatonListener初始化 */
 void locationListener_init()
 {

  locationListener = new LocationListener()
  {
   // 位置变化时触发
   public void onLocationChanged(Location location)
   {
    System.out.println("onLocationChanged");
    locationView.setText("时间:" + location.getTime() + "\n");
    locationView.append("经度:" + location.getLongitude() + "\n");
    locationView.append("纬度:" + location.getLatitude() + "\n");
    locationView.append("海拔:" + location.getAltitude() + "\n");
   }

   // gps禁用时触发
   public void onProviderDisabled(String provider)
   {
    System.out.println("onProviderDisabled");
    Toast.makeText(GPS_testActivity.this, "请开启GPS!",
      Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
    startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
   }

   // gps开启时触发
   public void onProviderEnabled(String provider)
   {
    Toast.makeText(GPS_testActivity.this, "GPS正常",
      Toast.LENGTH_SHORT).show();
   }

   // gps状态变化时触发
   public void onStatusChanged(String provider, int status,
     Bundle extras)
   {
    System.out.println("onStatusChanged");
    if (status == LocationProvider.AVAILABLE)
    {
     infoView.setText("当前GPS状态:可见的\n");
    } else if (status == LocationProvider.OUT_OF_SERVICE)
    {
     infoView.setText("当前GPS状态:服务区外\n");
    } else if (status == LocationProvider.TEMPORARILY_UNAVAILABLE)
    {
     infoView.setText("当前GPS状态:暂停服务\n");
    }
   }
  };
 }

}

 

说明:客户端得到gps数据传给服务器端处理。。最后显示出服务返回的数据 搞定!用户看起来很神奇的功能,在代码里并不复杂。不过服务器端处理数据稍微麻烦些,需要根据经纬度找出你附近的人儿

 

2.android设置gps自动开启方法总结

 1.第一种方法
 private void toggleGPS()
 {
  Intent gpsIntent = new Intent();
  gpsIntent.setClassName("com.android.settings",
    "com.android.settings.widget.SettingsAppWidgetProvider");
  gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
  gpsIntent.setData(Uri.parse("custom:3"));
  try
  {
   PendingIntent.getBroadcast(StartActivity.this, 0, gpsIntent, 0)
     .send();
  } catch (CanceledException e)
  {
   e.printStackTrace();
  }
 }

 

 

    2.第二种方法
    private void openGPSSettings()
    {       
       //获取GPS现在的状态(打开或是关闭状态)
     boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER );
 
     if(gpsEnabled)
     {

 

     //关闭GPS
      Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, false );
     }
     else
     {
      //打开GPS
      Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, true);

 

     }

 

      3.第三种方法(手动设置)
      LocationManager alm = (LocationManager)StartActivity.this.getSystemService(Context.LOCATION_SERVICE);      
      if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
      {           
       Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
      }       
     
      Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
      Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
      startActivityForResult(intent,0); //此为设置完成后返回到获取界面

说明: 第一第二种需要加上权限
   <!--允许程序读取或写入系统设置 -->
   <uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
   <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

 

3. android程序自动开启GPS功能完整代码实例

/**
 * GPS设置类
 *
 * @author hedandan
 *
 */
public class SettingsGPS
{
 /**
  * 开启GPS模式
  */
 public static void openGPS(Context context)
 {
  boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
    context.getContentResolver(), LocationManager.GPS_PROVIDER);

  if (!gpsEnabled)
  {
   toggleGPS(context);

   Toast.makeText(context, "GPS模式成功开启!", Toast.LENGTH_SHORT).show();
  }
 }

 /**
  * 关闭GPS模式
  */
 public static void closeGPS(Context context)
 {
  boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
    context.getContentResolver(), LocationManager.GPS_PROVIDER);

  if (gpsEnabled)
  {
   toggleGPS(context);

   Toast.makeText(context, "GPS模式成功关闭!", Toast.LENGTH_SHORT).show();
  }
 }

 /**
  * GPS模式开关
  */
 public static void toggleGPS(Context context)
 {
  Intent gpsIntent = new Intent();

  gpsIntent.setClassName("com.android.settings",
    "com.android.settings.widget.SettingsAppWidgetProvider");

  gpsIntent.addCategory("android.intent.category.ALTERNATIVE");

  gpsIntent.setData(Uri.parse("custom:3"));

  try
  {
   PendingIntent.getBroadcast(context, 0, gpsIntent, 0).send();
  } catch (CanceledException e)
  {
   e.printStackTrace();
  }
 }
}

说明:这是一个接口工具类,在android程序中只需调用此接口就可以实现GPS功能的自动开启与关闭

转载于:https://www.cnblogs.com/hedandan/archive/2012/08/10/2631980.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值