0

我試圖獲取用戶的當前位置,並在日誌無法使用LocationListener的機器人工作室2.3

這裏顯示它在日誌中顯示當前位置我的主要活動的Java代碼

import android.Manifest; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 

public class MainActivity extends AppCompatActivity { 

LocationManager locationManager; 
LocationListener locationListener; 

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

    locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 

    locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 

      Log.i("Location",location.toString()); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 

    //If the device is running version older than Marshmallow 

    if(Build.VERSION.SDK_INT<23) 
    { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); 
    } 

    else 
    { 
     //Checking if it has permission access 
     if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
     { 
      //ask for permission 

      ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1); 


     } 
     else 
     { 
      //we have permission 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); 
     } 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

    if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) 
    { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); 
    } 
} 

}

但我沒有看到我日誌中的銷售標籤,這裏有相應的日誌:

Here are My Logs

我是Android新手,因此我不太確定代碼出錯的位置。

+0

的方式,您正在使用的位置服務已經過時了。請檢查:https://developer.android.com/training/location/receive-location-updates.html在哪裏可以找到推薦的方式。實際的方法是通過您要求提供位置更新的Google Api客戶端(或最後一個已知位置(如果可用))。希望能幫助到你。 –

+0

它實際上看起來像你的代碼應該按原樣工作。設備上設置的位置模式是什麼? –

回答

0

起初,你需要移動LocationListeneronCreate()外:

public class MainActivity extends AppCompatActivity { 

LocationManager locationManager; 
LocationListener locationListener; 

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

    if(Build.VERSION.SDK_INT<23) 
    { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); 
    } 

    else 
    { 
     //Checking if it has permission access 
     if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
     { 
      //ask for permission 

      ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1); 


     } 
     else 
     { 
      //we have permission 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); 
     } 
    } 
} 


    locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 

      Log.i("Location",location.toString()); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 

    ..... 
} 

然後,修改onRequestPermissionResult()如下:

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

    switch (requestCode) { 
     case 1: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); 

      } else { 
       Toast.makeText(getApplicationContext(), "Please provide permission.", Toast.LENGTH_LONG).show(); 
      } 
      break; 
     } 
    } 
} 
+0

我應該在哪裏將'LocationListener'移動到 – KHKR

+0

就在''onCreate'外''MainActivity'類裏面' – tahsinRupam

+0

但我確實在onCreate之外聲明瞭'LocatinonListener' – KHKR