Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Provider未提供当前位置_Android_Location - Fatal编程技术网

Android Provider未提供当前位置

Android Provider未提供当前位置,android,location,Android,Location,我已经开发了一个应用程序,在这个应用程序中,我们将代码从我们的朋友手机发送到我们的手机。如果代码与保存的代码匹配,那么它应该将手机的当前位置作为短信发送到朋友手机。这是我的密码 if (message.equals(locatecode)) { // Get the location manager locationManager = (LocationManager) context.getSystemS

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

     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) { }
}


LocationManager是一只狡猾的野兽。不幸的是,它不会返回getLastKnownLocation中的位置,除非其他应用程序碰巧已经为您提供了位置。最终,您将在onLocationChanged中获得位置数据,但您只是将其打印出来,而不进行任何处理。然后,您将删除对位置的请求,这样它将只执行一次。考虑在OnLogTeC改中执行你的SMS消息代码,并且不删除VeluPoPosits,或者在你四处走动时不会得到任何更新。


另请注意,有一种新的更好的访问位置数据的方法,您确实应该使用它,因为它有利于电池寿命:

我无法在OnLocation中实现sms代码已更改,因为sms只能在broadcastreceiver类中实现。只有当smscode与保存的smscode匹配时,我才调用LocationManager。您能建议我如何在OnLocationChanged内部实现smscode吗?我的意思是这段代码:Toast.makeText(上下文,“inside location!null”,Toast.LENGTH_LONG).show();intlat=(int)(location1.getLatitude());int lng=(int)(location1.getLongitude());Toast.makeText(上下文,“纬度:”+String.valueOf(lat),Toast…应该在onlocationchanged课程中,并修改为使用onlocationchanged中发生的位置。Toast在onlocationchanged中不起作用。它在上下文中显示错误我用getBaseContext和hetApplicationContext替换了它,但它不起作用这是因为myLocationListener没有valid ui上下文。您可以在创建时传入它。
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.