SORU
2 ŞUBAT 2011, ÇARŞAMBA


Nasıl olmayan bir etkinlik sınıfta getSystemService (LocationManager) kullanabilir miyim?

Başka bir sınıf ağır işleri yapmak üzerine sorun ana Faaliyetleri OnCreate metodu boşaltma görevleri yaşıyorum.

Non-Activity sınıfından getSystemService aramak için çalıştığınızda, bir özel durum atılır.

Herhangi bir yardım büyük mutluluk :)

lmt.java:

package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.Location;

public class lmt extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        fyl lfyl = new fyl();
        Location location = lfyl.getLocation();
        String latLongString = lfyl.updateWithNewLocation(location);

        TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
        myLocationText.setText("Your current position is:\n"   latLongString);
    }
}

fyl.java

package com.atClass.lmt;

import android.app.Activity;
import android.os.Bundle;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;

public class fyl {
    public Location getLocation(){
        LocationManager locationManager;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(context);

        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);

        return location;
    }

    public String updateWithNewLocation(Location location) {
        String latLongString;

        if (location != null){
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:"   lat   "\nLong:"   lng;
        }else{
            latLongString = "No Location";
        }

        return latLongString;
    }
}

CEVAP
2 ŞUBAT 2011, ÇARŞAMBA


Bilgin olsun sınıf için.. içeriğiniz geçmesi gerekir
Bir çözüm fyl sınıf için böyle bir yapıcı olun

public class fyl {
 Context mContext;
 public fyl(Context mContext) {
       this.mContext = mContext;
 }

 public Location getLocation() {
       --
       locationManager = (LocationManager)mContext.getSystemService(context);

       --
 }
}

Faaliyet sınıfı, bu gibi onCreate fonksiyon bilgin olsun nesnesi oluşturun:

package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.Location;

public class lmt extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        fyl lfyl = new fyl(this); //Here the context is passing 

        Location location = lfyl.getLocation();
        String latLongString = lfyl.updateWithNewLocation(location);

        TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
        myLocationText.setText("Your current position is:\n"   latLongString);
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Dylan Dubay

    Dylan Dubay

    10 Temmuz 2013
  • Perihelion

    Perihelion

    23 NİSAN 2008
  • Drakinen

    Drakinen

    1 EYLÜL 2008