开发者

How to use LocationListener without "implements LocationListener"?

开发者 https://www.devze.com 2023-04-08 20:06 出处:网络
i finally reached an app that get\'s the GPS position of the user, but i reached it implementing LocationListener. it works fine, but i need to do it without implementing it, because i have to do a cl

i finally reached an app that get's the GPS position of the user, but i reached it implementing LocationListener. it works fine, but i need to do it without implementing it, because i have to do a class that doesn't implement methods.

I searched for a lot of tutorials and check a lot of websites and i try to transform my code to not implement LocationListener but i can't do it, every thing i tested broken my app and stop getting the GPS position of the user.

Please, if someone expert on this can transform my code for not be using "implements LocationListener" i'll be grated to him

this is the code to transform:

public class GpsMiniActivity extends Activity implements LocationListener{

private LocationManager mLocMgr;
private TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FrameLayout rl = new FrameLayout(this.getApplicationContext());
    LinearLayout ll= new LinearLayout(this.getApplicationContext());
    ll.setOrientation(LinearLayout.VERTICAL);

    setContentView(rl);
    rl.addView(ll);

    tv1=new TextView(getApplicationContext());
    ll.addView(tv1);

    //setContentView(R.layout.main);
    mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            500, 0, this);
}
@Override
public void onLocationChanged(Location location) {
    tv1.setText("Lat " +   location.getLatitude() + " Long " + location开发者_高级运维.getLongitude());
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}


public class GpsMiniActivity extends Activity {

private LocationManager mLocMgr;
private TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FrameLayout rl = new FrameLayout(this.getApplicationContext());
    LinearLayout ll= new LinearLayout(this.getApplicationContext());
    ll.setOrientation(LinearLayout.VERTICAL);

    setContentView(rl);
    rl.addView(ll);

    tv1=new TextView(getApplicationContext());
    ll.addView(tv1);

    //setContentView(R.layout.main);
    mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            500, 0, ll);
}
}
private LocationListener ll = new LocationListener(){
public void onLocationChanged(Location location) {
    tv1.setText("Lat " +   location.getLatitude() + " Long " + location.getLongitude());
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
}

There you go.


For that you will have to create a seperate LocationListener outside the onCreate() and give the reference of it to the LocationManager's requestLocationUpdates like this

 LocationListener mLocationListener = new LocationListener() {

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            @Override
            public void onProviderEnabled(String provider) {}
            @Override
            public void onProviderDisabled(String provider) {}
            @Override
            public void onLocationChanged(Location location) {
                tv1.setText("Lat " +   location.getLatitude() + " Long " + location.getLongitude());
            }
        };

And after that you will have to reference this LocationListener like this inside the onCreate()

mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                500, 0, mLocationListener);
0

精彩评论

暂无评论...
验证码 换一张
取 消