1
我正在嘗試創建一個將顯示模擬位置的應用程序。但只要我點擊按鈕來顯示位置,我就會發現應用程序意外停止的異常。這裏是代碼:在Android中使用requestSingleUpdate顯示模擬位置
public class AndroidLBS extends Activity {
public static TextView latText;
public static TextView lngText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button gpsButton = (Button) findViewById(R.id.gpsButton);
gpsButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
LoadCoords();}
});
}
public void LoadCoords()
{
latText = (TextView) findViewById(R.id.latText);
lngText = (TextView) findViewById(R.id.lngText);
Intent in=new Intent("LOC_OBTAINED");
PendingIntent pi=PendingIntent.getBroadcast(this,0,in,0);
registerReceiver(new BroadcastReceiver(){
public void onReceive(Context arg0, Intent arg1)
{
Bundle bundle=arg1.getExtras();
Location loc=(Location)bundle.get(LocationManager.KEY_LOCATION_CHANGED);
Double lat=loc.getLatitude();
Double longitude=loc.getLongitude();
latText.setText(lat.toString());
lngText.setText(longitude.toString());
}
},new IntentFilter("LOC_OBTAINED"));
LocationManager myManager=LocationManager)getSystemService(Context.LOCATION_SERVICE);
myManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER,true);
Location loc=new Location(LocationManager.GPS_PROVIDER);
loc.setLatitude(12.9);
loc.setLongitude(72.9);
myManager.setTestProviderLocation(LocationManager.GPS_PROVIDER,loc);
myManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, pi);
}
}
任何人都可以幫助我嗎?提前致謝!
請問您可以發佈例外嗎? – MahdeTo 2012-03-06 10:29:54
供應商「GPS」未知 – ankit0311 2012-03-06 10:36:24
也許是一個愚蠢的http://stackoverflow.com/questions/8298732/unable-to-start-service-requires-access-mock-location-secure-setting但沒有關於如何權限的信息添加或日誌/堆棧跟蹤很難知道。考慮到這是多大年紀,以及它應該關閉的事實,它從來沒有解決? – PaulR 2015-01-14 16:27:41