当调用Android API的位置管理器类中的方法时,以下警告会一直显示。我使用的是最新的Android Studio 2.1,目标操作系统是Android 4.0.3或更高版本。那有什么不对呢?提前谢谢!
I / dalvikvm:找不到方法android.content.Context.getSystemService,从方法引用.MainActivity $ GPSService.access $ super
W / dalvikvm:VFY:无法解析虚方法532:Landroid / content / Context; .getSystemService
(Ljava /郎/类;)Ljava /郎/对象;D / dalvikvm:VFY:将代码0x6f替换为0x0035 I / dalvikvm:找不到方法android.content.Context.getColorStateList,从方法引用
W / dalvikvm:VFY:无法解析虚方法514:Landroid / content / Context; .getColorStateList(I)Landroid / content / res / ColorStateList;
- 引用
D / dalvikvm:VFY:替换0x004f处的操作码0x6f I / dalvikvm:找不到方法android.content.ContextWrapper.getCodeCacheDir,从方法
W / dalvikvm:VFY:无法解析虚方法585:Landroid / content / ContextWrapper; .getCodeCacheDir()Ljava / io / File;
D / dalvikvm:VFY:在0x00e1替换操作码0x6f
I / dalvikvm:找不到方法android.content.ContextWrapper.getNoBackupFilesDir,从方法引用
W / dalvikvm:VFY:无法解析虚方法597:Landroid / content / ContextWrapper; .getNoBackupFilesDir()Ljava / io / File;
D / dalvikvm:VFY:在0x01f4替换操作码0x6f
I / dalvikvm:找不到方法android.content.Context.getDrawable,从方法引用
W / dalvikvm:VFY:无法解析虚方法516:Landroid / content / Context; .getDrawable(I)Landroid / graphics / drawable / Drawable;
D / dalvikvm:VFY:将代码0x6f替换为0x0317
I / dalvikvm:找不到方法android.content.ContextWrapper.getSystemServiceName,从方法引用
W / dalvikvm:VFY:无法解析虚方法607:Landroid / content / ContextWrapper; .getSystemServiceName(Ljava / lang / Class;)Ljava / lang / String;
D / dalvikvm:VFY:在0x03c6替换操作码0x6f
I / dalvikvm:找不到方法android.content.ContextWrapper.getExternalMediaDirs,从方法引用
W / dalvikvm:VFY:无法解析虚方法593:Landroid / content / ContextWrapper; .getExternalMediaDirs()[Ljava / io / File;
D / dalvikvm:VFY:将代码0x6f替换为0x040b I / dalvikvm:找不到方法android.content.Context.getColor,从方法引用
W / dalvikvm:VFY:无法解析虚方法513:Landroid / content / Context; .getColor(I)I
D / dalvikvm:VFY:在0x0534替换操作码0x6f
I / dalvikvm:找不到方法android.content.ContextWrapper.checkSelfPermission,从方法引用
W / dalvikvm:VFY:无法解析虚方法561:Landroid / content / ContextWrapper; .checkSelfPermission(Ljava / lang / String;)I
D / dalvikvm:VFY:将代码0x6f替换为0x06d2
public static class GPSService extends Service implements android.location.LocationListener{
MainActivity ma = new MainActivity();
private ScheduledExecutorService scheduleTaskExecutor;
private ScheduledFuture scheduledFuture=null;
private Context sContext = GPSService.this;
private String deviceId;
//= loadStr(getContext(), "deviceId");
final static String TAG = "GPSService";
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSService() {
super();
}
public Context getContext() {
return sContext;
}
public Location getLocation() {
try {
/*
locationManager = (LocationManager) sContext
.getSystemService(LOCATION_SERVICE);*/
// getting GPS status
/*
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
2000,
10, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
2000,
10, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}else {
showSettingsAlert();
}
}*/
}catch (SecurityException e){
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS(){
try{
if(locationManager != null){
locationManager.removeUpdates(GPSService.this);
}
}catch (SecurityException e){
e.printStackTrace();
}
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(sContext);
alertDialog.setTitle("GPS");
alertDialog.setMessage("GPS setting");
alertDialog.setPositiveButton("setting", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
sContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
//--------------------------------------------------------------
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
//------------------------------------------------------------
@Override
public void onCreate() {
super.onCreate();
Log.d("service","on create");
//getLocation();
try {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 2000, 10, this);
}catch(SecurityException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d("service","on start cmd");
Bundle extras = intent.getExtras();
if(extras == null){
this.deviceId=null;
Log.d("Service passed id","null");}
else
{
this.deviceId = (String) extras.get("deviceId");
Log.d("passed id",deviceId);
}
scheduleTaskExecutor= Executors.newScheduledThreadPool(5);
scheduledFuture = scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
Log.d("inside on Start Cmd","running thread");
try{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch(SecurityException e){
e.printStackTrace();
}
}
}, 0, 10, TimeUnit.SECONDS);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (scheduledFuture!=null){
scheduledFuture.cancel(false);
Log.d("service" , "on destroy scheduled future");
}
if (locationManager!=null)
stopUsingGPS();
Log.d("service" , "on destroy");
}
}
答案 0 :(得分:0)
抱歉,最后我自己拿到了。这是由于我的愚蠢错误。位置管理器方法应该按如下方式分组。
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 2000, 10, this);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);