Android實現(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();
// 注冊監(jiān)聽位置服務(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);//對電量的要求
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)聽位置服務(wù)
lm.removeUpdates(listener);
listener = null;
}
class MyLocationListener implements LocationListener {
/**
* 當(dāng)位置改變的時候回調(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ā)生改變的時候回調(diào) 開啟--關(guān)閉 ;關(guān)閉--開啟
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
}
/**
* 某一個位置提供者可以使用了
*/
public void onProviderEnabled(String provider) {
}
/**
* 某一個位置提供者不可以使用了
*/
public void onProviderDisabled(String provider) {
}
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- Android Studio使用Profiler來完成內(nèi)存泄漏的定位
- 解決Android原生定位的坑
- Android實現(xiàn)點(diǎn)擊某個按鈕指定位置彈出布局
- Android RecycleView滑動停止后自動吸附效果的實現(xiàn)代碼(滑動定位)
- Android 百度地圖定位實現(xiàn)仿釘釘簽到打卡功能的完整代碼
- android studio 使用Mocklocation虛擬定位
- 解決Android 10/Android Q手機(jī)在后臺無法正常定位問題
- Android實現(xiàn)高德地圖顯示及定位
- Android使用網(wǎng)絡(luò)獲取定位的方法
- Android開發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡單實現(xiàn)
- Android百度地圖定位、顯示用戶當(dāng)前位置
- Android中WebView控件支持地理位置定位方法
- Android 簡單服務(wù)定位器模式實現(xiàn)
相關(guān)文章
Android手勢滑動實現(xiàn)兩點(diǎn)觸摸縮放圖片
這篇文章主要介紹了Android手勢滑動實現(xiàn)兩點(diǎn)觸摸縮放圖片的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android通過SharedPreferences實現(xiàn)自動登錄記住用戶名和密碼功能
最近使用SharedPreferences實現(xiàn)了一個android自動登錄功能,特此分享到腳本之家平臺供大家參考2017-07-07
Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個數(shù)
本篇文章主要介紹了Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個數(shù),我們可以通過TextWatcher去觀察輸入框中輸入的內(nèi)容,有興趣的可以了解一下。2017-04-04
Android?WindowManager深層理解view繪制實現(xiàn)流程
WindowManager是Android中一個重要的Service,是全局且唯一的。WindowManager繼承自ViewManager。WindowManager主要用來管理窗口的一些狀態(tài)、屬性、view增加、刪除、更新、窗口順序、消息收集和處理等2022-11-11
Android?RecyclerBarChart繪制使用教程
這篇文章主要為大家介紹了Android?RecyclerBarChart繪制使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android中 TeaScreenPopupWindow多類型篩選彈框功能的實例代碼
這篇文章主要介紹了Android TeaScreenPopupWindow多類型篩選彈框功能,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-06-06
Android高級界面組件之拖動條和評星條的功能實現(xiàn)
這篇文章主要介紹了Android高級界面組件之拖動條和評星條的實現(xiàn)實例,需要的的朋友參考下2017-03-03
Android面試Intent采用了什么設(shè)計模式解析
這篇文章主要為大家介紹了Android面試Intent采用了什么設(shè)計模式解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

