215 Stimmen

Wie kann ich getSystemService in einer Nicht-Aktivitätsklasse (LocationManager) verwenden?

Ich habe Probleme mit der Auslagerung von Aufgaben aus der Hauptaktivitäten OnCreate-Methode auf eine andere Klasse, um die schwere Arbeit zu tun.

Wenn ich versuche, getSystemService von der Nicht-Activity-Klasse aufzurufen, wird eine Ausnahme ausgelöst.

Jede Hilfe wäre sehr willkommen :)

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;
    }
}

311voto

Labeeb Panampullan Punkte 33641

Sie müssen den Kontext an die Klasse fyl übergeben.
Eine Lösung besteht darin, einen Konstruktor wie diesen für Ihre fyl Klasse:

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

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

       --
 }
}

Erstellen Sie also in Ihrer Aktivitätsklasse das Objekt von fyl in onCreate Funktion wie folgt:

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);
    }
}

54voto

Maddy Punkte 4672

Sie können diese Option wählen:

getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

17voto

brenjt Punkte 15551

Eine Möglichkeit, dies zu umgehen, besteht darin, eine statische Klasse für Instanzen zu erstellen. Ich habe es eine Menge in AS3 verwendet ich hat für mich in Android-Entwicklung zu groß gearbeitet.

Config.java

public final class Config {
    public static MyApp context = null;
}

MyApp.java

public class MyApp extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Config.context = this;
    }
    ...
}

Sie können dann auf den Kontext zugreifen, indem Sie Config.context

LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = Config.context.getSystemService(context);

2voto

auspicious99 Punkte 3300

Für einige Nicht-Aktivitätsklassen, wie Arbeiter wird Ihnen im öffentlichen Konstruktor bereits ein Context-Objekt übergeben.

Worker(Context context, WorkerParameters workerParams)

Sie können das einfach verwenden, z.B. indem Sie es in einer privaten Context-Variablen in der Klasse speichern (z.B., mContext ), und dann zum Beispiel

mContext.getSystenService(Context.ACTIVITY_SERVICE)

1voto

dimvolk Punkte 308

Verwenden Sie diese in der Aktivität:

private Context context = this;

........
if(Utils.isInternetAvailable(context){
Utils.showToast(context, "toast");
}
..........

in Utils:

public class Utils {

    public static boolean isInternetAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
    }

}

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X