读书人

Android开发-用户定位服务-UserLocati

发布时间: 2013-01-28 11:49:56 作者: rapoo

Android开发--用户定位服务--UserLocation

public class MainActivity extends Activity {private Button button;private Button button2;private Button button3;private Button button4;private LocationManager locationManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=(Button)findViewById(R.id.button1);button2=(Button)findViewById(R.id.button2);button3=(Button)findViewById(R.id.button3);button4=(Button)findViewById(R.id.button4);button.setOnClickListener(new ButtonListener());button2.setOnClickListener(new ProviderButtonListener());button3.setOnClickListener(new BestProviderButtonListener());button4.setOnClickListener(new MyLocation());locationManager=(LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);}private class ButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//LocationManager locationManager=(LocationManager)MainActivity.this.//getSystemService(Context.LOCATION_SERVICE);/* * 各个参数的意义: * 1.定义当前所使用的Location Provider * 2.位置更新一次的最小时间间隔 * 3.位置更新的最小距离 * 4.绑定监听器--位置发生变化会调用其中的方法 */Log.d("BruceZhang", "Bond Success");//public void requestLocationUpdates (String provider, //long minTime, float minDistance, LocationListener listener) //Added in API level 1//Register for location updates using the named provider, and a pending intent. ////Parameters//provider  the name of the provider with which to register //minTime  minimum time interval between location updates, in milliseconds //minDistance  minimum distance between location updates, in meters //listener  a LocationListener whose onLocationChanged(Location) method will be called for each location update locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());}}private class ProviderButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubList<String> providers=locationManager.getAllProviders();for(Iterator<String> iterator=providers.iterator();iterator.hasNext();){String string=(String)iterator.next();Log.d("BruceZhang", string+"\n");}}}private class BestProviderButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubCriteria criteria=new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);criteria.setPowerRequirement(Criteria.POWER_LOW);criteria.setAltitudeRequired(false);criteria.setSpeedRequired(false);criteria.setCostAllowed(false);//第二个参数设置为false时,不管当前的那个provider是否可用,都需要进行查找,并根据条件设为最优String provider=locationManager.getBestProvider(criteria, false);Log.d("BruceZhang", "The best provider is:"+provider);}}private class MyLocation implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//对用用户定位服务主要是中间两个参数的设置locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 2000, new TestLocationListener());}}private class TestLocationListener implements LocationListener{  //这个函数的参数是用户当前的位置@Overridepublic void onLocationChanged(Location arg0) {// TODO Auto-generated method stub//Toast.makeText(MainActivity.this, "您当前的经度是:"+arg0.getLongitude()+" ,"+//"您当前的纬度是:"+arg0.getLatitude(),//Toast.LENGTH_SHORT).show();Log.d("BruceZhang", arg0.getLongitude()+"");Log.d("BruceZhang", arg0.getLatitude()+"");}@Overridepublic void onProviderDisabled(String arg0) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String arg0) {// TODO Auto-generated method stub}@Overridepublic void onStatusChanged(String arg0, int arg1, Bundle arg2) {// TODO Auto-generated method stub}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}



读书人网 >Android

热点推荐