我試圖讓緯度和經度的當前位置。 然而,當我使用下面的代碼,位置監聽器根本沒有被觸發。 我已經在清單文件中添加了LOCATION權限。無法獲得緯度和經度
任何人都可以幫忙嗎?
下面是Java代碼:
package com.example.setlocaction;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity{
double latitude, longitude, radius;
LocationManager mlocManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Acquire a reference to the system Location Manager
mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location loc)
{
// Called when a new location is found by the network location provider.
Log.i("loc","location changed");
if (loc!=null)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +
"Latitud =" + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
}
public void onStatusChanged(String provider, int status, Bundle extras)
{}
public void onProviderEnabled(String provider)
{}
public void onProviderDisabled(String provider)
{}
};
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = mlocManager.getBestProvider(criteria, true);
mlocManager.requestLocationUpdates(provider, 2000, 0, locationListener);
Location loc=mlocManager.getLastKnownLocation(provider);
if (loc==null)
{
Log.i("loc","loc=null");
}
}
}
mafinest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.setlocaction"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.setlocaction.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
發表您的清單代碼添加以下代碼在這裏 – Andrain
你需要清單中允許使用的COARSE + FINE位置以及您給它的時間--2000 ms,即2秒不是e即使是NETWORK_PROVIDER也沒有。將此延遲至20000 ms,然後重試。 – g00dy
@David我編輯了問題併發布了清單代碼。 – sam