-1

I know it is a common question and has been answered a number of times... But I am looking for an answer to a specific problem. I have written a method which is required to return current lat/long as string. The code goes like this:

public class LibraryProvider {
    public String basicMethod(String string) {
        String text = string;
        LocationManager locationManager;
        String provider;
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            text = "Provider " + provider + " has been selected.";
            int lat = (int) (location.getLatitude());
            int lng = (int) (location.getLongitude());
            text = text + "\n" + "lat: " + String.valueOf(lat);
            text = text + "\n" + "lng: " + String.valueOf(lng);
        } else {
            text = "Location not available";
        }

        return text;
    }
}

However, the android studio is not allowing this.get System Service:

cannot resolve Method getSystemService(java.lang.String)

I am new to android and not clear about context and intents... I think the problem has something to do with it.

(edited) the code i was using to load the above class is as under

private final class ServiceHandler extends android.os.Handler {

    public ServiceHandler(Looper looper){
        super(looper);
    }

    public void handleMessage(Message msg){
        dynamicClassLoader();
    }

    String text;

    private void dynamicClassLoader(){

        final String apkFile =Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/apkFile.apk";
        String className = "com.va.android.level2.lib.LibraryProvider";
        final File optimisedDexOutputPath = getDir("outdex", 0);
        DexClassLoader classLoader = new DexClassLoader(apkFile,optimisedDexOutputPath.getAbsolutePath(),null,getClassLoader());
        try{
            Class loadedClass = classLoader.loadClass(className);
          //  LibraryInterface lib = (LibraryInterface) loadedClass.newInstance();
          //  lib.basicMethod("hello");
            Object obj =(Object)loadedClass.newInstance();
            Method method = loadedClass.getMethod("basicMethod", String.class);
            text = (String) method.invoke(obj,"added method");
            showOutput(text);

        } catch (Exception e){
            e.printStackTrace();
        }

    }

    private void showOutput(final String str){
        mServiceHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MyService.this,str, Toast.LENGTH_LONG).show();
            }
        });
    }
}

earlier it was working, but now it is raising some exception at loadedClass.newInstance(); .....i think i need to pass the context ...but how??

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
anant sophia
  • 55
  • 2
  • 12

4 Answers4

1

Try get Activity Context reference in custom class constructor and use it ;

public class LibraryProvider {
  private Context context;
  public LibraryProvider(Context context){
      this.context=context;
  }
}

locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

You need a Context to call getSystemService(...). Actually you are calling it from "this" that is not a class that has a Context.

marco
  • 1,686
  • 1
  • 25
  • 33
0

try this

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
              Log.d("DEBUG",location.toString());
              double lat = location.getLatitude();
              double lon = location.getLongitude();
            }
        };
Partha Bisoi
  • 173
  • 2
  • 15
0

try this

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

see this link on how to get current location in Android

ivan.mylyanyk
  • 2,051
  • 4
  • 30
  • 37