0

我正在将地理位置作为短信发送到某个预定义的号码,但是以下代码不起作用,任何人都可以告诉我,如果我添加代码以仅跟踪位置,则以下代码中的 wat 是错误的,它给了我位置

// Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location
            // provider.
            Toast.makeText(getApplicationContext(), location.toString(), Toast.LENGTH_LONG).show();
            sendSMS(location);
        }

        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);
    Toast.makeText(getApplicationContext(),"end of pgm ", Toast.LENGTH_LONG).show();
}

static  void sendSMS(Location loc) {
    String phoneNumber = "8412866177";
    String location = loc.toString();
    Toast.makeText(null,location, Toast.LENGTH_LONG).show();
    // PendingIntent pi = PendingIntent.getActivity(this, 0, new
    // Intent(this, MainActivity.class), 0);
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, location, null, null);
}
4

1 回答 1

0

也许你没有在 Manifest.xml 中设置权限

于 2013-02-11T09:49:04.793 回答