当我在Android Studio上运行时,此APK可用,但在手机上构建并安装APK时,此APK不可用。应该进行哪些更改?

此代码完全可以在Android Studio中的示例设备上运行时执行预期的操作。但是,当我构建APK 并将其安装到手机中时,它不起作用。 Mapsactivity.java

package com.example.coordinatetracker;

import androidx.core.app.activityCompat;
import androidx.fragment.app.Fragmentactivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.Locationmanager;
import android.os.Bundle;

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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class Mapsactivity extends Fragmentactivity implements OnmapReadyCallback {

    private GoogleMap mMap;

    private LocationListener locationListener;
    private Locationmanager locationmanager;

    private final long MIN_DIST = 1;
    private final long MIN_TIME = 100;

    private LatLng latLng;

    @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);

        activityCompat.requestPermissions(this,new String[] {Manifest.permission.accESS_FINE_LOCATION},PackageManager.PERMISSION_GRANTED);
        activityCompat.requestPermissions(this,new String[] {Manifest.permission.accESS_COARSE_LOCATION},PackageManager.PERMISSION_GRANTED);

    }


    /**
     * 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;
        //googleMap.getUiSettings().setzoomGesturesEnabled(false);
        mMap.getUiSettings().setTiltGesturesEnabled(false);
        mMap.getUiSettings().setRotateGesturesEnabled(true);
        mMap.getUiSettings().setScrollGesturesEnabled(false);

        // Add a marker in Sydney and move the camera
        /*LatLng sydney = new LatLng(-34,151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Current Location"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/

        locationListener = new LocationListener() {

            Marker marker = null;

            @Override
            public void onLocationChanged(Location location) {
                try {
                    latLng = new LatLng(location.getLatitude(),location.getLongitude());
                    //Marker marker = mMap.addMarker(new MarkerOptions().position(latLng));
                    if (marker!=null) {
                        marker.remove();
                        marker=null;
                    }

                    String lat = String.format("%.4f",location.getLatitude());
                    String lng = String.format("%.4f",location.getLongitude());
                    String coordinates = lat + "," + lng;

                    marker = mMap.addMarker(new MarkerOptions().position(latLng).title(coordinates));

                    mMap.setMinzoomPreference(15.0f);
                    mMap.setMaxzoomPreference(18.0f);
                    CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).tilt(60).zoom(18).bearing(0).build();
                    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                    mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                }
                catch(SecurityException e){
                    e.printStackTrace();
                }

            }

            @Override
            public void onStatusChanged(String provider,int status,Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };
        locationmanager = (Locationmanager) getSystemService(LOCATION_SERVICE);

        try{
            locationmanager.requestLocationUpdates(Locationmanager.GPS_PROVIDER,MIN_TIME,MIN_DIST,locationListener);
        }
        catch(SecurityException e){
            e.printStackTrace();
        }

    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.coordinatetracker">

    <!--
         The accESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2,but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <uses-permission android:name="android.permission.accESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.accESS_COARSE_LOCATION"/>

    <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">

        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key,including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/.
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".Mapsactivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我已经附加了JAVA和XML文件。为了使该程序有效,应在何处进行哪些更改? PS:我读过关于即时运行的地方,但是我的Android Studio中没有这样的选项。

yanglu06550132 回答:当我在Android Studio上运行时,此APK可用,但在手机上构建并安装APK时,此APK不可用。应该进行哪些更改?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3167019.html

大家都在问