У меня четыре очень маленьких класса. Ниже приведены четыре класса. Мобильное представление не показывает мне ничего, кроме «Местоположение недоступно». И регистрация идет до "Location Finder Started". Больше ничего не срабатывает.
Основная Активность
public class CarbonTrackerMainActivity extends Activity {
private MainView mainView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView = new MainView(this);
locationFinder();
setContentView(mainView);
}
private void locationFinder(){
Log.d("Car","Location Finder Started !!");
LocationManager mlocManager = (LocationManager)getSystemService(this.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
}
Вид приложения
public class MainView extends View{
public MainView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas){
Paint background = new Paint();
background.setColor(Color.GRAY);
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
Paint text = new Paint();
text.setColor(Color.BLACK);
text.setTextSize(15);
canvas.drawText(DataHolder.getDataHolderObject().getLocationInTxt(), 10, 10, text);
invalidate();
}
}
Location Listener
public class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
loc.getLatitude();
loc.getLongitude();
DataHolder.getDataHolderObject().setLocation(loc);
Log.d("Car","Found one location!");
}
@Override
public void onProviderDisabled(String provider){
Log.d("Carbon","GPS Disabled!");
}
@Override
public void onProviderEnabled(String provider){
Log.d("Carbon","GPS Enabled!");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
Log.d("Carbon","GPS Status Changed!");
}
}
Синглтон для передачи переменных
public class DataHolder {
private static DataHolder dataHolderObject;
public Location location = null;
//======== Singleton Code =====================
private DataHolder(){}
public static DataHolder getDataHolderObject(){
if (dataHolderObject == null)
dataHolderObject = new DataHolder();
return dataHolderObject;
}
//======== End of singleton ===================
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public String getLocationInTxt() {
String text;
if (location==null)
return "Location Unavailable";
text = "My current location is:\n" + "Latitud = " + location.getLatitude() +"\nLongitud = " + location.getLongitude();
return text;
}
}