使用我的Android应用程序,当用户执行某项操作时,会启动后台服务,该服务会获取当前GPS位置并将其保存在数据库中,此外还会执行其他操作.在该服务中,我用的是requestLocationUpdates()从LocationManager类和等待onLocationChanged()我的LocationListener(这是由服务实现)被触发.但等待onLocationChanged开火的最佳方式是什么?我应该简单地轮询一个变量并等到它被设置?有小费吗?
注意:onLocationChanged()由于其他一些原因,我不能简单地写入数据库.
编辑:为了清除我的情况,我可以提供一个类似于我的情况的例子:假设用户按下一个按钮并myMethod触发一个方法,它将返回一些对象.然后,在这个myMethod中,我将从GPS(使用requestLocationChanged)注册位置更新,另外等待用户位置并将其用于某些事情.换句话说,myMethod在位置存在之前无法返回.onLocationChanged在这种情况下,我不知道如何使用.
我需要开发应用程序,用户必须找到他停放的汽车,并显示他和停车之间的距离.我使用GPS和定位服务.
对于距离我用半正矢式但距离始终显示0米.
我尝试了很多搜索谷歌的解决方案,但dint得到任何正确的解决方案.
谁能提出他们的建议?
我是android开发新手,我想创建一个应用程序,比如在我当前位置和选定位置之间给我MapView的距离.
请给我一些建议.
我已经设置了许可.为什么我仍然会收到此错误?
引起:java.lang.SecurityException:"gps"位置提供程序需要ACCESS_FINE_LOCATION权限.
我的明确内容:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)
我的活动代码:
public class LocationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
private LocationManager mLocationManager;
public PlaceholderFragment() {
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) { …Run Code Online (Sandbox Code Playgroud) 使用网络提供商进行位置提取时,Android设备是否需要互联网?我看到很少有文章说它没有互联网,很少说它需要互联网.
我正在研究Android内部如何进行位置提取(而不是代码).
文章说我们需要互联网:设备捕获小区ID和wifi热点并将其发送到谷歌服务器.谷歌服务器返回位置信息.这些文章说我们需要互联网来获取位置.
很少有文章说使用细胞三角测量确定位置,不需要互联网.
我试图做测试以确认相同.但是测试结果令人困惑,无法从中推断出任何东西.
设备配置:我关闭了互联网,wifi扫描和GPS.这意味着设备只有蜂窝信号.应用程序每分钟捕获一次位置.
测试结果1:
测试用例2 :(续测试用例1)
网络位置究竟如何运作.我更关注Android内部如何获取位置而不是代码.
所以这是我的问题:
- 它是否通过cellid和wifi热点并获取位置(使用互联网)?
- 它是否进行细胞三角测量?
- 它是否缓存数据并使用加速计和指南针来确定没有互联网的位置?
(虽然stackoverflow中有类似的问题.它们更多地是代码而不是Android内部工作.因此发布此问题)
我有以下情况:
如果汽车静止(不动),我想在每5分钟后调用位置更新api.如果汽车正在移动,我想每隔2分钟后调用位置更新api.
要么
如果汽车每隔5分钟移动2000米的距离比需要呼叫位置更新api.实际上如何检查该位置是否相同?
android locationlistener android-location android-mapview android-maps-v2
我有一项服务,应该捕获用户的位置和当前的电池电量,并将其发送到我的firebase后端.出于某种原因,似乎每当我在我的应用程序中启用此服务时,我的应用程序将随机打开而无需任何用户交互(即使用户在另一个应用程序中,我的应用程序将弹出打开).有谁知道为什么会这样?
这是我的服务代码:
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.server.converter.StringToIntConverter;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static android.R.attr.lines;
import static com.package.R.id.location;
import static com.package.R.id.view;
public class LocationBatteryService extends Service implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener …Run Code Online (Sandbox Code Playgroud) android android-service android-location google-play-services google-location-services
我们有一个基于位置的应用程序,根据用户位置,我们尝试推送一些广告系列通知.但这似乎耗尽了手机的电池电量,有时甚至消耗了30-35%的电量.
以下是我们的应用程序中Location的实现.
public class DashboardActivity extends BaseActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks,
LocationListener, ExitConfirmationDialog.OnExitResponseListner {
private final int HAS_PERMISSION_COARSE_LOCATION = 1;
private final int HAS_PERMISSION_FINE_LOCATION = 2;
LocationManager locationManager;
CustomTextViewDemi mNotificationCount;
String count = "";
Menu menu;
List<CreateFragmentsPojo> fragments;
boolean isSettingsScreenOpen = false;
int backPresedCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
setTitle("Dashboard");
setupNavigationView(0);
fragments = new ArrayList<>();
Utils.HandleViews(mLayout, false);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (isGPSLocationEnabled(locationManager)) {
buildGooleApiClient();
} else if (isNetworkLocationEnabled(locationManager)) {
buildGooleApiClient();
} else {
showAlert();
} …Run Code Online (Sandbox Code Playgroud) 我有一个项目,我需要获得印度的位置.但在我的模拟器中,我没有得到位置.
当我从我的模拟器中获取谷歌地图时,它显示了当前位于海中的位置.
API level I am using is 27.
Emulator I use is Nexus 5x.
Run Code Online (Sandbox Code Playgroud)如何获取此仿真器中的当前位置以及手动.
我试图编辑模拟器中的位置,并试图给出纬度和经度,但它不起作用.
android android-virtual-device android-emulator android-location
https://developer.android.com/about/versions/oreo/background-location-limits
为了降低功耗,Android 8.0(API级别26)限制了后台应用可以检索用户当前位置的频率。应用每小时只能接收几次位置更新。
该文档未提供有关后台位置检索频率限制的任何细节。
假设我的应用程序未在Android O设备上运行并且没有收到任何被动位置更新,我可以可靠地期望它接收到位置更新多少次?
有多少次是“只有几次”到底是什么?