Android實(shí)現(xiàn)手機(jī)定位的案例代碼
Android手機(jī)定位案例代碼
代碼如下:
package com.xuliugen.gpsdemo;
import com.itheima.gpsdemo.R;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
/**
* 手機(jī)定位程序代碼
* @author xuliugen
*/
public class MainActivity extends Activity {
// 用到位置服務(wù)
private LocationManager lm;
private MyLocationListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
// 獲得定位的方式
// List<String> provider = lm.getAllProviders();
// for(String l: provider){
// System.out.println(l);
// }
listener = new MyLocationListener();
// 注冊(cè)監(jiān)聽(tīng)位置服務(wù)
// 給位置提供者設(shè)置條件
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// 設(shè)置參數(shù)細(xì)化:
// criteria.setAccuracy(Criteria.ACCURACY_FINE);//設(shè)置為最大精度
// criteria.setAltitudeRequired(false);//不要求海拔信息
// criteria.setBearingRequired(false);//不要求方位信息
// criteria.setCostAllowed(true);//是否允許付費(fèi)
// criteria.setPowerRequirement(Criteria.POWER_LOW);//對(duì)電量的要求
String proveder = lm.getBestProvider(criteria, true);
lm.requestLocationUpdates(proveder, 0, 0, listener);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// 取消監(jiān)聽(tīng)位置服務(wù)
lm.removeUpdates(listener);
listener = null;
}
class MyLocationListener implements LocationListener {
/**
* 當(dāng)位置改變的時(shí)候回調(diào)
*/
public void onLocationChanged(Location location) {
String longitude = "經(jīng)度:" + location.getLongitude();
String latitude = "緯度:" + location.getLatitude();
String accuracy = "精確度:" + location.getAccuracy();
TextView textview = new TextView(MainActivity.this);
textview.setText(longitude + "\n" + latitude + "\n" + accuracy);
setContentView(textview);
}
/**
* 當(dāng)狀態(tài)發(fā)生改變的時(shí)候回調(diào) 開(kāi)啟--關(guān)閉 ;關(guān)閉--開(kāi)啟
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
}
/**
* 某一個(gè)位置提供者可以使用了
*/
public void onProviderEnabled(String provider) {
}
/**
* 某一個(gè)位置提供者不可以使用了
*/
public void onProviderDisabled(String provider) {
}
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Android Studio使用Profiler來(lái)完成內(nèi)存泄漏的定位
- 解決Android原生定位的坑
- Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕指定位置彈出布局
- Android RecycleView滑動(dòng)停止后自動(dòng)吸附效果的實(shí)現(xiàn)代碼(滑動(dòng)定位)
- Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能的完整代碼
- android studio 使用Mocklocation虛擬定位
- 解決Android 10/Android Q手機(jī)在后臺(tái)無(wú)法正常定位問(wèn)題
- Android實(shí)現(xiàn)高德地圖顯示及定位
- Android使用網(wǎng)絡(luò)獲取定位的方法
- Android開(kāi)發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡(jiǎn)單實(shí)現(xiàn)
- Android百度地圖定位、顯示用戶當(dāng)前位置
- Android中WebView控件支持地理位置定位方法
- Android 簡(jiǎn)單服務(wù)定位器模式實(shí)現(xiàn)
相關(guān)文章
Android手勢(shì)滑動(dòng)實(shí)現(xiàn)兩點(diǎn)觸摸縮放圖片
這篇文章主要介紹了Android手勢(shì)滑動(dòng)實(shí)現(xiàn)兩點(diǎn)觸摸縮放圖片的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android通過(guò)SharedPreferences實(shí)現(xiàn)自動(dòng)登錄記住用戶名和密碼功能
最近使用SharedPreferences實(shí)現(xiàn)了一個(gè)android自動(dòng)登錄功能,特此分享到腳本之家平臺(tái)供大家參考2017-07-07
Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個(gè)數(shù)
本篇文章主要介紹了Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個(gè)數(shù),我們可以通過(guò)TextWatcher去觀察輸入框中輸入的內(nèi)容,有興趣的可以了解一下。2017-04-04
Android?WindowManager深層理解view繪制實(shí)現(xiàn)流程
WindowManager是Android中一個(gè)重要的Service,是全局且唯一的。WindowManager繼承自ViewManager。WindowManager主要用來(lái)管理窗口的一些狀態(tài)、屬性、view增加、刪除、更新、窗口順序、消息收集和處理等2022-11-11
Android?RecyclerBarChart繪制使用教程
這篇文章主要為大家介紹了Android?RecyclerBarChart繪制使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
android listview的多列模版實(shí)例代碼
這篇文章主要介紹了android listview的多列模版實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,具有參考價(jià)值,需要的朋友可以參考下2017-01-01
Android中 TeaScreenPopupWindow多類(lèi)型篩選彈框功能的實(shí)例代碼
這篇文章主要介紹了Android TeaScreenPopupWindow多類(lèi)型篩選彈框功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06
Android高級(jí)界面組件之拖動(dòng)條和評(píng)星條的功能實(shí)現(xiàn)
這篇文章主要介紹了Android高級(jí)界面組件之拖動(dòng)條和評(píng)星條的實(shí)現(xiàn)實(shí)例,需要的的朋友參考下2017-03-03
Android面試Intent采用了什么設(shè)計(jì)模式解析
這篇文章主要為大家介紹了Android面試Intent采用了什么設(shè)計(jì)模式解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

