1

I am trying to create an application that will display a mock location. But as soon as I click the button to display the location, I get the exception that the application stopped unexpectedly. Here is the code:

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

     }
}

Can anyone help me with this? Thanks in advance!

ankit0311
  • 735
  • 3
  • 10
  • 20
  • can you post the exception please? – MahdeTo Mar 06 '12 at 10:29
  • Perhaps a dupe of http://stackoverflow.com/questions/8298732/unable-to-start-service-requires-access-mock-location-secure-setting but without information about how the permission was added or a log/stacktrace it's hard to know. Considering how old this is and the fact that it was never solved should it be closed? – PaulR Jan 14 '15 at 16:27

1 Answers1

0

Try adding permission ACCESS_MOCK_LOCATION to your manifest.

MahdeTo
  • 11,034
  • 2
  • 27
  • 28