每次运行代码时都会出现错误:
无法从经纪人那里获得服务。 java.lang.SecurityException:未知调用包名称“com.google.android.gms”。 在 android.os.Parcel.createExceptionOrNull(Parcel.java:3057) 在 android.os.Parcel.createException(Parcel.java:3041) 在 android.os.Parcel.readException(Parcel.java:3024) 在 android.os.Parcel.readException(Parcel.java:2966) 在 m.cqy.q(:com.google.android.gms.policy_maps_core_dynamite@[电子邮件受保护]:180) 在 m.cpk.run(:com.google.android.gms.policy_maps_core_dynamite@[电子邮件受保护]:54) 在 android.os.Handler.handleCallback(Handler.java:958) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loopOnce(Looper.java:205) 在 android.os.Looper.loop(Looper.java:294) 在 android.os.HandlerThread.run(HandlerThread.java:67)
我的代码:
package il.co.jonathan.monstercatch.ACTIVITIES;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
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 com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import il.co.jonathan.monstercatch.R;
public class Game_Activity extends FragmentActivity implements OnMapReadyCallback {
private Location currentLocation;
private LocationCallback locationCallback;
private FusedLocationProviderClient fusedClient;
private static final int REQUEST_CODE = 101;
private FrameLayout map;
private ImageView menuIcon;
private void initializeViews() {
map = findViewById(R.id.map);
menuIcon = findViewById(R.id.menuIcon);
}
private void setAdapters() {
}
private void setListeners() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
initializeViews();
setAdapters();
setListeners();
getLocation();
}
private void getLocation() {
if (ActivityCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_CODE);
return;
}
fusedClient = LocationServices.getFusedLocationProviderClient(this);
Task<Location> task = fusedClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
currentLocation = location;
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
if (supportMapFragment != null) {
supportMapFragment.getMapAsync(Game_Activity.this);
} else {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setScrollGesturesEnabled(false);
LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title("Player");
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
googleMap.addMarker(markerOptions);
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
}
}
}
}
我的毕业生:
plugins {
id("com.android.application") version "8.2.2"
id("com.google.gms.google-services") version "4.4.0"
id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") version "2.0.0"
}
android {
namespace = "il.co.jonathan.monstercatch"
compileSdk = 34
defaultConfig {
applicationId = "il.co.jonathan.monstercatch"
minSdk = 27
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
secrets {
propertiesFileName = "secrets.properties"
defaultPropertiesFileName = "local.defaults.properties"
// Configure which keys should be ignored by the plugin by providing regular expressions.
// "sdk.dir" is ignored by default.
ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore"
ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding = true
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation(project(":VIEWMODEL"))
implementation(project(":MODEL"))
implementation(project(":HELPER"))
implementation("androidx.navigation:navigation-fragment:2.7.6")
implementation("androidx.navigation:navigation-ui:2.7.6")
implementation("com.google.android.gms:play-services-maps:18.2.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("com.google.android.gms:play-services-location:21.1.0")
implementation("com.google.android.gms:play-services-gcm:17.0.0")
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="il.co.jonathan.monstercatch">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MonsterCatch"
tools:targetApi="31">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="my api" />
<activity
android:name=".ACTIVITIES.Game_Activity"
android:exported="false"
android:label="@string/title_activity_game" />
<activity
android:name=".ACTIVITIES.SignUpActivity2"
android:exported="false"
android:label="@string/title_activity_sign_up2"
android:theme="@style/Theme.MonsterCatch" />
<activity
android:name=".ACTIVITIES.LogInActivity2"
android:exported="false" />
<activity
android:name=".ACTIVITIES.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
解决我的问题
java.lang.SecurityException:未知调用包名称“com.google.android.gms”。
这是一个 API 密钥问题,请确保将其添加到 Manifest 和 .property 文件中。您还需要在 Google 开发者控制台中启用 Android SDK 和 API KEY。