2012-06-19 117 views
0

我是android.i的新手,通过模拟器运行应用程序。但总是出现错误.logcat显示:无法打开堆栈跟踪文件'/ data/anr/traces。 TXT':权限被拒绝通过gps获取当前位置(经度和纬度)

WhereAmIActivity.Java

package com.paad.whereami; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 

public class WhereAmIActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    LocationManager locationManager; 
    String context = Context.LOCATION_SERVICE; 
    locationManager = (LocationManager)getSystemService(context);//bulid locationmanager 

    String provider = LocationManager.KEY_LOCATION_CHANGED; 
    Location location = locationManager.getLastKnownLocation(provider);//get lastlocation 

    updateWithNewLocation(location);//pass to updatewithnewlocation 
} 

private void updateWithNewLocation(Location location){//show Location in TextView 
    String latLongString; 
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.myLocationText);// 
    if(location!=null){// 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     latLongString = "Lat:"+lat+"\nLong:"+lng; 
    }else{// 
     latLongString = "No location found"; 
    } 
    myLocationText.setText("Your Current POsition is:\n"+latLongString);// 
} 

}

使用许可权

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
+1

编写此权限:<使用权限android:name =“android.permission.INTERNET”/> –

+0

也尝试使用

回答

0

无法打开堆栈跟踪文件 '/data/anr/traces.txt':不准

我不知道你在哪里试图访问traces.txt文件。但如果其显示权限错误 那么解决它给它完全访问:)

在终端执行以下命令

$adb shell 
#cd \data 
#chmod 777 anr 

请记住,如果您尝试在实际的设备上,上述命令可能不起作用。您需要root用户。

0

这不是您获取当前位置的方式。它只是最后一个已知的位置。为了获得当前位置,需要操作系统告诉你它何时有一个新的位置(因为它需要很长的时间,基于很多事情),并且你可能会在,然后使用它。

相关问题