Method undefined for type SmsReciever, context problem

Put your problem here if it does not fit any of the other categories.

Method undefined for type SmsReciever, context problem

Postby bobby123 » Sat Nov 13, 2010 10:35 am

Hi, 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 with in my code with 'ctx', and that removes the error but my application crashes every time I run it on my phone. The code for recieving 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?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.content.BroadcastReceiver;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.location.Location;
  5. import android.location.LocationListener;
  6. import android.location.LocationManager;
  7. import android.os.Bundle;
  8. import android.telephony.gsm.SmsMessage;
  9. import android.widget.Toast;
  10.  
  11. @SuppressWarnings("deprecation")
  12. public class SmsReceiver extends BroadcastReceiver
  13. {
  14.            LocationManager lm;
  15.            LocationListener loc;
  16.            Context ctx;
  17.            public SmsReceiver(Context c) { ctx = c; }
  18.        
  19.     @Override
  20.     public void onReceive(Context context, Intent intent)
  21.     {
  22.         //---get the SMS message passed in---
  23.         Bundle bundle = intent.getExtras();        
  24.         SmsMessage[] msgs = null;
  25.         String str = "";            
  26.         if (bundle != null)
  27.         {
  28.             //---retrieve the SMS message received---
  29.             Object[] pdus = (Object[]) bundle.get("pdus");
  30.             msgs = new SmsMessage[pdus.length];            
  31.             for (int i=0; i<msgs.length; i++){
  32.                 msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                                    
  33.                 str += msgs[i].getMessageBody().toString() + "\n";        
  34.             }
  35.            
  36.             Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
  37.             if (msgs[0].getMessageBody().toString() == "Track")
  38.             {
  39.                 enableGPS();
  40.             }
  41.         }                        
  42.     }
  43.         public void enableGPS()
  44.         {              
  45.                         lm = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
  46.                         loc = new mylocationlistener();
  47.                         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
  48.         }
  49.         public void disableGPS()
  50.         {
  51.                 lm.removeUpdates(loc);
  52.         }
  53.  
  54.         private class mylocationlistener implements LocationListener {
  55.             @Override
  56.             public void onLocationChanged(Location location) {
  57.                 String s = "";
  58.                 s += "\tTime: "        + location.getTime() + "\n";
  59.                 s += "\tLatitude:  " + location.getLatitude()  + \n";
  60.                 s += "\tLongitude: " + location.getLongitude() + \n";
  61.                 s += "\tAccuracy:  " + location.getAccuracy()  + " metres\n";
  62.                 s += "\tAltitude:  " + location.getAltitude()  + " metres\n";
  63.  
  64.                //Toast.makeText(SmsReceiver.this, s, Toast.LENGTH_SHORT).show();
  65.             }
  66.  
  67.                 @Override
  68.                 public void onProviderDisabled(String arg0) {   }
  69.                 @Override
  70.                 public void onProviderEnabled(String arg0) {    }
  71.                 @Override
  72.                 public void onStatusChanged(String arg0, int arg1, Bundle arg2) {       }
  73.    }
  74. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Any help is hugely appreciated
bobby123
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Nov 13, 2010 10:21 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 15 guests