91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android如何獲取經緯度

發布時間:2020-10-20 20:59:09 來源:腳本之家 閱讀:117 作者:花落誰家2091572217 欄目:移動開發

android 定位的兩種方式:GPS_PROVIDER and NETWORK_PROVIDER

定位的可以借助LocationManager來實現

MainActivity代碼

static final String TAG = "MainActivity";
 
 
 private TextView locationTV;
 private LocationManager locationManager;
 private String provider;
 
 ArrayList<ContactModel> dataList = new ArrayList<ContactModel>();
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
//  initView();
 
 
  locationTV = (TextView) findViewById(R.id.locaiton_tv);
 
  locationManager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
  // 獲取所有可用的位置提供器
  List<String> providerList = locationManager.getProviders(true);
  if (providerList.contains(LocationManager.GPS_PROVIDER)) {
   provider = LocationManager.GPS_PROVIDER;
  } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
   provider = LocationManager.NETWORK_PROVIDER;
  } else {
   // 當沒有可用的位置提供器時,彈出Toast提示用戶
   Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show();
   return;
  }
  Location location = locationManager.getLastKnownLocation(provider);
  if (location != null) {
   // 顯示當前設備的位置信息
   showLocation(location);
  }
  if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
   // TODO: Consider calling
   // ActivityCompat#requestPermissions
   // here to request the missing permissions, and then overriding
   // public void onRequestPermissionsResult(int requestCode, String[] permissions,
   //           int[] grantResults)
   // to handle the case where the user grants the permission. See the documentation
   // for ActivityCompat#requestPermissions for more details.
   return;
  }
  locationManager.requestLocationUpdates(provider, 5000, 1, locationListener); 
 }
 private void showLocation(Location location) {
  String currentPosition = "latitude is " + location.getLatitude() + "\n"+ "longitude is " + location.getLongitude();
  locationTV.setText(currentPosition);
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
 
  if (locationManager != null) {
 
   if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    // ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    // public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //           int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;
   }
 
 
   // 關閉程序時將監聽器移除
   locationManager.removeUpdates(locationListener);
  }
 }
 
 LocationListener locationListener = new LocationListener() {
  @Override
  public void onLocationChanged(Location location) {
 
   Toast.makeText(MainActivity.this,"onLocationChanged",Toast.LENGTH_SHORT).show();
  }
 
  @Override
  public void onStatusChanged(String s, int i, Bundle bundle) {
   Toast.makeText(MainActivity.this,"onStatusChanged",Toast.LENGTH_SHORT).show();
  }
 
  @Override
  public void onProviderEnabled(String s) {
   Toast.makeText(MainActivity.this,"onProviderEnabled",Toast.LENGTH_SHORT).show();
  }
 
  @Override
  public void onProviderDisabled(String s) {
   Toast.makeText(MainActivity.this,"onProviderDisabled",Toast.LENGTH_SHORT).show();
  }
 };

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

北安市| 招远市| 阿拉善盟| 怀来县| 托克逊县| 治多县| 松江区| 斗六市| 宾川县| 涞水县| 阿尔山市| 洛宁县| 拜城县| 象山县| 开封县| 永平县| 威远县| 曲麻莱县| 山阴县| 青浦区| 二连浩特市| 呼玛县| 鹤岗市| 历史| 北流市| 永善县| 广丰县| 锡林浩特市| 方山县| 仁布县| 阿拉善右旗| 棋牌| 孟连| 西吉县| 汶上县| 碌曲县| 惠州市| 奉节县| 荣成市| 新平| 城市|