Advertisement
code_hacker

Untitled

Oct 28th, 2011
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.79 KB | None | 0 0
  1. package com.example.gui;
  2.  
  3.  
  4. import android.app.Activity;
  5. import android.app.PendingIntent;
  6. import android.content.BroadcastReceiver;
  7. import android.content.ContentResolver;
  8. import android.content.ContentValues;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.database.Cursor;
  12. import android.location.Geocoder;
  13. import android.location.Location;
  14. import android.location.LocationManager;
  15. import android.net.Uri;
  16. import android.os.Bundle;
  17. import android.telephony.SmsManager;
  18. import android.telephony.SmsMessage;
  19. import android.util.Log;
  20. import android.view.View;
  21. import android.widget.Toast;
  22.  
  23. public class ReceivelocationActivity extends BroadcastReceiver  {
  24.    
  25.     private static final String TAG = "LocationActivity";
  26.     public static final String SMS_URI = "content://sms";
  27.     public static final String ADDRESS = "address";
  28.     public static final String PERSON = "person";
  29.     public static final String DATE = "date";
  30.     public static final String READ = "read";
  31.     public static final String STATUS = "status";
  32.     public static final String TYPE = "type";
  33.     public static final String BODY = "body";
  34.     public static final String SEEN = "seen";
  35.    
  36.    
  37.     public static final int MESSAGE_TYPE_INBOX = 1;
  38.     public static final int MESSAGE_TYPE_SENT = 2;
  39.    
  40.     public static final int MESSAGE_IS_NOT_READ = 0;
  41.     public static final int MESSAGE_IS_READ = 1;
  42.    
  43.     public static final int MESSAGE_IS_NOT_SEEN = 0;
  44.     public static final int MESSAGE_IS_SEEN = 1;
  45.     private static final String LOCATION_SERVICE = null;
  46.  
  47.     LocationManager locationManager;
  48.     Geocoder geocoder;
  49.      
  50.      double longitude,latitude;
  51.      
  52.     @Override
  53.     public void onReceive(Context context, Intent intent) {
  54.         // TODO Auto-generated method stub
  55.        
  56.         Intent m=new Intent(context, ReceivelocationActivity.class);    
  57.           PendingIntent pi=PendingIntent.getBroadcast(context, 0, m, 0);
  58.         Bundle bundle = intent.getExtras();        
  59.         SmsMessage[] msgs = null;
  60.         String str = "";
  61.         String str2="";
  62.         String str3="";
  63.         String autoReplyToken = "Request_Accepted";
  64.         if (bundle != null)
  65.         {
  66.             //---retrieve the SMS message received---
  67.             Object[] pdus = (Object[]) bundle.get("pdus");
  68.             msgs = new SmsMessage[pdus.length];            
  69.             for (int i=0; i<msgs.length; i++){
  70.                 msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
  71.                 str += "SMS from " + msgs[i].getOriginatingAddress();                    
  72.                 str2=msgs[i].getOriginatingAddress();
  73.                 str += " :";
  74.                 str += msgs[i].getMessageBody().toString();
  75.              str3=msgs[i].getMessageBody().toString();
  76.                 str += "\n";        
  77.             }
  78.             //---display the new SMS message---
  79.             Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
  80.           //  int number=Integer.parseInt(str2);
  81.            
  82.            
  83.        
  84.             SmsManager sms = SmsManager.getDefault();
  85.             boolean isAutoReply = str3.startsWith(autoReplyToken);
  86.  
  87.            
  88.             locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  89.            
  90.        //     geocoder = new Geocoder(this);
  91.            
  92.             Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  93.            
  94.      
  95.             if (location != null) {
  96.               Log.d(TAG, location.toString());
  97.                this.onLocationChanged(location);
  98.            
  99.               }
  100.            
  101.            
  102.             String msg = Double.toString(latitude) + " " +Double.toString(longitude) ;
  103.             if (!isAutoReply) {
  104.                 String autoReplyText = autoReplyToken + msg;
  105.                 sms.sendTextMessage(str2, null, autoReplyText, pi, null);
  106.             }
  107.  
  108.          
  109.          //   sms.sendTextMessage(str2, null, "Whats up", pi, null);
  110.  
  111.         }                
  112.     }
  113.    
  114.     public void onLocationChanged(Location location) {
  115.         // TODO Auto-generated method stub
  116.    
  117.  
  118.           latitude=location.getLatitude();
  119.        longitude=location.getLongitude();
  120.    
  121.  
  122.     }
  123.  
  124.     private void putSmsToDatabase( ContentResolver contentResolver, SmsMessage sms )
  125.     {
  126.         // Create SMS row
  127.         ContentValues values = new ContentValues();
  128.         values.put( ADDRESS, sms.getOriginatingAddress() );
  129.         values.put( DATE, sms.getTimestampMillis() );
  130.         values.put( READ, MESSAGE_IS_NOT_READ );
  131.         values.put( STATUS, sms.getStatus() );
  132.         values.put( TYPE, MESSAGE_TYPE_INBOX );
  133.         values.put( SEEN, MESSAGE_IS_NOT_SEEN );
  134.    
  135.        
  136.         // Push row into the SMS table
  137.         contentResolver.insert( Uri.parse( SMS_URI ), values );
  138.     }
  139.    
  140.    
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement