2012-04-24 87 views
0

因为这个,我很痛苦几天。我想通过自动短信发送GPS数据,但我无法做到这一点。我想访问我的变量拉特和logn通过sendIntent.putextra.kindly发送数据建议任何解决方案。我想使用c2dm,但这将会太慢,并且邮件传输不能保证。我得到的错误是“lat/long无法解析为变量”如何通过自动短信发送GPS数据?

这是代码。

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

回答

0

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