私は非同期でこのGPSリスナーメソッドを取得しました:
// ASync Task Begin to perform Billing information
class performBackgroundTask extends AsyncTask<Void, Void, Void> implements GpsStatus.Listener {
private Context context;
private ProgressDialog dialog;
public performBackgroundTask(Context context) {
performBackgroundTask.this.context = context;
}
protected void onPreExecute() {
dialog = ProgressDialog.show(main.this, "Info", "GPS kapcsolatra vár...");
}
protected Void doInBackground(Void... params) {
// write here the code to download or any background task.
globalconstant.mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
globalconstant.mlocManager.addGpsStatusListener(this);
return null;
}
// protected void onPostExecute(Void unused) {
// if(dialog!=null)
// dialog.dismiss();
//
// }
/* GPS kapcsolódás figyelő */
public void onGpsStatusChanged(int event) {
// Log.w("TravellerLog :: ", "l1");
switch (event) {
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
dialog.dismiss();
show_sens = show_sens + "- GPS\n";
sensors.setText(show_sens);
//addGPSListener();
Toast.makeText(getApplicationContext(), "GPS kapcsolódva!",
Toast.LENGTH_SHORT).show();// this means you found GPS
// Co-ordinates
//myChronometer.setBase(SystemClock.elapsedRealtime());
myChronometer.start();
meres = true;
start_button.setText("STOP");
break;
case GpsStatus.GPS_EVENT_STARTED:
break;
case GpsStatus.GPS_EVENT_STOPPED:
break;
}
}
// GPS
private void addGPSListener() {
globalconstant.db.setVersion(1);
globalconstant.db.setLocale(Locale.getDefault());
globalconstant.db.setLockingEnabled(true);
final String gps =
"CREATE TABLE IF NOT EXISTS GPS_Values ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, Latitude float(10, 8), Longitude float(10, 8),Accuracy INTEGER,Speed INTEGER,timestamp TIMESTAMP);";
globalconstant.db.execSQL(gps);
Log.d("FESTIVALE :: ", "Frissítési idő: "
+ globalconstant.gps_update_value);
float f = Float.valueOf(globalconstant.gps_update_value.trim())
.floatValue();
float update = f * 1000;
globalconstant.mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
globalconstant.mlocListener = new MyLocationListener();
globalconstant.mlocManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, (long) update, 0,
globalconstant.mlocListener);
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
float szel = (float) loc.getLatitude();
float hossz = (float) loc.getLongitude();
int horiAcc = (int) (loc.getAccuracy());
// int speed=(int) ((loc.getSpeed()*3600)/1000); //sebesség km/h-ban
int speed = 0;
if (loc.hasSpeed()) {
speed = (int) ((loc.getSpeed() * 3600) / 1000); // sebesség
// km/h-ban
} else {
speed = 0;
}
String test = String.format("%.08f", szel);
String test2 = String.format("%.08f", hossz);
// Geocoder geocoder = new Geocoder(main.this, Locale.getDefault());
// try {
// List<Address> addresses = geocoder.getFromLocation(szel, hossz,
// 1);
// city = addresses.get(0).getLocality();
// } catch (IOException e) {
// e.printStackTrace();
// }
ContentValues gps_values = new ContentValues();
gps_values.put("Latitude", test);
gps_values.put("Longitude", test2);
gps_values.put("Accuracy", horiAcc);
gps_values.put("Speed", speed);
//gps_values.put("City", city);
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
gps_values.put("timestamp", dateFormat.format(date));
try {
globalconstant.db.beginTransaction();
globalconstant.db.insert("GPS_Values", null, gps_values);
globalconstant.db.setTransactionSuccessful();
} finally {
globalconstant.db.endTransaction();
}
Log.d("FESTIVALE :: ", "Hely" + test + ", " + test2 + " , "
+ horiAcc + " , " + speed);
// String Text = "My current location is: " + "Latitude = "
// + loc.getLatitude() + "\nLongitude = " + loc.getLongitude();
// Toast.makeText(getApplicationContext(), "Hely" +test + "\n" +
// test2 + "\n" + horiAcc + "\n" +speed + "\n" +city,
// Toast.LENGTH_SHORT)
// .show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled",
Toast.LENGTH_SHORT).show();
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
// show gps otions
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
break;
case DialogInterface.BUTTON_NEGATIVE:
dialog.cancel();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(main.this);
builder.setMessage("A GPS nincs aktiválva!\nAktiválja most?")
.setPositiveButton("Aktivál", dialogClickListener)
.setNegativeButton("Nem", dialogClickListener).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}// gps vége
}
エラーが発生しました:
09-26 12:59:10.299: E/AndroidRuntime(719): FATAL EXCEPTION: AsyncTask #1
09-26 12:59:10.299: E/AndroidRuntime(719): java.lang.RuntimeException: An error occured while executing doInBackground()
09-26 12:59:10.299: E/AndroidRuntime(719): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-26 12:59:10.299: E/AndroidRuntime(719): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
09-26 12:59:10.299: E/AndroidRuntime(719): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
09-26 12:59:10.299: E/AndroidRuntime(719): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
09-26 12:59:10.299: E/AndroidRuntime(719): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
09-26 12:59:10.299: E/AndroidRuntime(719): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
09-26 12:59:10.299: E/AndroidRuntime(719): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
09-26 12:59:10.299: E/AndroidRuntime(719): at java.lang.Thread.run(Thread.java:1019)
09-26 12:59:10.299: E/AndroidRuntime(719): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-26 12:59:10.299: E/AndroidRuntime(719): at android.os.Handler.<init>(Handler.java:121)
09-26 12:59:10.299: E/AndroidRuntime(719): at android.location.LocationManager$GpsStatusListenerTransport$1.<init>(LocationManager.java:1281)
09-26 12:59:10.299: E/AndroidRuntime(719): at android.location.LocationManager$GpsStatusListenerTransport.<init>(LocationManager.java:1281)
09-26 12:59:10.299: E/AndroidRuntime(719): at android.location.LocationManager.addGpsStatusListener(LocationManager.java:1320)
09-26 12:59:10.299: E/AndroidRuntime(719): at com.eyecom.festivale.main$performBackgroundTask.doInBackground(main.java:800)
09-26 12:59:10.299: E/AndroidRuntime(719): at com.eyecom.festivale.main$performBackgroundTask.doInBackground(main.java:1)
09-26 12:59:10.299: E/AndroidRuntime(719): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-26 12:59:10.299: E/AndroidRuntime(719): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
09-26 12:59:10.299: E/AndroidRuntime(719): ... 4 more
どうすればいいのかわからないので、ここに投稿しました。これを修正する方法についてアドバイス/アイデア/回答をください...必要に応じて私のコードを編集できます! 回答ありがとうございます。