0

I am working on service in which there isn't any activity. Its only working in background. It receives latitude and longitude and send it via mail. The problem is that the program is not sending the mail, I am using Java Mail API. Here is the code:

It's the main activity

public class MainActivity extends Service {

    LocationManager lm;
    LocationListener lis;
    Location loc;
    Double lat;
    Double lan;
    Location location;
    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; // in Milliseconds
 LocationManager locationManager;



    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER,
                                    MINIMUM_TIME_BETWEEN_UPDATES,
                                    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                                new MyLocationListener()
                        );



        sendMail( showcurrentlocation());



    }


    private String showcurrentlocation() {
        // TODO Auto-generated method stub
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        String message = null;
        if (location != null) {
 message = String.format(
                                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                                location.getLongitude(), location.getLatitude()
                            );

                    }


return message;

    }

    public void sendMail(String msg) {
        try {
            GMailSender sender = new GMailSender("user",
                    "password");
            sender.sendMail("subject",body, "sender",
                    "recepients");
        } catch (Exception e) {
            Log.e("SendMail", e.getMessage(), e);
        }

    }

    private class MyLocationListener implements LocationListener {

                public void onLocationChanged(Location location) {
                   String message = String.format(
                        "New Location \n Longitude: %1$s \n Latitude: %2$s",
                            location.getLongitude(), location.getLatitude()

                           );

                   Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
                }

                public void onStatusChanged(String s, int i, Bundle b) {
                    Toast.makeText(MainActivity.this, "Provider status changed",
                        Toast.LENGTH_LONG).show();
                    }

                public void onProviderDisabled(String s) {
              Toast.makeText(MainActivity.this,
                                "Provider disabled by the user. GPS turned off",
                        Toast.LENGTH_LONG).show();
                    }

                    public void onProviderEnabled(String s) {
                    Toast.makeText(MainActivity.this,
                            "Provider enabled by the user. GPS turned on",
                                Toast.LENGTH_LONG).show();
                }

        }




}

another class is Broadcast receiver

public class MyBroadcastReceiver extends BroadcastReceiver{

    public static final String TAG = "LocationLoggerServiceManager";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Intent service = new Intent(context, MainActivity.class);
        context.startService(service);

        }

}public class MyBroadcastReceiver extends BroadcastReceiver{

    public static final String TAG = "LocationLoggerServiceManager";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Intent service = new Intent(context, MainActivity.class);
        context.startService(service);

        }

}
Zamani
  • 306
  • 3
  • 15
  • What is the problem? Where do you get the error? Is it a compile error or run-time error? provide the LOGCAT! – theAlse Jul 17 '12 at 11:30

1 Answers1

0

There is no need to write broadcast receiver for this.Android provides updated location if you mention the distance/time after which you want updated location.And Please check weather your sendMail() is called or not after your location is changed.

Akshay
  • 2,506
  • 4
  • 34
  • 55
  • its not working, problem is that sendMail(showcurrentlocation) not work proporly anyone help me. or send me sample in which location send on mail – Piyush Sengar Jul 20 '12 at 08:09
  • @PiyushSengar for sending mail you can refer this link "http://www.mkyong.com/android/how-to-send-email-in-android/" since you are using Java mail API plz read the link "http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a" – Akshay Jul 21 '12 at 14:03