这是我收听GPS位置更新的方式(使用LocationManager和a LocationListener):
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new MyLocationistener(); // LocationListener
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
30000, // milliseconds (minTime)
20, // meters (minDistance)
listener );
Run Code Online (Sandbox Code Playgroud)
但我想动态调整使用的minTime和minDistance参数LocationManager#requestLocationUpdates.我的目标是根据几个使用政策节省电池,即:
我想知道:
LocationManager#removeUpdates并LocationManager#requestLocationUpdates再次,如果是唯一的选择.编辑:应用程序是一个跟踪系统,以了解人们在哪里,以便将任务分配给最接近给定poing的人.实际上电池几乎不会持续8个小时,所以我想增加它.
我有互联网访问应用程序,不想存储string.xml不同语言的许多文件.
我想要的是:
string.xml带有英文字符串问题是如何string.xml在运行时更改现有文件或添加新文件?
我想通过最好的提供商从gps/netwrok获取位置,但总是返回相同的位置,我也可以看到谷歌地图上有一个标志到我的正确位置.请有人能告诉我我做错了什么吗?
我的活动是:
public class MainMenu2 extends Activity implements LocationListener, OnClickListener
Run Code Online (Sandbox Code Playgroud)
在我的onCreate我有:
if(isGooglePlay()){
setContentView(R.layout.menu_2);
setUpMapIfNeeded();
}
Run Code Online (Sandbox Code Playgroud)
isGooglePlay方法:
private boolean isGooglePlay() { // looks if googlemaps is available
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(status == ConnectionResult.SUCCESS){
return true;
}
else{
((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
//Toast.makeText(this, "Google Play is not available", Toast.LENGTH_SHORT).show();
return(false);
}
}//isGooglePlay
Run Code Online (Sandbox Code Playgroud)
setUpMapIfNeeded方法:
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.menu_map)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试http://developer.android.com/training/location/receive-location-updates.html上的"LocationUpdates"示例.此应用程序获取并打印位置通知.
我正在尝试根据我的最新位置更改位置更新的间隔.
所以 - 我已将mLocationRequest.setInterval()添加到onLocationChanged中
结果非常错误.我的应用程序受到许多位置更新的轰炸(几秒钟!!!!)
我对样本的唯一更改是:
private int x=0;
@Override
public void onLocationChanged(Location location) {
// Report to the UI that the location was updated
mConnectionStatus.setText(R.string.location_updated);
// In the UI, set the latitude and longitude to the value received
mLatLng.setText(String.valueOf(x++));
mLocationRequest.setInterval(1000); // Change 1
mLocationClient.requestLocationUpdates(mLocationRequest, this); // Change 2
}
Run Code Online (Sandbox Code Playgroud)
如何更改onLocationChanged内的间隔?
我认为问题是requestLocationUpdates重置了最后一个请求,然后立即发送另一个通知.所以创建一个循环.(比最快的间隔快).所以我需要一种可靠的方法来改变"实时"LocationRequest的间隔
我曾尝试过android开发人员培训.但是,我的程序无效.我只想注册一个简单的地理围栏,如:
mZielGeofence = new SimpleGeofence(
"2",
48.69421,
13.88318,
10,
SimpleGeofence.GEOFENCE_EXPIRATION_TIME,
Geofence.GEOFENCE_TRANSITION_ENTER
);
Run Code Online (Sandbox Code Playgroud)
我不想使用数据库,因为我只想尝试输入一个特定的地理围栏,就像上面的那个.
你们有人找到了一个很好的教程,有详细的文档,或者有一个简单的代码示例吗?我只需要添加1个Geofence并对enter事件做出反应.
提前谢谢了
我正在使用android studio和compileSdkVersion是23,因为我使用下面的代码
if(locationManager != null){
locationManager.removeUpdates(GPSListener.this);
}
Run Code Online (Sandbox Code Playgroud)
停止gps更新GPS Listener是一个实现LocationListener的类.
但是在removeUpdates行中我得到了lint警告
调用需要用户可能拒绝的权限:代码应明确检查权限是否可用(带
checkPermission)或处理潜力SecurityException
我没有得到上面代码中的问题.需要在清单文件中添加任何额外的权限吗?
问候.
我有一个三星Galaxy Tab答:我已经实现了android.location.LocationListener和onLocationChanged(Location location)被调用,这让我有点权的经度和纬度,但是,
问题是:
location.getTime()给我错误的时间,它返回的时间接近30万密耳左右.应该是这样的1471243684497.
我不知道该怎么办.
GPSTracker类:
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener
{
//flag for GPS Status
boolean isGPSEnabled = false;
//flag for network status
//boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
//The minimum distance to …Run Code Online (Sandbox Code Playgroud) 关于这个主题可能有类似的问题,但我需要你对一些具体要求的想法和建议.
这是我的需要 - 我们正在开发一个跟踪用户旅行的应用程序.该应用程序将开始在后台收集该用户的位置,当用户从App.Background Service"开始"他的旅程时,将根据用户在特定持续时间内的移动来获取位置.当用户"从应用程序停止"他的行程时,我们正在计算用户在所有记录位置的帮助下行进的距离(使用Google距离计算API).该应用程序在理想情况下工作正常.但主要的挑战是 - 在某些情况下,我们无法获取用户的准确和精确位置.影响的场景是 - 没有互联网,2g/3g的数据计划,GPS没有返回准确数据的某些特定区域等.
lat-long数据不正确会导致跳闸距离和路线不正确.这是App的主要问题.
请问,任何人都可以建议最好的选择/建议吗?PS - 我们尝试过GPS,网络,FusedLocationProvider.
编辑2 -
我们已经在准确性和距离的基础上实现了逻辑.更接近点.而且刚刚从谷歌那里找到了一个有用的api来纠正一些与实际道路分散注意力的位置点.在此发布以供其他人参考......
我最近使用Google推出的Activity Transition API来检测用户何时进出车辆.下面是我用来实现相同的代码.
val intent = Intent(context, ActivityTransitionReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT)
transitions.apply {
add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build())
add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build())
}
var transitionRequest = ActivityTransitionRequest(transitions)
// myPendingIntent is the instance of PendingIntent where the app receives callbacks.
val task = ActivityRecognition.getClient(context).requestActivityTransitionUpdates(transitionRequest, pendingIntent)
task.addOnSuccessListener {
// Handle success
context.toast("Task added successfully")
}
task.addOnFailureListener {
// Handle error
context.toast("Error adding task")
}
Run Code Online (Sandbox Code Playgroud)
该应用程序工作正常,并在未从最近的应用程序选项卡中删除时接收广播.但是,当应用程序从最近的应用程序选项卡中删除或被系统杀死时,我无法接收广播.我不知道我做错了什么.还有其他方法可以达到同样的目的吗?
我考虑使用前台服务,以便应用程序不被杀死,但后来认为显示永久通知只是为了检测用户活动过渡不是一个好主意.任何帮助将不胜感激.谢谢.
编辑:我已经看到一些应用程序要求特殊权限,例如"忽略电池优化",当获得此权限时,该应用程序似乎始终有效并且不断发送通知.
android broadcastreceiver android-location activity-recognition
我想使用最新的融合位置提供商客户端在"后台服务"上获取位置更新.我不想使用所有正在使用的位置监听器和Google API客户端.我还需要使用谷歌播放服务提供的位置设置Api来检查位置设置是否禁用或启用"后台服务".请帮助.
android android-location google-play-services fusedlocationproviderapi google-location-services