【问题标题】:Displaying mock location using requestSingleUpdate in Android在 Android 中使用 requestSingleUpdate 显示模拟位置
【发布时间】:2012-03-23 19:40:34
【问题描述】:

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

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

     }
}

谁能帮我解决这个问题?提前致谢!

【问题讨论】:

  • 你能发布例外吗?
  • 可能是 stackoverflow.com/questions/8298732/… 的骗子,但没有关于如何添加权限或日志/堆栈跟踪的信息,很难知道。考虑到这有多老了,如果关闭它就永远无法解决这一事实?

标签: android gps


【解决方案1】:

尝试将权限ACCESS_MOCK_LOCATION 添加到您的清单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 2013-02-08
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    相关资源
    最近更新 更多