如何通过自动短信发送gps数据?

时间:2012-04-24 07:07:10

标签: android gps sms

由于这个原因,我感到很痛苦。我想通过自动短信发送gps数据,但我无法做到。我想访问我的变量lat和logn通过sendIntent.putextra发送数据。建议任何解决方案。我想使用c2dm,但这将太慢,并且不能保证消息传递。我得到的错误是“lat / long无法解析为变量”

这是代码。

public class SendSMSActivity extends Activity {

    Button buttonSend;
    public LocationManager locationManager;

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

        buttonSend = (Button) findViewById(R.id.buttonSend);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                0,
                0,
                MyLocationListener());


        class MyLocationListener implements LocationListener {

            public void onLocationChanged(Location location) {

                String message = String.format(
                        "New Location \n Longitude: %1$s \nLatitude: %2$s",
                        location.getLongitude(), location.getLatitude());
                double lat = location.getLatitude();
                double logn = location.getLongitude();
                Toast.makeText(SendSMSActivity.this, message, Toast.LENGTH_LONG).show();
            }

            public void onStatusChanged(String s, int i, Bundle b) {

                Toast.makeText(SendSMSActivity.this, "Provider status changed",
                        Toast.LENGTH_LONG).show();
            }

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

            public void onProviderEnabled(String s) {


                Toast.makeText(SendSMSActivity.this,
                        "Provider enabled by the user. GPS turned on",
                        Toast.LENGTH_LONG).show();
            }
        }


        buttonSend.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                try {

                    MyLocationListener obj = new MyLocationListener();


                    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                    sendIntent.putExtra("Location Point", lat);
                    sendIntent.putExtra("Location Point", lagn);
                    //  sendIntent.setType("vnd.android-dir/mms-sms");
                    startActivity(sendIntent);

                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),
                            "SMS faild, please try again later!",
                            Toast.LENGTH_LONG).show();
                    //e.printStackTrace();
                }

            }
        });

    }

    private LocationListener MyLocationListener() {
        // TODO Auto-generated method stub
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

变量latlogn是类MyLocationListener的成员,因此你不能在那里使用它们!另请注意,您在lagn中使用OnClickListener(),我认为这是一个错字:)