Android GPS GPSBasics project hacking

一、参考源码:

  GPS Basic Example - Android Example

    http://androidexample.com/GPS_Basic__-__Android_Example/index.php?view=article_discription&aid=68&aaid=93

二、Permission:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

三、Example:

package com.example.gpsbasics;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity implements LocationListener {

    private LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /********** get Gps location service LocationManager object ***********/
        /********** 获取GPS服务管理对象 ************/
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        /*
          Parameters :
             First(provider)    :  the name of the provider with which to register
                                :  注册的名字
             Second(minTime)    :  the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
                                :  最小通知时间间隔,以毫秒为单位。此字段仅作为节省电力方式,并且位置更新之间的实际时间可以比该值更大或更小。
             Third(minDistance) :  the minimum distance interval for notifications, in meters
                                :  最小间隔通知,以毫秒为单位
             Fourth(listener)   :  a {#link LocationListener} whose onLocationChanged(Location) method will be called for each location update
                                :  每个位置更新时谁的onLocationChanged (位置)方法将被调用
        */

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                3000,   // 3 sec
                10, this);

        /********* After registration onLocationChanged method called periodically after each 3 sec ***********/
    }

    /************* Called after each 3 sec **********/
    @Override
    public void onLocationChanged(Location location) {

        // location.getLatitude(): 纬度
        // location.getLongitude(): 维度
        String str = "Latitude: "+location.getLatitude()+" \nLongitude: "+location.getLongitude();
        Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
        Log.e("GPSBasics", "onLocationChanged.");
    }

    @Override
    public void onProviderDisabled(String provider) {

        /******** Called when User off Gps *********/

        Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderEnabled(String provider) {

        /******** Called when User on Gps  *********/

        Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

 

时间: 2024-07-30 21:58:20

Android GPS GPSBasics project hacking的相关文章

Android Mokoid Open Source Project hacking

/***************************************************************************** * Android Mokoid Open Source Project hacking * * 声明: * 1. 本文主要是为了了解Android HAL工作机制,从而决定分析mokoid开源项目: * 2. 源代码URL:https://code.google.com/p/mokoid/source/checkout: * 3. 本文通

如何解决Android GPS没法定位的问题

大家去网上搜索Android定位location为null没法定位问题,估计有一大堆文章介绍如何来解决,但是最后大家发现基本没用.本文将从Android定位实现原理来深入分析没法定位原因并提出真正的解决方案.在分析之前,我们肯定得先看看android官方提供的定位SDK. 默认Android GPS定位实例 获取LocationManager: mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVI

android layout-菜鸟的用Android创建的project中的layout文件夹是空的怎么回事啊?

问题描述 菜鸟的用Android创建的project中的layout文件夹是空的怎么回事啊? 菜鸟的用Android创建的project中的layout文件夹是空的怎么回事啊?而且src包也是空的,怎么回事啊? 解决方案 我最近也遇到了同样的问题,你的有解决了么 解决方案二: 要不要截个图看看...按照新建工程的向导来做,不应该出现此情况的.file->new->android application progect ->输入包名 progect名字..->next next...

android gps获取坐标测速不准确,问题点在哪里

问题描述 android gps获取坐标测速不准确,问题点在哪里 做法: 1.用的是LocationListener监听实时坐标.监听和计算等操作都在一个service里完成. 2.监听设置为实时监听. lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener): 3.有一个3秒间隔的的循环任务,任务就是计算3秒前后的2个实时坐标的距离,并根据距离和间隔时间计算速度. 结果: 计算出来的速度和实际

Android GPS详解及示例代码_Android

LBS(Location Based Services)直译的话就是基于地理位置的服务,这里面至少有两层意思,第一要能轻易的获取当前的地理位置,譬如经纬度海拔等,另一个就是在当前位置的基础上提供增值服务,譬如找附近的加油站.餐馆.酒店等.这里面的第一步:获取用户当前位置,我们就可以用Android的GPS定位服务来得到.Android提供了基于网络的定位服务和基于卫星的定位服务两种.在设置->位置和安全设置里面的前三项就是,最后一个增强型GPS是为了辅助快速找卫星的.  那么我们现在就写一个简单

Android GPS定位详解及实例代码_Android

      GPS定位是智能手机上一个比较有意思的功能,LBS等服务都有效的利用了GPS定位功能.本文就跟大家分享下Android开发中的GPS定位知识.        一.Android基础知识准备        1.Activity类        每一种移动开发环境都有自己的基类.如J2ME应用程序的基类是midlets,BREW的基类是applets,而Android程序的基类是Activity.这个activity为我们提供了对移动操作系统的基本功能和事件的访问.这个类包含了基本的构造

用android studio新建project的问题

问题描述 用android studio新建project的问题 新建完project之后左侧不显示module 只有一对build.gradle文件,请问这是什么情况? 解决方案 找到解决方法了原因就是软件bug 解决方法:http://www.cnsecer.com/842.html 解决方案二: 新建的时候选择android module了吗

AM335x Android eMMC mkmmc-android.sh hacking

# AM335x Android eMMC mkmmc-android.sh hacking # # 1. 有空解读一下android的分区文件. # 2. 代码来源:https://github.com/hendersa/bbbandroid-external-ti_android_utilities/blob/master/am335x/mk-mmc/mkmmc-android.sh # # 2016-9-8 深圳 南山平山村 曾剑锋 #!/bin/bash # 如果参数只有一个,这里就会使

Android custom View AirConditionerView hacking

package com.example.arc.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.SweepGradient; import android.util.Attr