我是新手 自学 改代码按照视频里写的 视频里代码没问题 到我这就有问题:locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,100,this); 这里的this 有问题 换成什么参数都有问题 写成this 的话 整行代码都有红线!
请大神帮忙!
package fragment;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.luao2.jiemianzhuantiao.R;
import com.lidroid.xutils.ViewUtils;
import org.xutils.view.annotation.ViewInject;
import java.io.IOException;
import java.util.List;
import luao2.utils.SharedUtils;
/**
* Created by Administrator on 2017/4/4.
*/
public class FragmentHome extends Fragment implements LocationListener{
@ViewInject(R.id.index_top_city)
private TextView topCity;
private String cityName; //当前城市名称
private LocationManager locationManager;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_index, null);
ViewUtils.inject(this,view);
//获取数据并显示
return view;
}
@Override
public void onStart() {
super.onStart();
//检查当前的GPS模块
checkGPSIsOpen();
}
private void checkGPSIsOpen(){
//获取当前的LocationManager对象
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
boolean isOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isOpen){
//进入GPS的设置页面
Intent intent = new Intent();
intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent,0);
}
//开始定位
startLocation();
}
//使用GPS定位的方法
private void startLocation(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,100,this);
}
//接收并且处理消息
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == 1){
topCity.setText(cityName);
}
return false;
}
});
//获取对应位置的经纬度并且定位城市
private void updateWithNewLocation(Location location){
double lat = 0.0,lng = 0.0;
if (location !=null){
lat = location.getLatitude();
lng = location.getLongitude();
Log.i("TAG","经度是"+lat+"纬度是:"+lng);
}else {
cityName = "无法获取城市信息";
}
//通过经纬度获取地址,由于地址会有多个,这个和经纬度精确度有关,本实例中定义了最大的返回数2,即在集合对象中有两个值
List<Address> list = null;
Geocoder ge = new Geocoder(getActivity());
try {
list = ge.getFromLocation(lat,lng,2);
} catch (IOException e) {
e.printStackTrace();
}
if (list !=null && list.size()>0){
for (int i = 0; i < list.size(); i++){
Address ad = list.get(i);
cityName = ad.getLocality();//获取城市
}
}
//发送一条空消息
handler.sendEmptyMessage(1);
}
//位置信息更改执行方法
@Override
public void onLocationChanged(Location location) {
//更新当前的位置信息
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onDestroy() {
super.onDestroy();
//保存城市
//停止定位
stopLocation();
}
//停止定位
private void stopLocation(){
locationManager.removeUpdates(this);
}
}