Visualizzazione post con etichetta android intent google navigator sdk eclipse app market. Mostra tutti i post
Visualizzazione post con etichetta android intent google navigator sdk eclipse app market. Mostra tutti i post

martedì 28 febbraio 2012

Android tips #4 - Start Barcode activity in Eclipse enviroment

Vediamo come integrare il lettore di barcode nelle nostre applicazioni android utilizzando quanto già sviluppato nel progetto ZXing.
Scaricare la libreria ZXing-2.0.zip dal collegamento seguente: http://code.google.com/p/zxing/downloads/detail?name=ZXing-2.0.zip&can=2&q=
Aprire quindi il file e individuare la libreria core.jar; che sarà da aggiungere agli external jars del nostro progetto.


Nella nostra activity dove meglio preferite potete eseguire l'activity barcode con il seguente codice:

public final static String RC_BARCODE_READER = "1234";
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");       
startActivityForResult(intent, RC_BARCODE_READER);

Nella stessa activity occorre fare l'override del metodo onActivityResult:

public void onActivityResult(int requestCode, int resultCode, Intent intent)
{    
   if (requestCode == RC_BARCODE_READER )
   {        
       if (resultCode == RESULT_OK)
      {            
           String contents = intent.getStringExtra("SCAN_RESULT");            
           String format = intent.getStringExtra("SCAN_RESULT_FORMAT");   
           //.....
      } else if (resultCode == RESULT_CANCELED)
      {                  
         //.....
       }    
   }
}

Si può procedere anche diversamente in modo più agile utilizzando la classe IntentIntegrator.java fornita dalla stessa libreria (IntentIntegrator.java).
Per eseguire l'activity barcode basta incollare il seguente codice:


IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();

La gestione del risultato si demanda quindi sempre al metodo onActivityResult incollando all'interno quanto segue:


IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null)
{
    //Elaborazione del risultato

}

Per altri dettagli si rimanda al collegamento: Scanning via intent

Enjoy!


venerdì 2 dicembre 2011

Android tips #3 - Get addresses from latitude and longitude

Dalla localizzazione della posizione è possibile recuperare l'indirizzo tramite il metodo getFromLocation della classe GeoCoder.

SharedPreferences mSetting = getSharedPreferences("myprefsfile", 0);
//set longitude, latidude on preferences


Di seguito il metodo per il recupero dell'indirizzo:


private String getGoogleMapLocationAddresses()
{
String add = "";
Geocoder gc = new Geocoder(getBaseContext(), Locale.getDefault());
try
{
List<address>
addresses = gc.getFromLocation(Double.parseDouble(mSetting.getString(C_LATITUDE, "")),
Double.parseDouble(mSetting.getString(C_LONGITUDE, "")), 5);

if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i) + NL;
}
} catch (IOException e)
{
Log.e(C_TAG, "getGoogleMapLocationAddresses", e);
}
return add;
}

lunedì 21 novembre 2011

Android tips #1 - Make Google Navigator Intent


Se il vostro obiettivo è eseguire l'applicazione Google Navigator verso una destinazione precedentemente acquisita e memorizzata nelle SharedPreferences è sufficente incollare il codice seguente nella vostra applicazione:

1) Creare un oggetto LocationListener per agganciare la posizione:





private static final String C_PREFS_NAME = "gnav";

private static final int C_TWO_MINUTES = 1000 * 60 * 2;

private static final String C_LATITUDE = "latitude";
private static final String C_LONGITUDE = "longitude";

LocationListener mLocationListener = new LocationListener()
{   
public void onLocationChanged(Location location) {
if (isBetterLocation(location, currentBestLocation))
{
currentBestLocation = location;
SharedPreferences settings = getSharedPreferences(
C_PREFS_NAME, 0);  
SharedPreferences.Editor editor = settings.edit();     
editor.putString(
C_LATITUDE, Double.toString(currentBestLocation.getLatitude()));
editor.putString(
C_LONGITUDE, Double.toString(currentBestLocation.getLongitude()));
editor.commit();
}
}

protected boolean isBetterLocation(Location location, Location currentBestLocation)
{
if (currentBestLocation == null) {
return true;
}

  // Check whether the new location fix is newer or older
  long timeDelta = location.getTime() - currentBestLocation.getTime();
  boolean isSignificantlyNewer = timeDelta > C_TWO_MINUTES;
  boolean isSignificantlyOlder = timeDelta < -C_TWO_MINUTES;
  boolean isNewer = timeDelta > 0;
  // If it's been more than two minutes since the current location, use
  // the new location
  // because the user has likely moved
  if (isSignificantlyNewer) {
   return true;
   // If the new location is more than two minutes older, it must be
   // worse
  } else if (isSignificantlyOlder) {
   return false;
  }
  // Check whether the new location fix is more or less accurate
  int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
    .getAccuracy());
  boolean isLessAccurate = accuracyDelta > 0;
  boolean isMoreAccurate = accuracyDelta < 0;
  boolean isSignificantlyLessAccurate = accuracyDelta > 200;
  // Check if the old and new location are from the same provider
  boolean isFromSameProvider = isSameProvider(location.getProvider(),
    currentBestLocation.getProvider());
  // Determine location quality using a combination of timeliness and
  // accuracy
  if (isMoreAccurate) {
   return true;
  } else if (isNewer && !isLessAccurate) {
   return true;
  } else if (isNewer && !isSignificantlyLessAccurate
    && isFromSameProvider) {
   return true;
  }
  return false;
 }
 /** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null)
{
return provider2 == null;
}
return provider1.equals(provider2);
}

2) Recuperare la posizione precedentemente memorizzata e aprire Google Navigator.
...
SharedPreferences settings = getSharedPreferences(C_PREFS_NAME, 0);        
String uri = "google.navigation:q=" + settings.getString(C_LATITUDE,"") + "," + settings.getString(C_LONGITUDE, "");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);

...