0
votes

When methods from the location manager class of Android API is called, The following warnings keep showing up. I am using the latest Android Studio 2.1 and the target OS is Android 4.0.3 or above. So what's wrong with that? Thanks in advance!

  • I/dalvikvm: Could not find method android.content.Context.getSystemService, referenced from method .MainActivity$GPSService.access$super

  • W/dalvikvm: VFY: unable to resolve virtual method 532: Landroid/content/Context;.getSystemService
    (Ljava/lang/Class;)Ljava/lang/Object;

  • D/dalvikvm: VFY: replacing opcode 0x6f at 0x0035 I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method

  • W/dalvikvm: VFY: unable to resolve virtual method 514: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;

  • D/dalvikvm: VFY: replacing opcode 0x6f at 0x004f I/dalvikvm: Could not find method android.content.ContextWrapper.getCodeCacheDir, referenced from method

  • W/dalvikvm: VFY: unable to resolve virtual method 585: Landroid/content/ContextWrapper;.getCodeCacheDir ()Ljava/io/File;

  • D/dalvikvm: VFY: replacing opcode 0x6f at 0x00e1

  • I/dalvikvm: Could not find method android.content.ContextWrapper.getNoBackupFilesDir, referenced from method

  • W/dalvikvm: VFY: unable to resolve virtual method 597: Landroid/content/ContextWrapper;.getNoBackupFilesDir ()Ljava/io/File;

  • D/dalvikvm: VFY: replacing opcode 0x6f at 0x01f4

  • I/dalvikvm: Could not find method android.content.Context.getDrawable, referenced from method

  • W/dalvikvm: VFY: unable to resolve virtual method 516: Landroid/content/Context;.getDrawable (I)Landroid/graphics/drawable/Drawable;

  • D/dalvikvm: VFY: replacing opcode 0x6f at 0x0317

  • I/dalvikvm: Could not find method android.content.ContextWrapper.getSystemServiceName, referenced from method

  • W/dalvikvm: VFY: unable to resolve virtual method 607: Landroid/content/ContextWrapper;.getSystemServiceName (Ljava/lang/Class;)Ljava/lang/String;

  • D/dalvikvm: VFY: replacing opcode 0x6f at 0x03c6

  • I/dalvikvm: Could not find method android.content.ContextWrapper.getExternalMediaDirs, referenced from method

  • W/dalvikvm: VFY: unable to resolve virtual method 593: Landroid/content/ContextWrapper;.getExternalMediaDirs ()[Ljava/io/File;

  • D/dalvikvm: VFY: replacing opcode 0x6f at 0x040b I/dalvikvm: Could not find method android.content.Context.getColor, referenced from method

  • W/dalvikvm: VFY: unable to resolve virtual method 513: Landroid/content/Context;.getColor (I)I

  • D/dalvikvm: VFY: replacing opcode 0x6f at 0x0534

  • I/dalvikvm: Could not find method android.content.ContextWrapper.checkSelfPermission, referenced from method

  • W/dalvikvm: VFY: unable to resolve virtual method 561: Landroid/content/ContextWrapper;.checkSelfPermission (Ljava/lang/String;)I

  • D/dalvikvm: VFY: replacing opcode 0x6f at 0x06d2


public static class GPSService extends Service implements android.location.LocationListener{

    MainActivity ma = new MainActivity();

    private ScheduledExecutorService scheduleTaskExecutor;
    private ScheduledFuture scheduledFuture=null;

    private Context sContext = GPSService.this;

    private String deviceId;
    //= loadStr(getContext(), "deviceId");

    final static String TAG = "GPSService";

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude


    // Declaring a Location Manager
    protected LocationManager locationManager;


    public GPSService() {
        super();
    }


    public Context getContext() {
        return sContext;
    }


    public Location getLocation() {
        try {




            /*
            locationManager = (LocationManager) sContext
                    .getSystemService(LOCATION_SERVICE);*/

            // getting GPS status

            /*
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;

                // First get location from Network Provider
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            2000,
                            10, this);
                    Log.d("Network", "Network");

                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                2000,
                                10, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }else {
                    showSettingsAlert();
                }
            }*/

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

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

        return location;
    }

    public void stopUsingGPS(){

        try{
            if(locationManager != null){
                locationManager.removeUpdates(GPSService.this);
            }
        }catch (SecurityException e){
            e.printStackTrace();
        }
    }

    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }

        return latitude;
    }

    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }
        return longitude;
    }


    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(sContext);

        alertDialog.setTitle("GPS");

        alertDialog.setMessage("GPS setting");

        alertDialog.setPositiveButton("setting", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                sContext.startActivity(intent);
            }
        });

        alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alertDialog.show();
    }

    //--------------------------------------------------------------

    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onLocationChanged(Location location) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    //------------------------------------------------------------

    @Override
    public void onCreate() {

        super.onCreate();
        Log.d("service","on create");
        //getLocation();

        try {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 2000, 10, this);

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

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.d("service","on start cmd");


        Bundle extras = intent.getExtras();
        if(extras == null){
            this.deviceId=null;
            Log.d("Service passed id","null");}
        else
        {
            this.deviceId = (String) extras.get("deviceId");
            Log.d("passed id",deviceId);
        }

        scheduleTaskExecutor= Executors.newScheduledThreadPool(5);
        scheduledFuture = scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
            public void run() {

                Log.d("inside on Start Cmd","running thread");

                try{
                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                } catch(SecurityException e){
                    e.printStackTrace();
                }


            }
            }, 0, 10, TimeUnit.SECONDS);


        return START_NOT_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (scheduledFuture!=null){
            scheduledFuture.cancel(false);
            Log.d("service" , "on destroy scheduled future");
        }
        if (locationManager!=null)
            stopUsingGPS();

        Log.d("service" , "on destroy");

    }

    }
1
can you pls format the err as wellrakesh kashyap

1 Answers

0
votes

Sorry, finally I got it myself. It was due to my stupid mistakes. The location manager methods should have been grouped tgt as the following.

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 2000, 10, this);
            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);