LocationManager不工作/显示位置

问题描述 投票:0回答:2

我正在努力使用LocationManagerLocationListener得到我的地理定位我得到一个结果当我在我的Android模拟器中使用它记录位置,但它不是准确的位置。但当我尝试我的手机时,它不是已经记录的位置,我不知道我在哪里出错了。我已经在Manifest中声明了所有要求,并将我的手机设置为gps上的High accuary并将它们连接到wifi。

这是我的代码:

的Manifest.xml

<?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="com.example.cmsapp">

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

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

    <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=".SplashscreenActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

这是我尝试记录地理位置的文件:

package com.example.cmsapp;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.provider.Browser;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.example.cmsapp.Http.Url;
import com.example.cmsapp.Service.AsyncResponse;
import com.example.cmsapp.Service.Internet;
import com.example.cmsapp.Service.Session;

import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    Session session = new Session(MainActivity.this);

    Button btnCall, btnSms;
    Intent intent;
    WebView wvMap;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnCall = (Button) findViewById(R.id.btnCall);
        btnSms = (Button) findViewById(R.id.btnSms);
        wvMap = (WebView) findViewById(R.id.wvMap);

        wvMap.setWebViewClient(new WebViewClient());

        WebSettings wSettings = wvMap.getSettings();

        wSettings.setJavaScriptEnabled(true);
        wSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        wSettings.setSupportZoom(true);
        wSettings.setDomStorageEnabled(true);
        wSettings.setGeolocationEnabled(true);

        wvMap.loadUrl(Url.map);

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.i("", "Location: " + location.getLatitude() + ", " + location.getLongitude());
            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, locationListener);

        btnCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:+639959870688"));

                if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                startActivity(intent);
            }
        });

        btnSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread threadStart = new Thread() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                final Session session = new Session(MainActivity.this);
                                Internet internet = new Internet(new AsyncResponse() {
                                    @Override
                                    public void processFinish(String result) {
                                        Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
                                    }
                                }, MainActivity.this);
                                String url_query = "?token=" + session.get_session("token") + "&fullname=" + session.get_session("fullname")
                                        + "&area=" + session.get_session("area");
                                url_query = url_query.replace("-", "_");
                                url_query = url_query.replace(" ", "%20");
                                internet.execute("get", Url.requestSms + url_query);
                            }
                        });
                    }
                };

                threadStart.start();
            }
        });
    }
}
android geolocation android-gps
2个回答
0
投票

您是否允许实际的应用权限?以下设置怎么样?

设置>应用和通知>应用信息> com.example.cmsapp(您的应用名称)>权限>位置

以下代码的结果怎么样?

final boolean isGpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

就我而言,模拟器无法获取位置,但在我的手机中它可以获得价值。


0
投票
    Location location;
    protected LocationManager locationManager;
    boolean isGPSEnabled =false;
    boolean isNetworkEnabled = false;        


myLocationDataSource = new MyLocationDataSource(this);
        locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER);

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    }
                }
            }

            if (location == null) {
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    }
                }
            }

            if(location != null){
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
            }
        }

你忘记了要求一个位置。

© www.soinside.com 2019 - 2024. All rights reserved.