Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.gui;
- import android.app.Activity;
- import android.app.PendingIntent;
- import android.content.BroadcastReceiver;
- import android.content.ContentResolver;
- import android.content.ContentValues;
- import android.content.Context;
- import android.content.Intent;
- import android.database.Cursor;
- import android.location.Geocoder;
- import android.location.Location;
- import android.location.LocationManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.telephony.SmsManager;
- import android.telephony.SmsMessage;
- import android.util.Log;
- import android.view.View;
- import android.widget.Toast;
- public class ReceivelocationActivity extends BroadcastReceiver {
- private static final String TAG = "LocationActivity";
- public static final String SMS_URI = "content://sms";
- public static final String ADDRESS = "address";
- public static final String PERSON = "person";
- public static final String DATE = "date";
- public static final String READ = "read";
- public static final String STATUS = "status";
- public static final String TYPE = "type";
- public static final String BODY = "body";
- public static final String SEEN = "seen";
- public static final int MESSAGE_TYPE_INBOX = 1;
- public static final int MESSAGE_TYPE_SENT = 2;
- public static final int MESSAGE_IS_NOT_READ = 0;
- public static final int MESSAGE_IS_READ = 1;
- public static final int MESSAGE_IS_NOT_SEEN = 0;
- public static final int MESSAGE_IS_SEEN = 1;
- private static final String LOCATION_SERVICE = null;
- LocationManager locationManager;
- Geocoder geocoder;
- double longitude,latitude;
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- Intent m=new Intent(context, ReceivelocationActivity.class);
- PendingIntent pi=PendingIntent.getBroadcast(context, 0, m, 0);
- Bundle bundle = intent.getExtras();
- SmsMessage[] msgs = null;
- String str = "";
- String str2="";
- String str3="";
- String autoReplyToken = "Request_Accepted";
- 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();
- str2=msgs[i].getOriginatingAddress();
- str += " :";
- str += msgs[i].getMessageBody().toString();
- str3=msgs[i].getMessageBody().toString();
- str += "\n";
- }
- //---display the new SMS message---
- Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
- // int number=Integer.parseInt(str2);
- SmsManager sms = SmsManager.getDefault();
- boolean isAutoReply = str3.startsWith(autoReplyToken);
- locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
- geocoder = new Geocoder(this);
- Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if (location != null) {
- Log.d(TAG, location.toString());
- this.onLocationChanged(location);
- }
- String msg = Double.toString(latitude) + " " +Double.toString(longitude) ;
- if (!isAutoReply) {
- String autoReplyText = autoReplyToken + msg;
- sms.sendTextMessage(str2, null, autoReplyText, pi, null);
- }
- // sms.sendTextMessage(str2, null, "Whats up", pi, null);
- }
- }
- public void onLocationChanged(Location location) {
- // TODO Auto-generated method stub
- latitude=location.getLatitude();
- longitude=location.getLongitude();
- }
- private void putSmsToDatabase( ContentResolver contentResolver, SmsMessage sms )
- {
- // Create SMS row
- ContentValues values = new ContentValues();
- values.put( ADDRESS, sms.getOriginatingAddress() );
- values.put( DATE, sms.getTimestampMillis() );
- values.put( READ, MESSAGE_IS_NOT_READ );
- values.put( STATUS, sms.getStatus() );
- values.put( TYPE, MESSAGE_TYPE_INBOX );
- values.put( SEEN, MESSAGE_IS_NOT_SEEN );
- // Push row into the SMS table
- contentResolver.insert( Uri.parse( SMS_URI ), values );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement