Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从gps获取位置并发送至邮件服务无活动_Java_Android_Gprs - Fatal编程技术网

Java 从gps获取位置并发送至邮件服务无活动

Java 从gps获取位置并发送至邮件服务无活动,java,android,gprs,Java,Android,Gprs,我正在从事没有任何活动的服务。它只在后台工作。它接收纬度和经度并通过邮件发送。问题是程序没有发送邮件,我使用的是Java邮件API。代码如下: 这是主要的活动 public class MainActivity extends Service { LocationManager lm; LocationListener lis; Location loc; Double lat; Double lan; Location location;

我正在从事没有任何活动的服务。它只在后台工作。它接收纬度和经度并通过邮件发送。问题是程序没有发送邮件,我使用的是Java邮件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);

        }

}

无需为此编写广播接收器。如果您提到要更新位置的距离/时间,Android会提供更新的位置。请检查更改位置后是否调用了sendMail()。

有什么问题?你从哪里得到的错误?这是编译错误还是运行时错误?提供LOGCAT!它不能工作,问题是sendMail(showcurrentlocation)不能正常工作,任何人都可以帮助我。或将样品发送到哪个位置mail@PiyushSengar要发送邮件,您可以参考此链接“”,因为您使用的是Java mail API plz,请阅读此链接“”