Newsgroup: it.comp.java
Titolo: [ANDROID] Problema Geolocalizzazione

Il seguente programma dovrebbe visualizzare delle coordinate 
geografiche, INVECE non appare NULLA.
Il programma funziona solo se collego al computer uno smartphone?
Con l'emulatore il programma NON funziona?
 
********** MainActivity.java **********
package com.prove.provaproject_30_geo_b;
 
import java.io.IOException;
import java.util.*;
 
import android.support.v7.app.ActionBarActivity;
import android.location.*;
import android.os.*;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
 
public class MainActivity extends ActionBarActivity
{
 
	private String providerId = LocationManager.GPS_PROVIDER;
     private Geocoder geo = null;
     private LocationManager locationManager=null;
     private static final int MIN_DIST=20;
     private static final int MIN_PERIOD=30000;
 
     private LocationListener locationListener = new LocationListener()
     {
         //. . .
         //. . .
     	 @Override
          public void onStatusChanged(String provider, int status, 
Bundle extras)
          {
 
          }
          @Override
          public void onProviderEnabled(String provider)
          {
              // attivo GPS su dispositivo
              updateText(R.id.enabled, "TRUE");
          }
          @Override
          public void onProviderDisabled(String provider)
          {
              // disattivo GPS su dispositivo
              updateText(R.id.enabled, "FALSE");
          }
          @Override
          public void onLocationChanged(Location location)
           {
              updateGUI(location);
           }
 
     };
 
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }
 
     @Override
     protected void onResume()
     {
         super.onResume();
         geo=new Geocoder(this, Locale.getDefault());
         locationManager = (LocationManager) 
getSystemService(LOCATION_SERVICE);
         Location location = 
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         if (location!=null)
             updateGUI(location);
         if (locationManager!=null && 
locationManager.isProviderEnabled(providerId))
             updateText(R.id.enabled, "TRUE");
         else
             updateText(R.id.enabled, "FALSE");
         locationManager.requestLocationUpdates(providerId, 
MIN_PERIOD,MIN_DIST, locationListener);
     }
 
     @Override
     protected void onPause()
     {
         super.onPause();
         if (locationManager!=null && 
locationManager.isProviderEnabled(providerId))
             locationManager.removeUpdates(locationListener);
     }
 
     private void updateGUI(Location location)
     {
         Date timestamp = new Date(location.getTime());
         updateText(R.id.timestamp, timestamp.toString());
         double latitude = location.getLatitude();
         updateText(R.id.latitude, String.valueOf(latitude));
         double longitude = location.getLongitude();
         updateText(R.id.longitude, String.valueOf(longitude));
         new AddressSolver().execute(location);
     }
 
     private void updateText(int id, String text)
     {
         TextView textView = (TextView) findViewById(id);
         textView.setText(text);
     }
 
     private class AddressSolver extends AsyncTask<Location, Void, String>
     {
 
         @Override
         protected String doInBackground(Location... params)
         {
             Location pos=params[0];
             double latitude = pos.getLatitude();
             double longitude = pos.getLongitude();
 
             List<Address> addresses = null;
             try
             {
                 addresses = geo.getFromLocation(latitude, longitude, 1);
             }
             catch (IOException e)
             {
 
             }
             if (addresses!=null)
             {
                 if (addresses.isEmpty())
                   {
                     return null;
                   }
                   else {
                      if (addresses.size() > 0)
                      {
                          StringBuffer address=new StringBuffer();
                          Address tmp=addresses.get(0);
                          for (int y=0;y<tmp.getMaxAddressLineIndex();y++)
                             address.append(tmp.getAddressLine(y)+"\n");
                          return address.toString();
                      }
                   }
             }
             return null;
         }
 
         @Override
         protected void onPostExecute(String result)
         {
             if (result!=null)
                 updateText(R.id.where, result);
             else
                   updateText(R.id.where, "N.A.");
 
         }
     }
 
}
 
********** activity_main.xml **********
<TableLayout xmlns:android="
http://schemas.android.com/apk/res/android
"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content">
     <TableRow android:padding="5dp">
         <TextView android:text="Abilitato" android:padding="5dp"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
         <TextView android:id="@+id/enabled" android:padding="5dp"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
     </TableRow>
     <TableRow android:padding="5dp">
         <TextView android:text="Data ora" android:padding="5dp"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
         <TextView android:id="@+id/timestamp" android:padding="5dp"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
     </TableRow>
     <TableRow android:padding="5dp">
         <TextView android:text="Latitudine" android:padding="5dp"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
         <TextView android:id="@+id/latitude" android:padding="5dp"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
     </TableRow>
     <TableRow android:padding="5dp">
         <TextView android:text="Longitudine" android:padding="5dp"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
         <TextView android:id="@+id/longitude" android:padding="5dp"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
     </TableRow>
     <TableRow android:padding="5dp">
         <TextView android:text="Localitï¿œ" android:padding="5dp"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
         <TextView android:id="@+id/where" android:padding="5dp"
             android:lines="2"
         android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>
     </TableRow>
</TableLayout>
 
********** AndroidManifest.xml **********
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android
"
     package="com.prove.provaproject_30_geo_b"
     android:versionCode="1"
     android:versionName="1.0" >
 
     <uses-sdk
         android:minSdkVersion="8"
         android:targetSdkVersion="21" />
 
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
 
     <application
         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
         <activity
             android:name=".MainActivity"
             android:label="@string/app_name" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 
                 <category 
android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
     </application>
 
</manifest>
 
---
Questa e-mail ï¿œ priva di virus e malware perchï¿œ ï¿œ attiva la protezione avast! Antivirus.
http://www.avast.com
 
 

 Con l'emulatore il programma NON funziona?
Certo che funziona, basta che l'emulatore emuli anche il GPS. Secondo te 
e' possibile?
 

 e' possibile?
 
Come faccio a emulare anche il GPS?
 
 
---
Questa e-mail è priva di virus e malware perché è attiva la protezione avast! Antivirus.
http://www.avast.com
 
 

 Come faccio a emulare anche il GPS?
Mi sembrava un'ovvietï¿œ: non lo fai.
D'altra parte ti devi aspettare che sul tuo device non ci sia un GPS, 
mica tutti ce l'hanno.
 
 

 Come faccio a emulare anche il GPS?
 
qui è spiegato:
 
http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-
 
the-android-emulator
 
in sostanza imposti manualmente le coordinate che vuoi simulare.