import java.util.List;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button bt;
private TextView tv_coordinate;
private TextView tv_city;
private String provider;
private LocationManager locationManager;
private LocationClient locationClient = null;
private LocationClientOption option;
// private BDLocationListener bdLocationListener;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
tv_coordinate = (TextView)findViewById(R.id.textView1);
tv_city = (TextView)findViewById(R.id.textView2);
bt = (Button)findViewById(R.id.button1);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
bt.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});
//坐标
List<String> providerList = locationManager.getProviders(true);
if(providerList.contains(LocationManager.GPS_PROVIDER))
{
provider = LocationManager.GPS_PROVIDER;
}else if(providerList.contains(LocationManager.NETWORK_PROVIDER))
{
provider = LocationManager.NETWORK_PROVIDER;
}else
{
Toast.makeText(MainActivity.this, "No Location provider",
Toast.LENGTH_SHORT).show();
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if(location != null)
{
showLocation(location);
}
locationManager.requestLocationUpdates(provider, 5000,
10, locationListener);
//城市名
locationClient = new LocationClient(getApplicationContext());
locationClient.registerLocationListener(bdLocationListener);
option.setCoorType("all");
option.setOpenGps(true);
BDLocation bdLocation = locationClient.getLastKnownLocation();
if(bdLocation != null)
{
showBDlocation(bdLocation);
}
locationClient.registerNotifyLocationListener(bdLocationListener);
locationClient.requestLocation();
locationClient.requestNotifyLocation();
}
protected void onDestroy()
{
super.onDestroy();
if(locationManager != null)
{
locationManager.removeUpdates(locationListener);
}
}
LocationListener locationListener = new LocationListener()
{
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
public void onProviderEnabled(String provider)
{
}
public void onProviderDisabled(String provider)
{
}
public void onLocationChanged(Location location)
{
showLocation(location);
}
};
public void showLocation(Location location)
{
String coordinate = "纬度:" + location.getLatitude() + "\n" + "经度:" + location.getLongitude();
tv_coordinate.setText(coordinate);
}
BDLocationListener bdLocationListener = new BDLocationListener()
{
public void onReceiveLocation(BDLocation bdlocation)
{
showBDlocation(bdlocation);
}
};
public void showBDlocation(BDLocation bdLocation)
{
String city = bdLocation.getCity();
tv_city.setText(city);
}
}
运行报错java.lang.NoClassDefFoundError: com.zk.dddd.MainActivity$2
求帮助解决错误
想知道怎么能定位出当前城市