Question

I am new to Android development. I am developing an app that upon receiving an SMS message with a unique string of text, the GPS is enabled and starts tracking the location of the phone. The issue I'm having is with the getSystemService() method. I receive the error "The method getSystemService(String) is undefined for the type SmsReceiver", I believe this is because it does not have a context. I have tried to add in a context within my code with 'ctx', and that removes the error but my application crashes every time I run it on my phone. The code for receiving the SMS works fine, and the GPS location tracking code works fine if it's in my main class.

I still don't fully understand contexts, can anyone help me out please?

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class SmsReceiver extends BroadcastReceiver {
    LocationManager lm;
    LocationListener loc;
    Context ctx;
    public SmsReceiver(Context c) { ctx = c; } 

    @Override
    public void onReceive(Context context, Intent intent) {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null){
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                                    
                str += msgs[i].getMessageBody().toString() + "\n";        
            }

            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            if (msgs[0].getMessageBody().toString() == "Track"){
                enableGPS();
            }
        }                         
    }

    public void enableGPS() {  
        lm = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
        loc = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
    }

    public void disableGPS() {
        lm.removeUpdates(loc);
    }

    private class mylocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            String s = "";
            s += "\tTime: "        + location.getTime() + "\n";
            s += "\tLatitude:  " + location.getLatitude()  + "°\n";
            s += "\tLongitude: " + location.getLongitude() + "°\n";
            s += "\tAccuracy:  " + location.getAccuracy()  + " metres\n";
            s += "\tAltitude:  " + location.getAltitude()  + " metres\n";

            //Toast.makeText(SmsReceiver.this, s, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderDisabled(String arg0) { }
        @Override
        public void onProviderEnabled(String arg0) { }
        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) { }
    }
}
Was it helpful?

Solution

you can pass Context as argument to enableGPS and use it to call getSystemService

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top