我正在使用LocationManager
和LocationListener
来获取地理位置信息,当我在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();
}
});
}
}
答案 0 :(得分:0)
您是否允许实际的应用程序权限? 下面的设置怎么样?
设置>应用和通知>应用信息> com.example.cmsapp(您的应用名称)>权限>位置
接下来的代码结果如何?
final boolean isGpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
对于我来说,仿真器无法获取位置,但在我的手机中可以获取值。
答案 1 :(得分: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);
}
}
您忘记了请求位置。