Android提供商未提供当前位置

时间:2015-02-25 07:52:09

标签: android location

我开发了一个应用程序,我们将代码从朋友的手机发送到我们的手机。如果代码与保存的代码匹配,那么它应该将手机的当前位置作为短信发送给朋友手机。这是我的代码

     if (message.equals(locatecode)) {
                    // Get the location manager
                    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                    // Creating an empty criteria object
                    Criteria criteria = new Criteria();
                    // Getting the name of the provider that meets the criteria
                    provider = locationManager.getBestProvider(criteria, true);
                    Toast.makeText(context, provider, Toast.LENGTH_SHORT).show();
                    if (provider != null && !provider.equals("")) {
                        locationManager.requestLocationUpdates(provider, 0, 0, locListener);
                        Toast.makeText(context,"After requestLocationUp", Toast.LENGTH_SHORT).show();
                        // Get the location from the given provider
                         Location location1 = locationManager.getLastKnownLocation(provider);
                        Toast.makeText(context, (CharSequence) location1, Toast.LENGTH_SHORT).show();
                       if(location1!=null) {
                            Toast.makeText(context, "inside location!null",Toast.LENGTH_LONG).show();
                            int lat = (int) (location1.getLatitude());
                            int lng = (int) (location1.getLongitude());
                            Toast.makeText(context, "latitude:"+String.valueOf(lat),Toast.LENGTH_LONG).show();
                            Toast.makeText(context, "longitude:"+String.valueOf(lng),Toast.LENGTH_LONG).show();
                          try {
                            SmsManager smsManager = SmsManager.getDefault();
                            smsManager.sendTextMessage(senderNum, null, m1+"latitude is"+lat+"longitude is"+lng, null, null);
                            Toast toast = Toast.makeText(context, "SMS sent.", Toast.LENGTH_LONG);
                            toast.show();
                           } catch (Exception e) {
                            Toast.makeText(context, "SMS failed, please try again.", Toast.LENGTH_LONG).show();
                           }
                       }
                        else
                            Toast.makeText(context, "Location can't be retrieved", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(context, "No Provider Found", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(context, "not matched", Toast.LENGTH_LONG).show();
                }
            } // end for loop
        }
    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);
    }
}

public class myLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if(location!=null) {
            locationManager.removeUpdates(locListener);
            Double latitude = location.getLatitude();
            Double longitude = location.getLongitude();
            System.out.println(latitude + "" + longitude);
        }
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras){      // TODO Auto-generated method stub}
    @Override
    public void onProviderEnabled(String s) {}
    @Override
    public void onProviderDisabled(String s) { }

}

my problem is it always shows location cannot be retrieved.I have got the same problem in emulator and in phone also.i have given permissions also.So please help me out how to resolve this as soon as possible.

1 个答案:

答案 0 :(得分:0)

LocationManager是一个棘手的野兽。不幸的是,它不会在getLastKnownLocation中返回一个位置,除非其他应用程序恰好已经为您提供了一个位置。您最终将在onLocationChanged中获取位置数据,但您只是将其打印出来并对其执行任何操作。然后你要删除你的位置请求,这样它只会这样做一次。考虑在onLocationChanged中执行短信息代码,不要删除更新,否则当你四处走动时就不会得到任何更新。

旁注,有一种新的更好的方法来访问您真正应该使用的位置数据,因为它更适合电池续航时间:https://developer.android.com/google/play-services/location.html