最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中GPS定位的用法實例

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

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

一般在Android中通過GPS獲得當(dāng)前位置,首先要獲得一個LocationManager實例,通過該實例的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"/>

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

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

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

相關(guān)文章

  • Android中實現(xiàn)多線程操作的幾種方式

    Android中實現(xiàn)多線程操作的幾種方式

    多線程一直是一個老大難的問題,首先因為它難以理解,其次在實際工作中我們需要面對的關(guān)于線程安全問題也并不常見,今天就來總結(jié)一下實現(xiàn)多線程的幾種方式,感興趣的可以了解一下
    2021-06-06
  • Android獲取雙卡雙待手機的SIM卡信息示例代碼

    Android獲取雙卡雙待手機的SIM卡信息示例代碼

    這篇文章主要給大家介紹了關(guān)于Android獲取雙卡雙待手機的SIM卡信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Android 更新UI的方法匯總

    Android 更新UI的方法匯總

    這篇文章主要介紹了Android 更新UI的方法匯總的相關(guān)資料, 非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • Android使用文件進行數(shù)據(jù)存儲的方法

    Android使用文件進行數(shù)據(jù)存儲的方法

    這篇文章主要介紹了Android使用文件進行數(shù)據(jù)存儲的方法,較為詳細(xì)的分析了Android基于文件實現(xiàn)數(shù)據(jù)存儲所涉及的相關(guān)概念與使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • Android 中ListView點擊Item無響應(yīng)問題的解決辦法

    Android 中ListView點擊Item無響應(yīng)問題的解決辦法

    如果listitem里面包括button或者checkbox等控件,默認(rèn)情況下listitem會失去焦點,導(dǎo)致無法響應(yīng)item的事件,怎么解決呢?下面小編給大家分享下listview點擊item無響應(yīng)的解決辦法
    2016-12-12
  • 淺談Android studio 生成apk文件時的 key store path 的問題

    淺談Android studio 生成apk文件時的 key store path 的問題

    這篇文章主要介紹了淺談Android studio 生成apk文件時的 key store path 的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android學(xué)習(xí)系列一用按鈕實現(xiàn)顯示時間

    Android學(xué)習(xí)系列一用按鈕實現(xiàn)顯示時間

    這篇文章主要介紹了Android學(xué)習(xí)系列一用按鈕實現(xiàn)顯示時間的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • Android App增量更新詳解及實例代碼

    Android App增量更新詳解及實例代碼

    這篇文章主要介紹了Android App增量更新詳解的相關(guān)資料,并附實例代碼,要的朋友可以參考下
    2016-09-09
  • Android實現(xiàn)鎖屏熒光效果

    Android實現(xiàn)鎖屏熒光效果

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)熒光效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Android ListView滾動到底后自動加載數(shù)據(jù)

    Android ListView滾動到底后自動加載數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了Android之ListView滾動到底后自動加載數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評論

新乐市| 凌云县| 舒兰市| 冕宁县| 泌阳县| 岱山县| 耒阳市| 鄢陵县| 古田县| 疏附县| 晴隆县| 七台河市| 张家界市| 上思县| 梅河口市| 临西县| 民和| 武胜县| 吉木乃县| 湘乡市| 内丘县| 祁东县| 施甸县| 凤凰县| 江安县| 边坝县| 财经| 临海市| 鄂托克旗| 公主岭市| 鄂托克旗| 修水县| 浦城县| 开江县| 山丹县| 澄迈县| 金华市| 历史| 松潘县| 炎陵县| 怀集县|