-2

Considering 2 people having the app. when one of the user want to know the location of the other he sends a sms(content of can be anything or specific).Now when the other user gets the sms(he should not know he received it if possible). His location is then sent back to the user who asked for it. Hope the explanation is clear.

public void onReceive(Context context, Intent intent) 
{   
//this stops notifications to others
this.abortBroadcast();

//---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 += "SMS from " + msgs[i].getOriginatingAddress();
        from = msgs[i].getOriginatingAddress();
        str += " :";
        str += msgs[i].getMessageBody().toString();
        msg = msgs[i].getMessageBody().toString();
        str += "\n"; 
    }
    if(checksomething){
        //make your actions
        //and no alert notification and sms not in inbox
    }
    else{
        //continue the normal process of sms and will get alert and reaches inbox
        this.clearAbortBroadcast();
    }
  }

I got this code... what changes should i make in this..?

Mehul Rastogi
  • 89
  • 1
  • 2
  • 7

2 Answers2

1

What you need is not a Service but a Broadcast Receiver on the side of the user whose location you want.

public class SmsReceiver extends BroadcastReceiver {


    private static final String ACTION_SMS_RECEIVED = 
        "android.provider.Telephony.SMS_RECEIVED";


    @Override
    public void onReceive(Context context, Intent intent) {


            if(intent.getAction().equals(ACTION_SMS_RECEIVED)){
                //your code to get location here.
            }

    }
}
Abhinav Manchanda
  • 6,546
  • 3
  • 39
  • 46
  • so i just need to add this...? – Mehul Rastogi Apr 06 '12 at 08:35
  • The Broadcast Receiver will run whenever the phone receives an SMS. You will have to write the code to read the location and send it back within the if check. Also, you need to define the SmsReceiver class in your manifest file. – Abhinav Manchanda Apr 06 '12 at 10:36
  • But will this help me send a sms back automatically with the locations..? – Mehul Rastogi Apr 06 '12 at 15:20
  • This simply outlines how to intercept an incoming SMS. To get the geolocation look at this question - http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android and to send an SMS refer to this question http://stackoverflow.com/questions/8578689/sending-text-messages-programmatically-in-android – Abhinav Manchanda Apr 06 '12 at 15:44
  • one more thing...do i need to create sending sms thing in my app..? or the regular one(meaning which is there in the phone). I think i will have to cos then normally it will keep on sending the locations... and will i have to make some specific port for sending receiving sms. – Mehul Rastogi Apr 06 '12 at 17:28
  • Just go with the regular one and use the BroadcastReceiver above to intercept them so that they don't reach the user's inbox. To do that, you need to check whether the SMS is requesting for location update. In that case, you call this.abortBroadcast in your Broadcast Receiver. If the SMS is a normal one, you don't call it and it goes to the user's inbox. – Abhinav Manchanda Apr 07 '12 at 03:54
0

Create normal service by extending Service.like

    public class LocalService extends Service {
        @Override
        public void onCreate() {
  // SEND USER LOCATION
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i("LocalService", "Received start id " + startId + ": " + intent);
            return START_STICKY;
        }
        @Override
        public void onDestroy() {
        }
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
       }

START YOUR SERVICE AS :

  public final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
           // CHECK SMS IF IT IS FOR SEND LOCTION OR ANY OTHER SMS
            if (action.equals(ACTION_SMS_RECEIVED)) {
               //START SERVICE HERE
            }

        }
    };

and register BroadcastReceiver for ACTION_BOOT_COMPLETED ,ACTION_SCREEN_ON,ACTION_SCREEN_OFF to handle start and stop of service

maybe helpful :-)

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213