Android 设置GPS并等待启用
我在设置GPS服务时遇到问题。我想检查GPS是否启用。如果禁用,它将显示一个对话框供用户打开。问题在于它不是等待用户启用GPS并获取位置 我有一节课Android 设置GPS并等待启用,android,gps,sequence,wait,Android,Gps,Sequence,Wait,我在设置GPS服务时遇到问题。我想检查GPS是否启用。如果禁用,它将显示一个对话框供用户打开。问题在于它不是等待用户启用GPS并获取位置 我有一节课 public class GPSTracker extends Service implements LocationListener{ private final Context mContext; boolean isGPSEnable = false; boolean isNetworkEnable = false; boolean ca
public class GPSTracker extends Service implements LocationListener{
private final Context mContext;
boolean isGPSEnable = false;
boolean isNetworkEnable = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_FOR_UPDATE = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATE = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManager;
public GPSTracker(Context ctx) {
this.mContext = ctx;
getLocation();
}
public Location getLocation()
{
try {
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnable)
{
showSettingsGPSAlert();
}
else if(!isNetworkEnable)
{
showSettingsNetWorkAlert();
}
else
{
this.canGetLocation = true;
if(isNetworkEnable)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATE,
MIN_DISTANCE_FOR_UPDATE, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnable) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATE,
MIN_DISTANCE_FOR_UPDATE, 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();
}
}
}
}
}
}catch (Exception ex)
{
Log.e("<<Location Error>>",ex.getMessage());
}
return location;
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public boolean canGetGPS() {
return this.isGPSEnable;
}
public boolean canGetNetwork() {
return this.isNetworkEnable;
}
public void showSettingsGPSAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startService(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
public void showSettingsNetWorkAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("NetWork is settings");
// Setting Dialog Message
alertDialog.setMessage("NetWork is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
公共类GPSTracker扩展服务实现LocationListener{
私有最终上下文mContext;
布尔值isGPSEnable=false;
布尔值isNetworkEnable=false;
布尔值canGetLocation=false;
位置;
双纬度;
双经度;
更新的专用静态最终长最小距离=10;//10米
私有静态最终长分钟时间更新=1000*60*1;//1分钟
受保护的LocationManager LocationManager;
公共GPSTracker(上下文ctx){
this.mContext=ctx;
getLocation();
}
公共位置getLocation()
{
试一试{
locationManager=(locationManager)mContext.getSystemService(LOCATION\u服务);
isGPSEnable=locationManager.isProviderEnabled(locationManager.GPS\U提供程序);
isNetworkEnabled=locationManager.isProviderEnabled(locationManager.NETWORK\u提供程序);
如果(!isGPSEnable)
{
showSettingsGPSAlert();
}
如果(!isNetworkEnable),则为else
{
showSettingsNetWorkAlert();
}
其他的
{
this.canGetLocation=true;
如果(是否可联网)
{
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,
最短时间更新,
用于更新的最小距离);
Log.d(“网络”、“网络”);
如果(locationManager!=null){
位置=位置管理器
.getLastKnownLocation(LocationManager.网络提供商);
如果(位置!=null){
纬度=位置。getLatitude();
longitude=location.getLongitude();
}
}
}
if(isGPSEnable){
if(位置==null){
locationManager.RequestLocationUpdate(
LocationManager.GPS\u提供程序,
最短时间更新,
用于更新的最小距离);
Log.d(“GPS启用”、“GPS启用”);
如果(locationManager!=null){
位置=位置管理器
.getLastKnownLocation(LocationManager.GPS\U提供商);
如果(位置!=null){
纬度=位置。getLatitude();
longitude=location.getLongitude();
}
}
}
}
}
}捕获(例外情况除外)
{
Log.e(“,例如getMessage());
}
返回位置;
}
使用GPS()的公共无效停止{
如果(locationManager!=null){
locationManager.RemoveUpdate(GPSTracker.this);
}
}
公共双纬度(){
如果(位置!=null){
纬度=位置。getLatitude();
}
//返回纬度
返回纬度;
}
公共双getLongitude(){
如果(位置!=null){
longitude=location.getLongitude();
}
//返回经度
返回经度;
}
公共布尔canGetLocation(){
返回此.canGetLocation;
}
公共布尔值canGetGPS(){
返回此.isGPSEnable;
}
公共网络(){
返回此文件。isNetworkEnable;
}
公共无效显示设置PSAlert(){
AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(mContext);
//设置对话框标题
alertDialog.setTitle(“GPS是设置”);
//设置对话框消息
alertDialog.setMessage(“GPS未启用。是否转到设置菜单?”);
//按“设置”按钮时
alertDialog.setPositiveButton(“设置”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
意向意向=新意向(设置、动作、位置、来源、设置);
mContext.startService(意图);
}
});
//按取消按钮时
alertDialog.setNegativeButton(“取消”,新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
//显示警报消息
alertDialog.show();
}
public void showSettingsNetWorkAlert(){
AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(mContext);
//设置对话框标题
alertDialog.setTitle(“网络即设置”);
//设置对话框消息
alertDialog.setMessage(“网络未启用。是否转到设置菜单?”);
//按“设置”按钮时
alertDialog.setPositiveButton(“设置”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
意向意向=新意向(设置、操作、网络、操作员、设置);
mContext.startActivity(意图);
}
});
//按取消按钮时
alertDialog.setNegativeButton(“取消”,新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
//显示警报消息
alertDialog.show();
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
已更改位置上的公共无效(位置){
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
}
我有课用它
public class RestaurantListFragment extends ListFragment {
private ArrayList<Restaurant> restaurants;
private SQLDataHelper dataHelper;
private GPSTracker gps;
private double UserLatitude;
private double UserLongitude;
private ProgressDialog progressBar;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.restaurant_list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
gps = new GPSTracker(getActivity());
UserLatitude = gps.getLatitude();
UserLongitude = gps.getLongitude();
Toast.makeText(getActivity(),UserLatitude + "|" + UserLongitude,Toast.LENGTH_SHORT).show();
dataHelper = new SQLDataHelper(getActivity(), "restaurantDB");
restaurants = new ArrayList<Restaurant>();
dataHelper.openDB();
Cursor cursor = dataHelper.query("Restaurant", new String[]{"Id", "ResName", "Logo", "Address", "Latitude", "Longitude"}, null
, null, null, null, null);
if (cursor.moveToFirst()) {
do {
Restaurant restaurant = new Restaurant();
restaurant.setId(cursor.getInt(0));
restaurant.setResName(cursor.getString(1));
restaurant.setLogo(cursor.getString(2));
restaurant.setAddress(cursor.getString(3));
restaurants.add(restaurant);
} while (cursor.moveToNext());
}
//Collections.sort(restaurants);
RestaurantAdapter restaurantAdapter = new RestaurantAdapter(getActivity(),restaurants);
setListAdapter(restaurantAdapter);
}
公共类RestaurantListFragment扩展ListFragment{
私人ArrayList餐厅;
私有SQLDataHelper-dataHelper;
私人全球定位系统;
私人双用户纬度;
二等兵