android - Send SMS just once for GPS location -


i'm making application runs in background in mobile. when send sms hdpk gps, application supposed send gps co-ordinates of cell on fixed number. receive gps co-ordinates on number continuously. have tried removeupdates in vain! please me sort issue out. when application listens in background sms,the gps , application crashes when receives message, although toast before crash. if application running on screen, wont crash , continuously sends messages of co-ordinates.

public class recactivity extends activity { double current_lat, current_lng; boolean flag=true; // string provider=locationmanager.gps_provider; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main);  final string sms_received = "android.provider.telephony.sms_received"; broadcastreceiver smsbr = new broadcastreceiver() {  @override public void onreceive(context context,intent intent) {  bundle bundle = intent.getextras(); if (bundle != null) {  object[] pdus = (object[]) bundle.get("pdus"); final smsmessage[] messages = new smsmessage[pdus.length]; (int = 0; < pdus.length; i++) messages[i] = smsmessage.createfrompdu((byte[]) pdus[i]); if (messages.length > -1) { string messagebody=messages[0].getmessagebody(); if(messagebody.tostring().matches("hdpk gps")) {  locationmanager mlocmanager = (locationmanager)getsystemservice(recactivity.location_service); locationlistener mloclistener = new mylocationlistener();                     toast.maketext(recactivity.this,"gps started", toast.length_long)                                         .show(); mlocmanager.requestlocationupdates(locationmanager.gps_provider, 1000,1, mloclistener);                                   }                                                                                             }  } }  };  intentfilter smsfilter = new intentfilter(sms_received); this.registerreceiver(smsbr, smsfilter); }  public class mylocationlistener implements locationlistener {  public void onlocationchanged(location loc) { toast.maketext(recactivity.this,"gps working", toast.length_long).show(); current_lat=loc.getlatitude(); current_lng=loc.getlongitude(); string text = "my location is: " +  "latitude = " + current_lat +  "longitude = " + current_lng;  smsmanager sender=smsmanager.getdefault(); sender.sendtextmessage("9762281814",null,text , null, null); toast.maketext(recactivity.this, "sms sent", toast.length_long).show();    }    public void onproviderdisabled(string arg0) { // todo auto-generated method stub smsmanager sender=smsmanager.getdefault(); sender.sendtextmessage("9762281814",null,"gps disabled" , null, null);  }  public void onproviderenabled(string arg0) { // todo auto-generated method stub smsmanager sender=smsmanager.getdefault(); sender.sendtextmessage("9762281814",null,"gps enabled" , null, null);  }   public void onstatuschanged(string arg0, int arg1, bundle arg2) { // todo auto-generated method stub  }  }  } 

the manifest file:

<uses-sdk android:minsdkversion="10" /> <uses-permission android:name="android.permission.receive_sms"/> <uses-permission android:name="android.permission.read_sms"/> <uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.send_sms"/> <uses-permission android:name="android.permission.access_coarse_location"/>  <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".recactivity" android:label="@string/app_name" > <intent-filter android:priority="100"> <action android:name="android.intent.action.main" />  <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>  </manifest> 

here code:

package recsm.rec.receiveharsh;  import android.app.activity; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.telephony.smsmanager; import android.telephony.smsmessage; import android.widget.toast;  public class recactivity extends activity { double current_lat, current_lng; boolean flag=true; locationmanager mlocmanager; locationlistener mloclistener; // string provider=locationmanager.gps_provider; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main);  final string sms_received = "android.provider.telephony.sms_received"; broadcastreceiver smsbr = new broadcastreceiver() {  @override public void onreceive(context context,intent intent) {  bundle bundle = intent.getextras(); if (bundle != null) {  object[] pdus = (object[]) bundle.get("pdus"); final smsmessage[] messages = new smsmessage[pdus.length]; (int = 0; < pdus.length; i++) messages[i] = smsmessage.createfrompdu((byte[]) pdus[i]); if (messages.length > -1) {   string messagebody=messages[0].getmessagebody();   if(messagebody.tostring().matches("hdpk gps")) {  locationmanager mlocmanager = (locationmanager)getsystemservice(recactivity.location_service); locationlistener mloclistener = new mylocationlistener();                     toast.maketext(recactivity.this,"gps started", toast.length_long)                                         .show(); mlocmanager.requestlocationupdates(locationmanager.gps_provider, 1000,1, mloclistener);                                   }   }  } }  };  intentfilter smsfilter = new intentfilter(sms_received); this.registerreceiver(smsbr, smsfilter); }  public class mylocationlistener implements locationlistener {  public void onlocationchanged(location loc) {   toast.maketext(recactivity.this,"gps working", toast.length_long) .show();  current_lat=loc.getlatitude(); current_lng=loc.getlongitude(); string text = "my location is: " +  "latitude = " + current_lat +  "longitude = " + current_lng;  smsmanager sender=smsmanager.getdefault(); sender.sendtextmessage("9762281814",null,text , null, null); toast.maketext(recactivity.this, "sms sent", toast.length_long).show(); mlocmanager.removeupdates(mloclistener);   }    public void onproviderdisabled(string arg0) { // todo auto-generated method stub smsmanager sender=smsmanager.getdefault(); sender.sendtextmessage("9762281814",null,"gps disabled" , null, null);  }  public void onproviderenabled(string arg0) { // todo auto-generated method stub smsmanager sender=smsmanager.getdefault(); sender.sendtextmessage("9762281814",null,"gps enabled" , null, null);  }  public void onstatuschanged(string arg0, int arg1, bundle arg2) { // todo auto-generated method stub  }  }  } 

string text = "my location is: " +  "latitude = " + current_lat +  "longitude = " + current_lng;  smsmanager sender=smsmanager.getdefault(); sender.sendtextmessage("9762281814",null,text , null, null); toast.maketext(recactivity.this, "sms sent", toast.length_long).show();  mlocmanager.removeupdates(mloclistener); <<<<<<<<<<<< include 

code looks fine me.

is device have gps hardware in it??.

why dont try fetching location network provider. make sure have android.permission.internet in manifest before trying code.

if (connectivitymanager.getnetworkinfo(connectivitymanager.type_mobile)                 .isconnected()                 || connectivitymanager.getnetworkinfo(                         connectivitymanager.type_wifi).isconnected())         {             /*log.d("vipul", "wifi or gprs connected");*/             if (locationmanager                     .isproviderenabled(locationmanager.network_provider))             {                 constants.connected = true;                 /*log.v("tripsketch", "network provider enabled");*/                 locationmanager.requestlocationupdates(                         locationmanager.network_provider, 5000, 1, this);             } else             {                 constants.connected = false;                 /*log.d("vipul",                         "please enable wireless networks in location setting!!");*/                 toast.maketext(                         context,                         "please enable wireless networks in location setting!!",                         10000).show();             }          } else if (locationmanager                 .isproviderenabled(locationmanager.gps_provider))         {             constants.connected = true;             /*log.d("vipul", "gps provider enabled!");*/             locationmanager.requestlocationupdates(                     locationmanager.gps_provider, 120000, 100, this);          } else         {             /*log.d("vipul", "neither gps nor edge/gprs connected");*/             constants.connected = false;             toast.maketext(                     context,                     "please connect edge/gprs \nalso enable wireless networks in location setting!!",                     10000).show();         }     } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -