3 pièce(s) jointe(s)
Location.getTime() ne fonctionne pas correctement ?
Bonjour,
Je voudrais vous soumettre un problème que je rencontre avec le getTime() de location, car il me renvoie une date toujours égale à J-1 22h00.
Comme je ne sais pas où trouver ce paramètre (question posée en début de semaine et restée sans réponse à ce jour), je ne sais pas si c'est ma programmation qui est fausse, où si c'est "normal".
Ce qui m'embête le plus c'est d'avoir une heure fixe, car cela me bloque dans la suite du développement (et des tests) que je dois faire.
Je vous mets ci-dessous le code que j'ai réalisé. Notez bien que j'ai mis le minimum qui devrait afficher un getTime(). Une fois que cela marchera je l'optimiserai pour ne pas user trop de batterie, faire appel au Network si le Gps n'est pas dispo, etc ...
Je vous mets aussi en pièce jointe la totalité de ce mini-projet pour vous éviter d'avoir à tout réécrire et comme çà vous pourrez tester vous même et constater que cela ne marche pas correctement (enfin ce que j'ai fait). La seule chose que je ne vous joint pas c'est l'AVD que vous devrez créer. J'ai choisi de mon coté une AVD avec Nexus One (Target 4.4.2 / Api 19).
Avant de vous mettre le code ci-après, je vous mets aussi une capture écran pour que vous constatiez par vous même un test que j'ai fait en début de journée (15/08 à 13h00, et pour lequel il me renvoi 14/08 à 22h00). Après le démarrage de l'application, on va dans Eclipse simuler un changement de localisation (Window > Show view > Other > Emulator control) et vous devrier avoir quelque chose qui ressemble à la capture vidéo ci-après :
Pièce jointe 153827
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.localisation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_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> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| package com.example.localisation;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import android.app.Activity;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements LocationListener{
// GpsStatus.Listener, GpsStatus.NmeaListener
double dLat, dLgt;
Long lTime;
String sTime;
Date dDate, dTime;
private LocationManager lm;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick_MainActivity();
}
@Override
public void onResume(){
super.onResume();
// Begin by this one
TextView tvTrace = (TextView) findViewById(R.id.textView1);
tvTrace.setText("We are onResume");
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
// A utiliser avec le telephone
// lm.requestLocationUpdates(bestLocationProvider, 15000, 100.0f, this);
// A utiliser avec l'émulateur
// lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 100.0f, this);
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, this);
}else{
tvTrace.setText("onResume: le GPS_PROVIDER n'est pas dispo");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void btnClick_MainActivity(){
Button boutonQ = (Button) findViewById(R.id.btnQuitter);
boutonQ.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
finish();
System.exit(0);
}
});
Button boutonI = (Button) findViewById(R.id.btnTime);
boutonI.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
TextView tvTrace = (TextView) findViewById(R.id.textView1);
tvTrace.setText("Time : " + sTime);
}
});
}
@Override
public void onLocationChanged(Location aNewLocation){
dLat = (double) (aNewLocation.getLatitude());
dLgt = (double) (aNewLocation.getLongitude());
Long lTime = aNewLocation.getTime();
dDate = new Date(lTime);
SimpleDateFormat mSdf = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss", Locale.FRENCH);
// GMT, UTC, CEST, Europe/Paris, GMT+02
mSdf.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
sTime = mSdf.format(dDate);
TextView tvTrace = (TextView) findViewById(R.id.textView1);
tvTrace.setText("onLocationChanged.sTime : " + sTime.toString());
}
@Override
public void onProviderDisabled(String provider){
}
@Override
public void onProviderEnabled(String provider){
}
@Override
public void onStatusChanged(String provider, int status, Bundle exras){
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.localisation.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bonjour à tous" />
</LinearLayout>
<Button
android:id="@+id/btnTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Time" />
<Button
android:id="@+id/btnQuitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnTime"
android:layout_centerHorizontal="true"
android:text="Quitter" />
</RelativeLayout> |
Code:
1 2 3 4 5 6 7 8 9 10 11
| <?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Localisation</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="textView1">Bonjour à tous</string>
<string name="btnTime">Time</string>
<string name="btnQuitter">Quitter</string>
</resources> |
Merci par avance à ceux qui prendront le temps de regarder ce problème et de mettre le doigt sur ce que j'aurai mal programmé.
-- EDIT --
J'ajoute une capture pour que vous puissiez voir ce que j'ai mis dans l'AVD avec laquelle j'ai fait mes tests.
Pièce jointe 153831