下面的代码给了我手机的位置,但没有发送消息。
当我在位置管理器减速之前给出短信代码时,它会在尝试之前发送短信。
try{
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
String message;
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
String loc=location.toString();
// Toast.makeText(getApplicationContext(), loc,Toast.LENGTH_LONG).show();
String phoneNumber="5556";
message="this is amog and it is my current location "+loc;
sendSMS(phoneNumber, message);
Toast.makeText(getApplicationContext(), message,Toast.LENGTH_LONG).show();
sendSMS(phoneNumber, message);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}catch(Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(),Toast.LENGTH_LONG).show();
}