因为这个,我痛苦了好几天。我想通过自动短信发送 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;
}
}