Copy Code code 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= "To obtain latitude and longitude degree:"
Android:textcolor= "#007979"/>
<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= "The best way to choose:"
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 Code code 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.getlongitude ();
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 ());
}
}
}
}
Permissions:
Copy Code code 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"/>