Android定位服务在emulator中工作,而不是在使用AsyncTask的真实设备上工作?
我使用AsyncTask使用最佳提供者标准获取用户位置。它可以在模拟器上工作,但不能在真实设备上工作 我正在使用Android定位服务在emulator中工作,而不是在使用AsyncTask的真实设备上工作?,android,android-asynctask,geolocation,google-location-services,Android,Android Asynctask,Geolocation,Google Location Services,我使用AsyncTask使用最佳提供者标准获取用户位置。它可以在模拟器上工作,但不能在真实设备上工作 我正在使用Criteria()选择最佳提供商,并使用getlastnownlocation获取位置 这是我的密码: class GetPositionTask extends AsyncTask<Void, Void, Location> implements LocationListener { ProgressDialog progressDialog; priv
Criteria()
选择最佳提供商,并使用getlastnownlocation
获取位置
这是我的密码:
class GetPositionTask extends AsyncTask<Void, Void, Location> implements LocationListener
{
ProgressDialog progressDialog;
private Location location;
private LocationManager lm;
private String mProviderName;
protected void onPreExecute()
{
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkPermission()) {
Criteria criteria = new Criteria(); //Using criteria to get the best provider.
mProviderName = lm.getBestProvider(criteria, true);
lm.requestLocationUpdates(mProviderName, 1000, 0, this);
}
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Please Wait..");
progressDialog.setMessage("Processing...!!");
progressDialog.setCancelable(false);
progressDialog.show();
}
protected Location doInBackground(Void... params)
{
// Try to use the last known position
if (checkPermission()) {
Location location = lm.getLastKnownLocation(mProviderName);
return location;
}
return null;
}
protected void onPostExecute(Location location)
{
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (location != null) {
Toast.makeText(getApplicationContext(), "GPS locked! Processing your address!", Toast.LENGTH_SHORT).show();
} else {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("Server Error! Please Try again!!") //This error message is thrown in the real device, not on emulator.
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setCancelable(false)
.show();
}
if (checkPermission()) {
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.removeUpdates(this);
if (location != null) {
new GeocodeAsyncTask().execute(location.getLatitude(), location.getLongitude()); //This is another AsyncTask to get the address from the location.
}
}
}
@Override
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_FINE_LOCATION);
return result == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0) {
boolean locationAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (locationAccepted)
checkLocationStatus();
else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(ACCESS_FINE_LOCATION)) {
showMessageOKCancel("You need to allow access to the Locations!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_CODE);
}
}
});
return;
}
}
}
}
break;
}
}
private void checkLocationStatus() {
if (checkPermission()) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
new GetPositionTask().execute();
}
else{
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
} else {
Toast.makeText(this, "Needs to access GPS. Please give access to it!", Toast.LENGTH_SHORT).show();
requestPermission();
}
}
类GetPositionTask扩展AsyncTask实现LocationListener
{
进行对话进行对话;
私人位置;
私人位置经理lm;
私有字符串mProviderName;
受保护的void onPreExecute()
{
lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
if(checkPermission()){
Criteria=new Criteria();//使用条件获取最佳提供程序。
mProviderName=lm.getBestProvider(条件为true);
lm.RequestLocationUpdate(mProviderName,1000,0,this);
}
progressDialog=新建progressDialog(MainActivity.this);
progressDialog.setTitle(“请稍候”);
progressDialog.setMessage(“处理…!!”);
progressDialog.setCancelable(假);
progressDialog.show();
}
受保护位置doInBackground(无效…参数)
{
//尝试使用最后一个已知的位置
if(checkPermission()){
位置=lm.getLastKnownLocation(mProviderName);
返回位置;
}
返回null;
}
受保护的void onPostExecute(位置)
{
if(progressDialog!=null&&progressDialog.isShowing()){
progressDialog.disclose();
}
如果(位置!=null){
Toast.makeText(getApplicationContext(),“GPS已锁定!正在处理您的地址!”,Toast.LENGTH\u SHORT.show();
}否则{
新建AlertDialog.Builder(MainActivity.this)
.setTitle(“错误”)
.setMessage(“服务器错误!请重试!!”)//此错误消息在真实设备中抛出,而不是在模拟器上抛出。
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
})
.setCancelable(错误)
.show();
}
if(checkPermission()){
lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
lm.移除更新(本);
如果(位置!=null){
新建GeocodeAsyncTask().execute(location.getLatitude(),location.getLatitude());//这是从该位置获取地址的另一个异步任务。
}
}
}
@凌驾
public void onLocationChanged(位置){}
公共无效onProviderDisabled(字符串提供程序){}
公共无效onProviderEnabled(字符串提供程序){}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){}
}
私有布尔检查权限(){
int result=ContextCompat.checkSelfPermission(getApplicationContext(),访问\u FINE\u位置);
返回结果==PackageManager.PERMISSION\u已授予;
}
私有void requestPermission(){
ActivityCompat.requestPermissions(这是一个新字符串[]{ACCESS\u FINE\u LOCATION},PERMISSION\u REQUEST\u CODE);
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,字符串权限[],int[]grantResults){
开关(请求代码){
案例权限\请求\代码:
如果(grantResults.length>0){
boolean locationAccepted=grantResults[0]==PackageManager.PERMISSION\u已授予;
如果(位置已接受)
checkLocationStatus();
否则{
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
if(应显示请求许可理由(访问位置)){
showMessageOKCancel(“您需要允许访问这些位置!”,
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
requestPermissions(新字符串[]{ACCESS\u FINE\u LOCATION},
权限(请求代码);
}
}
});
返回;
}
}
}
}
打破
}
}
私有void checkLocationStatus(){
if(checkPermission()){
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
if(locationManager.isProviderEnabled(locationManager.GPS\U提供程序)){
新建GetPositionTask().execute();
}
否则{
startActivity(新意图(设置、动作、位置、来源、设置));
}
}否则{
Toast.makeText(此“需要访问GPS。请授予访问权限!”,Toast.LENGTH\u SHORT.show();
requestPermission();
}
}
}
有什么错误吗?任何暗示都会有帮助
有没有办法不用最后一个已知的位置就可以得到当前的位置
编辑:
只是一个旁注:在真正的设备上,GPS符号在一秒钟内是可见的,然后它就隐藏了,然后出现了错误