亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中GPS定位的用法實(shí)例

 更新時間:2014年09月03日 10:32:42   投稿:shichen2014  
這篇文章主要介紹了Android中GPS定位的用法實(shí)例,是Android程序設(shè)計中比較經(jīng)典的應(yīng)用,需要的朋友可以參考下

GPS定位是目前很多手機(jī)都有的功能,且非常實(shí)用。本文以實(shí)例形式講述了Android中GPS定位的用法。分享給大家供大家參考之用。具體方法如下:

一般在Android中通過GPS獲得當(dāng)前位置,首先要獲得一個LocationManager實(shí)例,通過該實(shí)例的getLastKnownLocation()方法獲得第一個的位置,該方法的說明如下:

void android.location.LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

provider即定位方式,可以采用GPS定位(LocationManager.GPS_PROVIDER)或者網(wǎng)絡(luò)定位(LocationManager.NETWORK_PROVIDER),本文是GPS定位,因此使用LocationManager.GPS_PROVIDER。minTime是位置更新的間隔時間。listener是位置改變的監(jiān)聽器,自己定義一個LocationListener(),重寫onLocationChanged(),加入位置改變時的動作。

布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
 
  <TextView
    android:id="@+id/txt_time"
    style="@style/my_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="時間:" />
 
  <TextView
    android:id="@+id/txt_lat"
    style="@style/my_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="經(jīng)度:" />
 
  <TextView
    android:id="@+id/txt_lng"
    style="@style/my_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="緯度:" />
 
</LinearLayout>

MainActivity.java文件如下:

package com.hzhi.my_gps;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
   
  TextView txt_time;
  TextView txt_lat;
  TextView txt_lng;
  LocationManager lom;
  Location loc;
  Double lat;
  Double lng;
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date now;
  String str_date;
  Timer timer;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
     
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     
    get_con();
    get_gps();
     
    timer = new Timer(true);
    timer.schedule(task, 0, 1000);
  }
 
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
   
  public void get_gps(){
     
    lom = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    loc = lom.getLastKnownLocation(LocationManager.GPS_PROVIDER);
     
    if (loc != null) {
      lat = loc.getLatitude();
      lng = loc.getLongitude();
      txt_lat.setText("緯度:" + String.valueOf(lat));
      txt_lng.setText("經(jīng)度:" + String.valueOf(lng));
    }
    else{
      txt_lat.setText("緯度:未知" );
      txt_lng.setText("經(jīng)度:未知" );
    }
     
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = lom.getBestProvider(criteria, true);
     
    lom.requestLocationUpdates(provider, 1000, 10, los);
  }
   
  LocationListener los = new LocationListener(){
    public void onLocationChanged(Location location){
      if (location != null) {
        lat = location.getLatitude();
        lng = location.getLongitude();
        txt_lat.setText("緯度:" + String.valueOf(lat));
        txt_lng.setText("經(jīng)度:" + String.valueOf(lng));
      }
      else{
        txt_lat.setText("緯度:未知" );
        txt_lng.setText("經(jīng)度:未知" );
      }
    };
     
    public void onProviderDisabled(String provider){
     
    };
     
    public void onProviderEnabled(String provider){ };
     
    public void onStatusChanged(String provider, int status,
    Bundle extras){ };
  };
   
  // 獲取控件
  public void get_con(){
     
    txt_time = (TextView) findViewById(R.id.txt_time);
    txt_lat = (TextView) findViewById(R.id.txt_lat);
    txt_lng = (TextView) findViewById(R.id.txt_lng);
  }
   
  Handler handler = new Handler(){
     
    public void handleMessage(Message msg){
      switch (msg.what){
      case 1:
        get_time();
        break;
      }
    }
  };
   
  TimerTask task = new TimerTask(){ 
     public void run() { 
       Message message = new Message();   
       message.what = 1;   
       handler.sendMessage(message);  
    } 
  };
   
  // 獲取時間
  public void get_time(){
     
    now = new Date(System.currentTimeMillis());
    str_date = formatter.format(now);
    txt_time.setText("時間:" + str_date);
  }
}

在AndroidManifest.xml文件中加入權(quán)限:

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

運(yùn)行前先打開GPS衛(wèi)星,運(yùn)行結(jié)果如下圖所示:

讀者可以在室外信號充足的地方試試,是可以獲取GPS位置的。

希望本文所述對大家的Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android通過手勢實(shí)現(xiàn)的縮放處理實(shí)例代碼

    Android通過手勢實(shí)現(xiàn)的縮放處理實(shí)例代碼

    Android通過手勢實(shí)現(xiàn)的縮放處理實(shí)例代碼,需要的朋友可以參考一下
    2013-05-05
  • android獲取圖片尺寸的兩種方式及bitmap的縮放操作

    android獲取圖片尺寸的兩種方式及bitmap的縮放操作

    這篇文章主要介紹了android獲取圖片尺寸的兩種方式及bitmap的縮放操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Kotlin協(xié)程的基礎(chǔ)與使用示例詳解

    Kotlin協(xié)程的基礎(chǔ)與使用示例詳解

    這篇文章主要為大家介紹了Kotlin協(xié)程的基礎(chǔ)與使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Android 自定義Switch開關(guān)按鈕的樣式實(shí)例詳解

    Android 自定義Switch開關(guān)按鈕的樣式實(shí)例詳解

    本文主要講的是在Android原生Switch控件的基礎(chǔ)上進(jìn)行樣式自定義,內(nèi)容很簡單,但是在實(shí)現(xiàn)的過程中還是遇到了一些問題,在此記錄下來,需要的朋友參考下吧
    2017-12-12
  • Android用Canvas繪制貝塞爾曲線

    Android用Canvas繪制貝塞爾曲線

    這篇文章主要為大家詳細(xì)介紹了Android用Canvas繪制貝塞爾曲線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android?源碼淺析RecyclerView?Adapter

    Android?源碼淺析RecyclerView?Adapter

    這篇文章主要介紹了Android?源碼淺析之RecyclerView?Adapter示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android利用RecyclerView實(shí)現(xiàn)全選、置頂和拖拽功能示例

    Android利用RecyclerView實(shí)現(xiàn)全選、置頂和拖拽功能示例

    列表控件可以說是我們絕大部分App中都會使用的,為了提升交互樂趣,我們經(jīng)常需要在列表中加入置頂、拖拽等操作,下面這篇文章主要介紹了Android利用RecyclerView如何實(shí)現(xiàn)全選、置頂和拖拽功能的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-04-04
  • Android仿直播類app贈送禮物功能

    Android仿直播類app贈送禮物功能

    這篇文章主要介紹了Android仿直播類app贈送禮物功能,本文通過實(shí)例代碼效果圖展示的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • 基于linux與windows平臺下 如何下載android sdk源代碼的方法詳解

    基于linux與windows平臺下 如何下載android sdk源代碼的方法詳解

    本文主要是介紹在linux和windows平臺下,如何下載android sdk的源代碼,注意是sdk的源代碼,而不是android的所有源代碼,同時介紹如何把sdk源代碼加入到eclipse里,使android 平臺手機(jī)開發(fā)者可以直接查看源代碼,通過閱讀SDK源碼,能更好的理解和運(yùn)用Android的API
    2013-05-05
  • Android 表格布局TableLayout示例詳解

    Android 表格布局TableLayout示例詳解

    本文主要介紹Android TableLayout布局,這里整理了TableLayout的資料,并附示例代碼和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下
    2016-08-08

最新評論