如何在Android中获取当前位置
我现在使用的密码:
这是我的主要活动
package com.example.locationtests;import android.os.Bundle;import android.app.Activity;import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GPSTracker mGPS = new GPSTracker(this); TextView text = (TextView) findViewById(R.id.texts); if(mGPS.canGetLocation ){ mGPS.getLocation(); text.setText("Lat"+mGPS.getLatitude()+"Lon"+mGPS.getLongitude()); }else{ text.setText("Unabletofind"); System.out.println("Unable"); }}@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}}
这是我用来跟踪的类:
package com.example.locationtests;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface; import android.content.Intent;import android.location.Location;import android.location.LocationListener; import android.location.LocationManager;import android.os.Bundle;import android.provider.Settings; import android.util.Log;public final class GPSTracker implements LocationListener { private final Context mContext; // flag for GPS status public boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS status boolean canGetLocation = false;
郎朗坤
桃花长相依
莫回无
相关分类