Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "# FFD3D7DF"
Android: orientation = "vertical">
<LinearLayout
Android: id = "@ + id/location"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginBottom = "20dip"
Android: layout_marginLeft = "20dip"
Android: layout_marginRight = "20dip"
Android: layout_marginTop = "20dip"
Android: background = "@ drawable/bg_frame"
Android: gravity = "center_vertical"
Android: orientation = "vertical"
Android: paddingBottom = "2dip"
Android: paddingLeft = "10dip"
Android: paddingRight = "10dip"
Android: paddingTop = "10dip">
<TextView
Android: id = "@ + id/providerTitle"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "get latitude and longitude :"
Android: textColor = "#007979" type = "codeph" text = "/codeph"/>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
<RadioGroup
Android: id = "@ + id/providerGroup"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
<RadioButton
Android: id = "@ + id/gpsProvide"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "gps"
Android: textColor = "#005AB5"/>
<RadioButton
Android: id = "@ + id/networkProvide"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "base station"
Android: textColor = "#005AB5"/>
</RadioGroup>
<ImageButton
Android: id = "@ + id/bestLocationProId"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/loction"/>
</LinearLayout>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "best choice :"
Android: textColor = "#005AB5"/>
<TextView
Android: id = "@ + id/locationProId"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textColor = "#8F4586"/>
</LinearLayout>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "longitude :"
Android: textColor = "#005AB5"/>
<EditText
Android: id = "@ + id/latEditTextId"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: enabled = "false"
Android: textColor = "#8F4586"/>
</LinearLayout>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "latitude :"
Android: textColor = "#005AB5"/>
<EditText
Android: id = "@ + id/lonEditTextId"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: enabled = "false"
Android: textColor = "#8F4586"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<--- Activity->Copy codeThe Code is as follows: package com. talkweb. mobileapp;
Import java. text. DecimalFormat;
Import android. app. Activity;
Import android. content. Context;
Import android. location. Criteria;
Import android. location. Location;
Import android. location. LocationListener;
Import android. location. LocationManager;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. EditText;
Import android. widget. ImageButton;
Import android. widget. RadioButton;
Import android. widget. RadioGroup;
Import android. widget. TextView;
Import android. widget. RadioGroup. OnCheckedChangeListener;
/**
*
* @ Author Mr. Z
* @ Time 2012-5-16
*
*/
Public class LocationappActivity extends Activity {
Private ImageButton btnGetBestLocationPro;
Private EditText txtLat;
Private EditText txtLon;
Private TextView txtLocationPro;
Private LocationManager locationManager;
Private DecimalFormat format;
Private String provider;
Private RadioGroup providerGroup;
Private RadioButton radGps;
Private RadioButton radNetwork;
Private String latStr;
Private String lonStr;
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
This. setContentView (R. layout. main );
BtnGetBestLocationPro = (ImageButton) findViewById (R. id. bestLocationProId );
BtnGetBestLocationPro. setOnClickListener (new GetBestLocationProListener ());
TxtLat = (EditText) findViewById (R. id. latEditTextId );
TxtLon = (EditText) findViewById (R. id. lonEditTextId );
TxtLocationPro = (TextView) findViewById (R. id. locationProId );
Format = new DecimalFormat ("#. 000000 ");
LocationManager = (LocationManager) LocationappActivity. this. getSystemService (Context. LOCATION_SERVICE );
Criteria criteria = new Criteria ();
Criteria. setAccuracy (Criteria. ACCURACY_FINE );
Criteria. setPowerRequirement (Criteria. POWER_LOW );
Criteria. setAltitudeRequired (false );
Criteria. setSpeedRequired (false );
Criteria. setCostAllowed (false );
Provider = locationManager. getBestProvider (criteria, false );
TxtLocationPro. setText (provider );
LocationManager. requestLocationUpdates (LocationManager. GPS_PROVIDER, 0, 0, new TestLocationListener ());
ProviderGroup = (RadioGroup) findViewById (R. id. providerGroup );
RadGps = (RadioButton) findViewById (R. id. gpsProvide );
RadNetwork = (RadioButton) findViewById (R. id. networkProvide );
ProviderGroup. setOnCheckedChangeListener (new LocationProvideCheckedlistener ());
If (provider. equals (LocationManager. GPS_PROVIDER )){
System. out. println ("gps ");
RadGps. setSelected (true );
RadGps. setChecked (true );
} Else if (provider. equals (LocationManager. NETWORK_PROVIDER )){
System. out. println ("network ");
RadNetwork. setSelected (true );
RadNetwork. setChecked (true );
}
}
Private class GetBestLocationProListener implements OnClickListener {
@ Override
Public void onClick (View v ){
Criteria criteria = new Criteria ();
Criteria. setAccuracy (Criteria. ACCURACY_FINE );
Criteria. setPowerRequirement (Criteria. POWER_LOW );
Criteria. setAltitudeRequired (false );
Criteria. setSpeedRequired (false );
Criteria. setCostAllowed (false );
String provider = locationManager. getBestProvider (criteria, false );
TxtLocationPro. setText (provider );
LocationManager. requestLocationUpdates (LocationManager. NETWORK_PROVIDER, 0, 0, new TestLocationListener ());
}
}
Private class TestLocationListener implements LocationListener {
@ Override
Public void onLocationChanged (Location location ){
Double lat = location. getLatitude ();
Double lon = location. getlongdistance ();
LatStr = format. format (lat );
LonStr = format. format (lon );
TxtLat. setText (latStr );
TxtLon. setText (lonStr );
}
@ Override
Public void onProviderDisabled (String provider ){
}
@ Override
Public void onProviderEnabled (String provider ){
}
@ Override
Public void onStatusChanged (String provider, int status, Bundle extras ){
}
}
Private class LocationProvideCheckedlistener implements OnCheckedChangeListener {
@ Override
Public void onCheckedChanged (RadioGroup group, int checkedId ){
If (checkedId = radGps. getId ()){
LocationManager. requestLocationUpdates (LocationManager. GPS_PROVIDER, 0, 0, new TestLocationListener ());
} Else if (checkedId = radNetwork. getId ()){
LocationManager. requestLocationUpdates (LocationManager. NETWORK_PROVIDER, 0, 0, new TestLocationListener ());
}
}
}
}
Permission:Copy codeThe Code is as follows: <uses-permission android: name = "android. permission. CAMERA"/>
<Uses-permission android: name = "android. permission. VIBRATE"/>
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
<Uses-permission android: name = "android. permission. ACCESS_LOCATION_EXTRA_COMMANDS"/>
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>
<Uses-permission android: name = "android. permission. RECORD_AUDIO"/>
<Uses-permission android: name = "android. permission. MODIFY_AUDIO_SETTINGS"/>
<Uses-permission android: name = "android. permission. READ_CONTACTS"/>
<Uses-permission android: name = "android. permission. WRITE_CONTACTS"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. CHANGE_CONFIGURATION"/>
<Uses-permission android: name = "android. permission. WAKE_LOCK"/>
<Uses-permission android: name = "android. permission. BLUETOOTH"/>