Vikas Singh Vikas Singh - 3 months ago 21
Android Question

Auto send SMS with GPS location

I'm developing an app to send user location via

SMS
after a fixed time.
I have almost accomplished my task. I'm only facing problem when
GPS
location is not get
SMS
is sent but when i on
GPS
the sending sms is not working.

String myAddress = "";

List<Address> location;
private void getAddress() {
// TODO Auto-generated method stub



try {

startService(new Intent(mContext,GPSTracker.class));
GPSTracker tracker = new GPSTracker(mContext);

location = tracker.getGeocoderAddress(mContext);

if (tracker.canGetLocation() == false)
{
tracker.showSettingsAlert();
}
else
{
GlobalValues.location_home_lat = tracker.getLongitude();
GlobalValues.location_home_lng = tracker.getLatitude();

}


if(location != null) {
Address returnedAddress = location.get(0);
StringBuilder strReturnedAddress = new StringBuilder("location:\t");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
myAddress = strReturnedAddress.toString();



}




private void countdownStart() {
// TODO Auto-generated method stub
detectionStart.setBase(SystemClock.elapsedRealtime());
detectionStart.start();

detectionStart.setOnChronometerTickListener(new OnChronometerTickListener() {

@Override
public void onChronometerTick(Chronometer chronometer) {
// TODO Auto-generated method stub
long elapsedMillis = SystemClock.elapsedRealtime() - detectionStart.getBase();
Date date = new Date(elapsedMillis);
DateFormat formatter = new SimpleDateFormat("s");
String dateFormatted = formatter.format(date);
detectionStart.setText(dateFormatted);

if(elapsedMillis>THRESHOLD){
if(userNumber.length()>0 && message.length()>0){
sendSms(userNumber,message);
}
else{
Toast.makeText(getBaseContext(),
"Error in sendind sms",
Toast.LENGTH_SHORT).show();
}
stopAlarm();
onCloseActivity();
goMainActivity();
}
}
});
}


protected void sendSms(String number, String message) {
// TODO Auto-generated method stub
String SENT = "android.telephony.SmsManager.STATUS_ON_ICC_SENT";
String DELIVERED = "SMS_DELIVERED";

PendingIntent sentPI = PendingIntent.getBroadcast(DoorbellDetectedActivity.this, 0,
new Intent(SENT), 0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(DoorbellDetectedActivity.this, 0,
new Intent(DELIVERED), 0);


SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(userNumber, null, message, null, null);
}

hd1 hd1
Answer
    final LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        final LocationListener mlocListener = new MyLocationListener();

        final Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        updateWithNewLocation(location);
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        mc=mapview.getController();
        mc.setZoom(18);
        mlo=new MyLocationOverlay(this,mapview);
        mapview.getOverlays().add(mlo);
        mapview.invalidate();   
        }




        public class MyLocationListener implements LocationListener

        {

        public void onLocationChanged(final Location location)
        {
            updateWithNewLocation(location);
        }
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        }

        private void updateWithNewLocation(final Location location)
        {
        mapview=(MapView) findViewById(R.id.map);
        String latLongString;
        TextView myLocationText;
        myLocationText = (TextView)findViewById(R.id.text);
        String addressString = "No address found";

        if ( location != null )
        {
        final double lat = location.getLatitude();
        final double lng = location.getLongitude();

        latLongString = "Lat:" + lat + "\nLong:" + lng;
        final double latitude = location.getLatitude();
        final double longitude = location.getLongitude();
        addressString = String.format("%f, %f", latitude, longitude);
        }
        catch ( final IOException e )
        {
            System.err.println(e.toString());
        }
        }
        else
        {
        latLongString = "No location found";
        }
        myLocationText.setText("Your Current Position is:\n" +
 addressString);


        }





        @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // TODO Auto-generated method stub
            MenuInflater inf = getMenuInflater();
            inf.inflate(R.menu.newmenu, menu);
                return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            String tv1; 
            TextView myLocationText;
            myLocationText = (TextView)findViewById(R.id.text);
            tv1= myLocationText.getText().toString(); 
            Intent sendIntent = new Intent(Intent.ACTION_VIEW);
            sendIntent.putExtra("sms_body", tv1); 
            sendIntent.setType("vnd.android-dir/mms-sms");
            startActivity(sendIntent); 
            return(super.onOptionsItemSelected(item));
       }

Hope it helps

Comments