2012-04-24 70 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,我認爲這是一個錯字:)