激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Android - Android中GPS定位的用法實例

Android中GPS定位的用法實例

2021-03-08 14:25Android開發(fā)網(wǎng) Android

這篇文章主要介紹了Android中GPS定位的用法實例,是Android程序設(shè)計中比較經(jīng)典的應(yīng)用,需要的朋友可以參考下

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

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

?
1
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(),加入位置改變時的動作。

布局文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<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文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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)限:

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

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

Android中GPS定位的用法實例

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产精品高潮呻吟久久 | 91精品国产乱码久久久久 | japanesexxxxxxxhd | 99久久一区二区 | 亚洲日本韩国精品 | 中文字幕在线免费看 | 精品国产观看 | 欧美一级黄 | 澳门一级淫片免费视频 | 欧美精品久久久久久久久久 | 日韩视频一区二区在线观看 | 在线成人免费视频 | 国产呻吟 | 水卜樱一区二区av | 少妇一级淫片免费看 | 欧美a在线观看 | 91九色免费视频 | 激情毛片 | 精品久久久久久久久久久久久久 | 精品久久久久久久久久久久久 | 91久久99热青草国产 | 91视频成人入口 | 国产亚洲精品视频中文字幕 | 色偷偷一区 | 澳门一级淫片免费视频 | 女人一级一级毛片 | 久久福利剧场 | 国产精品久久av | 久色porn| 国产精品久久久久久久久久东京 | 日本免费大片免费视频 | www日韩大片 | 黄色免费网站在线播放 | 日韩av电影在线免费观看 | 小雪奶水翁胀公吸小说最新章节 | 51色视频| 国产成人精品一区在线播放 | 国产精品久久久久久久亚洲按摩 | 久久国产精品小视频 | 黄污免费网站 | 中国av中文字幕 |