我已经读过this question,并尝试了它的答案,但它对我不起作用。
我做了一个非常简单的测试应用程序,这是清单:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" android:maxSdkVersion="22"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.here.android.maps.appid"
android:value="itkC4uIay69MXXXXXXXX" />
<meta-data
android:name="com.here.android.maps.apptoken"
android:value="NdT8laoCmRysyhXXXXXXXX" />
<service
android:enabled="true"
android:exported="false"
android:name="com.here.services.internal.LocationService"
android:process=":remote">
</service>
</application>
我应该明确说明我添加了内容,但该行
android:name="com.here.services.internal.LocationService"
在Android Studio中从“服务”一词开始以红色显示(尽管所有编译,但我都认为有问题)
这是我的MainActivity
package com.company.Application;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.here.android.mpa.common.GeoCoordinate;
import com.here.android.mpa.common.GeoPosition;
import com.here.android.mpa.common.PositioningManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private static final String[] REQUIRED_SDK_PERMISSIONS = new String[] {
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE };
// permissions request code
private final static int REQUEST_CODE_ASK_PERMISSIONS = 9893;
private Timer tickMinTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermissions();
}
private void setUpPositioning() {
PositioningManager pm = PositioningManager.getInstance();
PositioningManager.LocationStatus ls = pm.getLocationStatus(PositioningManager.LocationMethod.GPS_NETWORK);
Log.i("Position", "Setting up positioning");
if (ls == PositioningManager.LocationStatus.AVAILABLE) {
Log.i("Position", "Positioning is available");
} else {
Log.w("Position", "Positioning not available right now: " + ls.toString());
}
boolean ret = pm.start(PositioningManager.LocationMethod.GPS_NETWORK);
Log.i("Position", "Positioning start returns " + ret);
timerTickEveryMinute();
}
private void timerTickEveryMinute() {
tickMinTimer = new Timer();
tickMinTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// going to try to get the location here - if not once a minute then once every x minutes
GeoPosition pos = PositioningManager.getInstance().getPosition();
if (pos == null) {
Log.w("Position", "GeoPosition is Null");
} else {
GeoCoordinate coord = pos.getCoordinate();
Log.i("Position", "Location: Latitude = " + coord.getLatitude() + ", Longitude = " + coord.getLongitude());
Log.i("Position", "Accuracy = " + pos.getLatitudeAccuracy() + ", " + pos.getLongitudeAccuracy());
}
}
}, 5000, 1000*60);
}
// check for permissions
/**
* Checks the dynamically controlled permissions and requests missing permissions from end user.
*/
protected void checkPermissions() {
final List<String> missingPermissions = new ArrayList<String>();
// check all required dynamic permissions
for (final String permission : REQUIRED_SDK_PERMISSIONS) {
final int result = ContextCompat.checkSelfPermission(this, permission);
if (result != PackageManager.PERMISSION_GRANTED) {
missingPermissions.add(permission);
}
}
if (!missingPermissions.isEmpty()) {
// request all missing permissions
final String[] permissions = missingPermissions
.toArray(new String[missingPermissions.size()]);
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_ASK_PERMISSIONS);
} else {
final int[] grantResults = new int[REQUIRED_SDK_PERMISSIONS.length];
Arrays.fill(grantResults, PackageManager.PERMISSION_GRANTED);
onRequestPermissionsResult(REQUEST_CODE_ASK_PERMISSIONS, REQUIRED_SDK_PERMISSIONS,
grantResults);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
@NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
for (int index = permissions.length - 1; index >= 0; --index) {
if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
// exit the app if one permission is not granted
Toast.makeText(this, "Required permission '" + permissions[index]
+ "' not granted, exiting", Toast.LENGTH_LONG).show();
finish();
return;
}
}
final List<String> missingPermissions = new ArrayList<String>();
for (final String permission : REQUIRED_SDK_PERMISSIONS) {
final int result = ContextCompat.checkSelfPermission(this, permission);
if (result != PackageManager.PERMISSION_GRANTED) {
missingPermissions.add(permission);
}
}
if (missingPermissions.isEmpty()) {
// all permissions were granted
setUpPositioning();
}
break;
}
}
}
因此,pm.start()始终返回false,当然GeoPosition始终为null。已授予所有权限,并且手机上的所有GPS等已打开。
也许这很简单,但是任何人都可以看到缺少的内容吗? -我要做的就是让我的应用每分钟检查一次位置。此代码不适用于免费增值模式吗?
答案 0 :(得分:0)
在@HERE开发人员支持的建议下,我使此示例正常工作=代码更改如下:
Private Sub CallSum()
With ActiveSheet.Cells(16, 1)
.Value = SumBigNumbers("12345678987654321", "34567890123987654")
.NumberFormat = "0.00"
End With
End Sub
现在我收到这样的日志:
private void setUpPositioning() {
// new lines from HERE
MapEngine.getInstance().init(this, new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(Error error) {
if (error == Error.NONE) {
Log.i("Position", "correctly started map engine");
} else {
Log.i("Position", "Problem setting up map engine: " + error);
}
}
});
//end of lines from HERE
PositioningManager pm = PositioningManager.getInstance();
PositioningManager.LocationStatus ls = pm.getLocationStatus(PositioningManager.LocationMethod.GPS_NETWORK);
Log.i("Position", "Setting up positioning");
if (ls == PositioningManager.LocationStatus.AVAILABLE) {
Log.i("Position", "Positioning is available");
} else {
Log.w("Position", "Positioning not available right now: " + ls.toString());
}
boolean ret = pm.start(PositioningManager.LocationMethod.GPS_NETWORK);
Log.i("Position", "Positioning start returns " + ret);
timerTickEveryMinute();
}
我不确定我缺少什么权限,但是这似乎无关紧要,因为代码现在可以正常工作了……不幸的是,它在我更复杂的应用程序中不起作用:-(。被提及here
答案 1 :(得分:0)
好像您在here-maps-android-sdk-call-to-positioningmanager-start-method-returns-false的帮助下解决了他的问题 但是problems-using-here-api-to-get-my-position-wifi-updated
仍然存在问题要正确初始化地图引擎,必须具备以下权限:
Manifest.permission.ACCESS_NETWORK_STATE
Manifest.permission.ACCESS_WIFI_STATE
Manifest.permission.INTERNET
缺少授予它们之一的权限将导致日志中出现MISSING_PERMISSIONS错误(我在第一个SO链接上看到过)。 关于定位,建议同时授予两个权限-ACCESS_FINE_LOCATION(允许应用程序访问精确位置)和ACCESS_COARSE_LOCATION(允许应用程序访问大概位置)。
请查看这些建议,并让我们知道问题是否已解决。谢谢!