Location 在Android 开发中还是经常用到的,比如 通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开发).等。而在Android 中通过LocationManager 来获取Location .通常获取Location 有GPS 获取,WIFI 获取。



第一步:

创建一个Android 工程命名为LocationDemo .


第二步:修改main.xml 代码如下:
<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      >  <TextView       android:id="@+id/longitude"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="longitude:"      />  <TextView      android:id="@+id/latitude"        android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="latitude:"      />  </LinearLayout>


第三步:修改LocationDemo.java ,代码如下:
    package cn.caiwb.location;      import android.app.Activity;      import android.content.Context;      import android.location.Location;      import android.location.LocationManager;      import android.os.Bundle;      import android.widget.TextView;      public class LocationDemo extends Activity {                   private TextView longitude;          private TextView latitude;          @Override          public void onCreate(Bundle savedInstanceState) {              super.onCreate(savedInstanceState);              setContentView(R.layout.main);                            longitude = (TextView)findViewById(R.id.longitude);              latitude = (TextView)findViewById(R.id.latitude);                            Location mLocation = getLocation(this);                            longitude.setText("Longitude: " + mLocation.getLongitude());              latitude.setText("Latitude: " + mLocation.getLatitude());          }                    //Get the Location by GPS or WIFI          public Location getLocation(Context context) {              LocationManager locMan = (LocationManager) context                      .getSystemService(Context.LOCATION_SERVICE);              Location location = locMan                      .getLastKnownLocation(LocationManager.GPS_PROVIDER);              if (location == null) {                  location = locMan                          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);              }              return location;          }      }  


第四步:增加权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>






更多相关文章

  1. Android Studio中同步代码时报:Received status code 400 from se
  2. android java代码覆盖率
  3. Android对话框风格的Activity代码
  4. Android 删除指定文件代码
  5. Android摄像头视频帧数据测试代码
  6. 代码实现Layout android:layout_alignParentRight
  7. Android 震动 和 停止 代码
  8. android ril 代码逻辑分析
  9. 用Android代码实现自动打开USB调试

随机推荐

  1. Android CoordinatorLayout + AppBarLayo
  2. android中的左右滑动
  3. android 界面更新之runOnUiThread()
  4. 学习Android Studio开发工具之Activity3(
  5. Mono For Android(安卓)重要的类文件
  6. Android数据存储(一)
  7. Android—— 4.2 Vold挂载管理_NetlinkMa
  8. Android C++层内存泄漏检测
  9. NDK开发笔记——Android使用openssl
  10. Android基础 : Android ContentProvider