Android GPS 操作

来源:互联网 发布:怎样在mac上装office 编辑:程序博客网 时间:2024/06/04 19:39

强制关闭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;
}


}

0 0