Find location provider in emulator

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Find location provider in emulator

Postby qwertzuiop » Thu Mar 10, 2011 1:56 pm

Hi

I tried in several ways to get a location provider (network or gps) on variant api-version emulators. it never worked.
i also tried to follow this tutorial: http://www.vogella.de/articles/AndroidL ... ticle.html

The emulator seems works but can't find a provider.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.vogella.android.locationapi.simple;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.location.Criteria;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. public class ShowLocation extends Activity implements LocationListener {
  14.         private TextView latituteField;
  15.         private TextView longitudeField;
  16.         private LocationManager locationManager;
  17.         private String provider;
  18.  
  19.         /** Called when the activity is first created. */
  20.         @Override
  21.         public void onCreate(Bundle savedInstanceState) {
  22.                 super.onCreate(savedInstanceState);
  23.                 setContentView(R.layout.main);
  24.                 latituteField = (TextView) findViewById(R.id.TextView02);
  25.                 longitudeField = (TextView) findViewById(R.id.TextView04);
  26.  
  27.                 // Get the location manager
  28.                 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  29.                 // Define the criteria how to select the locatioin provider -> use
  30.                 // default
  31.                 Criteria criteria = new Criteria();
  32.                 provider = locationManager.getBestProvider(criteria, false);
  33.                 Location location = locationManager.getLastKnownLocation(provider);
  34.  
  35.                 // Initialize the location fields
  36.                 if (location != null) {
  37.                         System.out.println("Provider " + provider + " has been selected.");
  38.                         int lat = (int) (location.getLatitude());
  39.                         int lng = (int) (location.getLongitude());
  40.                         latituteField.setText(String.valueOf(lat));
  41.                         longitudeField.setText(String.valueOf(lng));
  42.                 } else {
  43.                         latituteField.setText("Provider not available");
  44.                         longitudeField.setText("Provider not available");
  45.                 }
  46.         }
  47.  
  48.         /* Request updates at startup */
  49.         @Override
  50.         protected void onResume() {
  51.                 super.onResume();
  52.                 locationManager.requestLocationUpdates(provider, 400, 1, this);
  53.         }
  54.  
  55.         /* Remove the locationlistener updates when Activity is paused */
  56.         @Override
  57.         protected void onPause() {
  58.                 super.onPause();
  59.                 locationManager.removeUpdates(this);
  60.         }
  61.  
  62.         @Override
  63.         public void onLocationChanged(Location location) {
  64.                 int lat = (int) (location.getLatitude());
  65.                 int lng = (int) (location.getLongitude());
  66.                 latituteField.setText(String.valueOf(lat));
  67.                 longitudeField.setText(String.valueOf(lng));
  68.         }
  69.  
  70.         @Override
  71.         public void onStatusChanged(String provider, int status, Bundle extras) {
  72.                 // TODO Auto-generated method stub
  73.  
  74.         }
  75.  
  76.         @Override
  77.         public void onProviderEnabled(String provider) {
  78.                 Toast.makeText(this, "Enabled new provider " + provider,
  79.                                 Toast.LENGTH_SHORT).show();
  80.  
  81.         }
  82.  
  83.         @Override
  84.         public void onProviderDisabled(String provider) {
  85.                 Toast.makeText(this, "Disenabled provider " + provider,
  86.                                 Toast.LENGTH_SHORT).show();
  87.         }
  88. }
  89.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="de.vogella.android.locationapi.simple"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.      
  7. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  8. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
  9. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
  10.  
  11.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  12.         <activity android:name=".ShowLocation"
  13.                   android:label="@string/app_name">
  14.             <intent-filter>
  15.                 <action android:name="android.intent.action.MAIN" />
  16.                 <category android:name="android.intent.category.LAUNCHER" />
  17.             </intent-filter>
  18.         </activity>
  19.  
  20.     </application>
  21.  
  22.  
  23.  
  24. </manifest>
  25.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


I don't get an error when sending new location mock data with eclipse emulator control.

Any idea?

Thank you.
qwertzuiop
Freshman
Freshman
 
Posts: 4
Joined: Thu Mar 10, 2011 1:48 pm

Top

Re: Find location provider in emulator

Postby blundell » Thu Mar 24, 2011 10:30 am

Are you running an emulator that has GPS installed?

Have you enabled 'use location' in the settings of the emulator, for wifi and network?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Find location provider in emulator

Postby qwertzuiop » Tue May 03, 2011 9:37 am

yes, gps is installed and location is enabled.

strange.

is there some other option?
qwertzuiop
Freshman
Freshman
 
Posts: 4
Joined: Thu Mar 10, 2011 1:48 pm

Re: Find location provider in emulator

Postby blundell » Tue May 03, 2011 10:17 am

Have you tried setting some criteria instead of just using the default?

http://developer.android.com/reference/ ... teria.html
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 6 guests