在Android中使用requestSingleUpdate显示模拟位置

时间:2012-03-06 10:28:52

标签: android gps

我正在尝试创建一个显示模拟位置的应用程序。但是只要我单击按钮显示位置,我就会得到应用程序意外停止的异常。这是代码:

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);

     }
}

任何人都可以帮我吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试向您的清单添加权限ACCESS_MOCK_LOCATION