Android GPS 操作

强制关闭GPS:

// 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(this, 0, GPSIntent, 0).send();
// } catch (CanceledException e) {
// e.printStackTrace();
// }

贴上代码

package com.kingnode.pubcloud.service;


import com.kingnode.pubcloud.utils.PubConstant;


import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;


/**
 * 
 *  监听GPS开始关闭状态 服务
 * @author aiwenping@kingnode.com
 * @data: 2014-4-27 下午3:58:24
 * @version: V1.0
 */
public class LocationService extends Service implements LocationListener {


private final String TAG = "LocationService";
private final Context mContext = LocationService.this;


// 标记GPS状态
boolean isGPSEnabled = false;


// 标记网络状态
boolean isNetworkEnabled = false;


boolean canGetLocation = false;


Location location; // 位置
double latitude; // 纬度
double longitude; // 经度


// 更新的最短距离 以米为单位
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 米


// 更新的最短时间 以毫秒为单位
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 分钟


// 声明一个 Location Manager
protected LocationManager locationManager;


@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();


Log.d(TAG, "初始化地理位置信息服务");


getLocation();
}


public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// 获取GPS状态
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.d(TAG, "获取GPS状态" + isGPSEnabled);
// 获取网络状态
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.d(TAG, "获取网络状态" + isNetworkEnabled);
if (!isGPSEnabled && !isNetworkEnabled) {
// 网络和GPS都不能使用
} else {
this.canGetLocation = true;
Log.d(TAG, "首先通过网络获取位置信息");
// 首先通过网络获取位置信息
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// 如果GPS打开 通过GPS服务获取纬度/经度信息
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Intent intent = new Intent();
Log.d(TAG, "获取   gps 的经纬度 is " + latitude
+ "  ' " + longitude);
intent.setAction(PubConstant.LOCATION_BROADCAST_ACTION_GPS);
sendBroadcast(intent);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}


@Override
public void onLocationChanged(Location location) {


Log.d(TAG, "onLocationChanged");
}


@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, " onProviderDisabled is " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged ");
}


@Override
public IBinder onBind(Intent arg0) {
return null;
}


/**
* 获取纬度
* */
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}


// 返回纬度
return latitude;
}


/**
* 获取经度
* */
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}


// 返回经度
return longitude;
}


/**
* 检查是否可以获取位置信息

* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}


}

使用GPS 定位,首先,需要在清单文件(AndroidManifest.xml)中注册获取定位的权限: **1.获取位置管理器对象LocationManager** ``` import android.location.LocationManager; LocationManager lm; // lm =(LocationManager) this.getSystemService(Context`.LOCATION_SERVICE); // ``` **2.一般使用LocationManager的getLastKnownLocation(LocationManager.GPS_PROVIDER);方法获取Location对象** ``` String provider = LocationManager.GPS_PROVIDER;// 指定LocationManager的定位方法 Location location = locationManager.getLastKnownLocation(provider);// 调用getLastKnownLocation()方法获取当前的位置信息 ``` 不过不建议用这种方法,有几点原因: 一,在很多提供定位服务的应用程序中,不仅需要获取当前的位置信息,还需要监视位置的变化,在位置改变时调用特定的处理方法 ,其中LocationManager提供了一种便捷、高效的位置监视方法requestLocationUpdates(),可以根据位置的距离变化和时间间隔设定,产生位置改变事件的条件,这样可以避免因微小的距离变化而产生大量的位置改变事件 。 二,当你开启GPS,provider的值为GPS。这时的定位方式为GPS,由于GPS定位慢,所以它不可能立即返回你一个Location对象,所以就返回null了。 **3.推荐locationManager.requestLocationUpdates();方法** LocationManager中设定监听位置变化的代码如下: ``` lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10,new MyLocationListener()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值