androidgpssmsmanager

auto send sms when gps location is available


i'm developing an app to send user location via sms after a fixed time and if the phone restarts the app should starts automatically in the background without any launcher activity. I have quiet accomplished my task I'm only facing problem in getting location, as gps takes some time and app gets crash when it does not find any location. here is my code of main Activity

public class MainActivity extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) 
     {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

    startLocationTracking();

}

private void startLocationTracking()
{
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    Intent alarmintent1 = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent sender1=PendingIntent.getBroadcast(MainActivity.this, 100, alarmintent1, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

    try {
        am.cancel(sender1);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("exjfkd"+e);
    }

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE,10);

    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*600, sender1);
    System.out.println("set timer");
}

}

public class AlarmReceiver extends BroadcastReceiver{


long time = 600* 1000; 
long distance = 10; 

@SuppressLint("NewApi") 

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

    System.out.println("alarm receiver....");
    Intent service = new Intent(context, MyService.class);
    context.startService(service);

    //Start App On Boot Start Up
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
    Intent App = new Intent(context, MainActivity.class);
    App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(App);

 }
try{
    LocationManager locationManager = (LocationManager)context
            .getSystemService(Context.LOCATION_SERVICE);


    Criteria criteria = new Criteria();

    String provider = locationManager.getBestProvider(criteria, true);

    locationManager.requestLocationUpdates(provider, time,
            distance, locationListener);


    Location location = locationManager.getLastKnownLocation(provider);

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String device_id = tm.getDeviceId();  // returns IMEI number 


    String phoneNo = "+923362243969";

    String Text = "Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;


    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNo, null, Text, null, null);
    Log.i("Send SMS", "");

 } 
    catch (Exception e) {
    e.printStackTrace();
 } 
    this.abortBroadcast();

}


LocationListener locationListener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onLocationChanged(Location location) {

    }
};

}

i'm little confuse in logic I want my app to send sms when gps coordinates are available. and if gps in disabled on the phone it if network is available on the phone it should get the location through network and send it through the network.


Solution

  • EDIT

    Your app is crashing since for the first time after reboot GPS/network won't have the lastknowlocation since

    Location location = locationManager.getLastKnownLocation(provider);
    

    In this line you will be receiving location as null.

    String Text = "Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;
    

    and in this line you will get the null pointer exception since location.getLatitude() is not posible.

    so before this code try to check location!=null.

    if(location!=null){
         String Text = "Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;
    }
    

    Use this method to know the status of GPS availablity

    public boolean isgpsavailable(Activity activity) {
            LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
            boolean result = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            return result;
    }
    

    Then put your code into a if(isgpsavailable(this)){...} Statement