0

私は活動のないサービスに取り組んでいます。バックグラウンドでのみ動作します。緯度経度を受信し、メールで送信します。問題は、プログラムがメールを送信していないことです。Java Mail API を使用しています。コードは次のとおりです。

主な活動です

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();
                }

        }




}

別のクラスは放送受信機です

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);

        }

}
4

1 に答える 1

0

このために放送受信機を書く必要はありません。更新した場所を希望する距離/時刻を指定すると、Androidは更新された場所を提供します。また、場所が変更された後にsendMail()が呼び出されるかどうかを確認してください。

于 2012-07-17T12:00:19.997 に答える