java - Android 地图活动未在设备上显示位置
问题描述
我制作了一个 android 应用程序,它使用 android 的地图活动显示用户的位置。它在模拟器上运行良好。它在第一次尝试时在设备上也可以正常工作,然后后续尝试无法获取位置。它需要一些时间,然后它会注销 - DpmTcmClient: 120 次后找不到 'tcm' 套接字。放弃尝试。
package com.example.showuserlocation;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
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.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.net.InetAddress;
import java.util.List;
import java.util.Locale;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
LatLng userLocation;
float lat=0,lon=0;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// if requestCode == 1 this is the actual way
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,100,locationListener);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
userLocation = new LatLng(lat, lon);
//mMap.addMarker(new MarkerOptions().position(userLocation).title("You are here"));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mMap.clear();
Log.i("Location",location.toString());
lat = (float)location.getLatitude();
lon = (float)location.getLongitude();
userLocation = new LatLng(lat, lon);
mMap.addMarker(new MarkerOptions().position(userLocation).title("You are here"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> address = geoCoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
if (address != null && address.size()>0) {
Log.i("Address",address.get(0).toString());
if (address.get(0).getAddressLine(0) != null) {
Toast.makeText(MapsActivity.this, address.get(0).getAddressLine(0), Toast.LENGTH_LONG).show();
Log.i("Info",address.get(0).getAddressLine(0));
}
}
} catch (Exception e) {
Toast.makeText(MapsActivity.this, "Could not extract address", Toast.LENGTH_SHORT).show();
e.printStackTrace();
Log.i("Error","Could not extract address");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}
else {
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastLocation != null) {
userLocation = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(userLocation).title("You are here"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
Toast.makeText(this, "Your last location", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "No Known Last Location", Toast.LENGTH_SHORT).show();
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,100000,1000,locationListener);
}
}
}
以下是日志:
2020-06-30 16:16:40.769 16169-16169/? I/howuserlocatio: Late-enabling -Xcheck:jni
2020-06-30 16:16:40.989 16169-16169/com.example.showuserlocation I/howuserlocatio: The ClassLoaderContext is a special shared library.
2020-06-30 16:16:41.113 16169-16169/com.example.showuserlocation I/Perf: Connecting to perf service.
2020-06-30 16:16:41.255 16169-16169/com.example.showuserlocation I/zzbz: Making Creator dynamically
2020-06-30 16:16:41.267 16169-16169/com.example.showuserlocation W/howuserlocatio: Unsupported class loader
2020-06-30 16:16:41.270 16169-16169/com.example.showuserlocation W/howuserlocatio: Skipping duplicate class check due to unsupported classloader
2020-06-30 16:16:41.273 16169-16169/com.example.showuserlocation I/DynamiteModule: Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:202117000
2020-06-30 16:16:41.273 16169-16169/com.example.showuserlocation I/DynamiteModule: Selected remote version of com.google.android.gms.maps_dynamite, version >= 202117000
2020-06-30 16:16:41.273 16169-16169/com.example.showuserlocation V/DynamiteModule: Dynamite loader version >= 2, using loadModule2NoCrashUtils
2020-06-30 16:16:41.306 16169-16169/com.example.showuserlocation I/DynamiteLoaderV2: [71] Mapsdynamite
2020-06-30 16:16:41.310 16169-16169/com.example.showuserlocation W/howuserlocatio: Unsupported class loader
2020-06-30 16:16:41.312 16169-16169/com.example.showuserlocation W/howuserlocatio: Skipping duplicate class check due to unsupported classloader
2020-06-30 16:16:41.374 16169-16169/com.example.showuserlocation I/Google Maps Android API: Google Play services client version: 12451000
2020-06-30 16:16:41.382 16169-16169/com.example.showuserlocation I/Google Maps Android API: Google Play services package version: 202117028
2020-06-30 16:16:41.493 16169-16169/com.example.showuserlocation W/howuserlocatio: Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
2020-06-30 16:16:41.763 16169-16212/com.example.showuserlocation D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-06-30 16:16:41.764 16169-16212/com.example.showuserlocation I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
2020-06-30 16:16:41.769 16169-16169/com.example.showuserlocation W/Activity: Slow Operation: Activity com.example.showuserlocation/.MapsActivity onCreate took 580ms
2020-06-30 16:16:41.820 16169-16238/com.example.showuserlocation I/Adreno: QUALCOMM build : c455b9b, Ib7912a5d4e
Build Date : 09/16/19
OpenGL ES Shader Compiler Version: EV031.25.03.07
Local Branch : mybranche9d87587-05ad-f10c-aeb4-5cb90179c3d8
Remote Branch : quic/gfx-adreno.lnx.1.0.r48-rel
Remote Branch : NONE
Reconstruct Branch : NOTHING
2020-06-30 16:16:41.820 16169-16238/com.example.showuserlocation I/Adreno: Build Config : S L 6.0.7 AArch64
2020-06-30 16:16:41.830 16169-16238/com.example.showuserlocation I/Adreno: PFP: 0x005ff112, ME: 0x005ff066
2020-06-30 16:16:41.835 16169-16238/com.example.showuserlocation I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-06-30 16:16:41.835 16169-16238/com.example.showuserlocation I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-06-30 16:16:41.836 16169-16238/com.example.showuserlocation I/OpenGLRenderer: Initialized EGL, version 1.4
2020-06-30 16:16:41.836 16169-16238/com.example.showuserlocation D/OpenGLRenderer: Swap behavior 2
2020-06-30 16:16:41.947 16169-16169/com.example.showuserlocation I/Toast: Show toast from OpPackageName:com.example.showuserlocation, PackageName:com.example.showuserlocation
2020-06-30 16:16:43.766 16169-16233/com.example.showuserlocation W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
2020-06-30 16:16:43.771 16169-16233/com.example.showuserlocation I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
2020-06-30 16:16:43.771 16169-16233/com.example.showuserlocation I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 4
2020-06-30 16:16:43.775 16169-16233/com.example.showuserlocation I/DynamiteLoaderV2: [71] Googlecertificates
2020-06-30 16:16:43.776 16169-16233/com.example.showuserlocation W/howuserlocatio: Unsupported class loader
2020-06-30 16:16:43.778 16169-16233/com.example.showuserlocation W/howuserlocatio: Skipping duplicate class check due to unsupported classloader
2020-06-30 16:16:49.153 16169-16203/com.example.showuserlocation I/howuserlocatio: ProcessProfilingInfo new_methods=811 is saved saved_to_disk=1 resolve_classes_delay=8000
2020-06-30 16:18:42.059 16169-16234/com.example.showuserlocation W/DpmTcmClient: Couldn't find 'tcm' socket after 120times. quit trying
解决方案
推荐阅读
- java - Java,非阻塞控制台输出
- android - 工具栏与 Android 中的其他内容重叠
- web-applications - 如何模拟用户在网站上的操作?
- unit-testing - 没有应用模拟和间谍
- javascript - Google 翻译 API 中的“文本段过多”错误
- arrays - 术语之间的区别:元素与成员(数组)?
- c++ - 如何在 C++ 中使用自动循环输入数字
- amazon-web-services - 如何根据单个对象的可用性计算总可用性?
- python - Django 可能的循环导入
- wordpress - Wordpress“一键谷歌登录”出现“registration_failed”错误