Android 高手进阶教程:[13]Location 的使用

2024-11-17 01:45:35

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

2、修改main.xml代码如下:viewplaincopytoclipboardprint?<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/longitude"android:layout_width="fill_parent" android:layout_height="wrap_content"android:text="longitude:"/><TextViewandroid:id="@+id/latitude"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="latitude:"/></LinearLayout><?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/longitude"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="longitude:"/><TextViewandroid:id="@+id/latitude"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="latitude:"/></LinearLayout>

3、修改LocationDemo.java,代码如下:viewplaincopytoclipboardprint?packagecom.android.tutor;importandroid.app.Activity;importandroid.content.Context;importandroid.location.Location;importandroid.location.LocationManager;importandroid.os.Bundle;importandroid.widget.TextView;publicclassLocationDemoextendsActivity{privateTextViewlongitude;privateTextViewlatitude;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);longitude=(TextView)findViewById(R.id.longitude);latitude=(TextView)findViewById(R.id.latitude);LocationmLocation=getLocation(this);longitude.setText("Longitude:"+mLocation.getLongitude());latitude.setText("Latitude:"+mLocation.getLatitude());}//GettheLocationbyGPSorWIFIpublicLocationgetLocation(Contextcontext){LocationManagerlocMan=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);Locationlocation=locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);if(location==null){location=locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);}returnlocation;}}packagecom.android.tutor;importandroid.app.Activity;importandroid.content.Context;importandroid.location.Location;importandroid.location.LocationManager;importandroid.os.Bundle;importandroid.widget.TextView;publicclassLocationDemoextendsActivity{privateTextViewlongitude;privateTextViewlatitude;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);longitude=(TextView)findViewById(R.id.longitude);latitude=(TextView)findViewById(R.id.latitude);LocationmLocation=getLocation(this);longitude.setText("Longitude:"+mLocation.getLongitude());latitude.setText("Latitude:"+mLocation.getLatitude());}//GettheLocationbyGPSorWIFIpublicLocationgetLocation(Contextcontext){LocationManagerlocMan=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);Locationlocation=locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);if(location==null){location=locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);}returnlocation;}}

4、增加权限,修改AndroidManifest.xml代码如下(第16行为所增行):viewplaincopytoclipboardprint?<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.android.tutor"android:versionCode="1"android:versionName="1.0"><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><activityandroid:name=".LocationDemo"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application><uses-sdkandroid:minSdkVersion="7"/><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/></manifest><?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.android.tutor"android:versionCode="1"android:versionName="1.0">yl��ag�n+ �'+ :0; margin-top:0; " >privateTextViewlongitude;privateTextViewlatitude;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);longitude=(TextView)findViewById(R.id.longitude);latitude=(TextView)findViewById(R.id.latitude);<applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><activityandroid:name=".LocationDemo"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application><uses-sdkandroid:minSdkVersion="7"/><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/></manifest>-famiq�Tm�'+ @& man'; " >}returnlocation;}}

5、运行LocationDemo工程,所得效果如下(真机深圳测试):

Android 高手进阶教程:[13]Location 的使用
猜你喜欢