当我使用 Eclipse DDMS 测试应用程序时 - 它可以工作,调用 onlocationchanged 但是当我在平板电脑上安装应用程序时 - 没有
我的来源
public class Fragment_1 extends Fragment{
final String LOG_TAG = "myLogs";
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
private String bestProvider;
private Context contextActivity;
private LocationManager locationManager;
private MyLocationListener locationListener;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
contextActivity = getActivity();
locationManager = (LocationManager) contextActivity.getSystemService(contextActivity.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
bestProvider = locationManager.getBestProvider(crit, false);
locationListener = MyLocationListener.createMyLocationListener(this);
locationManager.requestLocationUpdates(
bestProvider,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
locationListener);
}
和我的班级 MyLocationListener
ublic class MyLocationListener implements LocationListener {
private Fragment_1 fragment;
private static MyLocationListener myLocationListener;
public MyLocationListener(Fragment_1 fragment_1) {
// TODO Auto-generated constructor stub
fragment = fragment_1;
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(fragment.getActivity() !=null) {
TextView textViev = (TextView)fragment.getActivity().findViewById(R.id.textView1);
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
textViev.setText(message);}
}
public static MyLocationListener createMyLocationListener (Fragment_1 fragment_1) {
if (myLocationListener == null) {
myLocationListener = new MyLocationListener(fragment_1);
} else {
myLocationListener.setFragment(fragment_1);
}
return myLocationListener;
}
private void setFragment (Fragment_1 fragment){
this.fragment = fragment;
}
}
和清单中的许可
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />