国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2020-04-11 11:44:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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

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是位置更新的間隔時(shí)間。listener是位置改變的監(jiān)聽(tīng)器,自己定義一個(gè)LocationListener(),重寫onLocationChanged(),加入位置改變時(shí)的動(dòng)作。

布局文件如下:

<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="時(shí)間:" />   <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);      }   };     // 獲取時(shí)間  public void get_time(){         now = new Date(System.currentTimeMillis());    str_date = formatter.format(now);    txt_time.setText("時(shí)間:" + str_date);  }}

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

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

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

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

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 连云港市| 恭城| 大冶市| 福州市| 阜阳市| 滦南县| 彭阳县| 宁陵县| 石柱| 灵丘县| 乌拉特后旗| 始兴县| 城市| 洛浦县| 景德镇市| 瑞昌市| 恭城| 永丰县| 都江堰市| 康定县| 鞍山市| 白朗县| 云和县| 瓦房店市| 庐江县| 项城市| 汶川县| 姚安县| 科尔| 达孜县| 上林县| 十堰市| 汉沽区| 会泽县| 宣恩县| 元阳县| 顺义区| 阿合奇县| 青浦区| 渝中区| 江都市|