gpt4 book ai didi

Android,加速度计不会停止不知道为什么?

转载 作者:行者123 更新时间:2023-11-30 04:05:20 25 4
gpt4 key购买 nike

谁能帮我解释为什么加速度计不停止?有 unregisterListener 行,但什么也没有。

代码如下:

public class Festivale extends Activity implements SensorEventListener {

Button button;
CheckBox video, gps, acc;
Boolean recording = false;
public static SQLiteDatabase db;
String strlocation;
String city;
private SensorManager sensorManager;
Chronometer myChronometer;
EditText myEditText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myChronometer = (Chronometer) findViewById(R.id.chronometer);
myEditText = (EditText) findViewById(R.id.editText1);

addDataBase(); // adatbázist hoz létre
addListenerOnButton();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Készítette: Lakatos Dávid\nEmail: david.lakatos@gmail.com\nTel.: +3620/427-1166")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
return true;

}

private void addGPSListener() {

String text = myEditText.getText().toString();

float f = Float.valueOf(text.trim()).floatValue();
float update = f * 1000;

if (update < 100 || update > 1000000) {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(Festivale.this).create();
alertDialog.setTitle("Hiba!");
alertDialog.setMessage("Helytelen frissítési idõ:\n" + update
/ 1000 + " sec!");
alertDialog.show();
} else {
globalconstant.mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
globalconstant.mlocListener = new MyLocationListener();
globalconstant.mlocManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, (long) update, 0,
globalconstant.mlocListener);
}

}

/* Létrehozzuk a program adatbázisát */
private void addDataBase() throws SQLException {
db = openOrCreateDatabase("Festivale.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);

}

public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);

button.setEnabled(true);
button.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

gps = (CheckBox) findViewById(R.id.checkBox1);
acc = (CheckBox) findViewById(R.id.checkBox2);
video = (CheckBox) findViewById(R.id.checkBox3);

/*
* GPS
*/
if (gps.isChecked()) {
if (recording) {
globalconstant.mlocManager
.removeUpdates(globalconstant.mlocListener);
button.setText("Start");
recording = false;
myChronometer.stop();
myChronometer.setBase(SystemClock.elapsedRealtime());
acc.setClickable(true);
video.setClickable(true);
myEditText.setFocusable(true);
gps.setChecked(false);
} else {
acc.setClickable(false);
video.setClickable(false);
myEditText.setFocusable(false);
myChronometer.setBase(SystemClock.elapsedRealtime());
myChronometer.start();
db.setVersion(1);
db.setLocale(Locale.getDefault());
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),Horizontal_Accuracy INTEGER,Altitude INTEGER,City TEXT,cur_timestamp TIMESTAMP);";
db.execSQL(gps);

addGPSListener();// meghívja a gps-t
recording = true;
button.setText("STOP");
}
}
/*
* Gyorsulásmérõ
*/
if (acc.isChecked()) {

if (recording) {
StopListenerAcc();
button.setText("Start");
recording = false;
myChronometer.stop();
myChronometer.setBase(SystemClock.elapsedRealtime());
gps.setClickable(true);
video.setClickable(true);
myEditText.setFocusable(true);
acc.setChecked(false);
} else {
gps.setClickable(false);
video.setClickable(false);
myEditText.setFocusable(false);
myChronometer.setBase(SystemClock.elapsedRealtime());
myChronometer.start();
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);

final String acc =

"CREATE TABLE IF NOT EXISTS Accelerometer ("

+ "id INTEGER PRIMARY KEY AUTOINCREMENT, X_Coordinate float(10, 6), Y_Coordinate float(10, 6), Z_Coordinate float(10, 6), cur_timestamp TIMESTAMP);";
db.execSQL(acc);

// Bekapcsolja az Accelerometer-t
addListenerAcc();
recording = true;
button.setText("STOP");
}
}

/*
* VideoRögzítés meghívása
*/
if (video.isChecked()) {
// kamera meghívása
Intent myIntent = new Intent(Festivale.this, record.class);
Festivale.this.startActivity(myIntent);
}

}

});

}

private void StopListenerAcc() {
// TODO Auto-generated method stub
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// add listener
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.unregisterListener(this);
}

// GPS
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 Altitude = (int) (loc.getAltitude());

String test = String.format("%.08f", szel);
String test2 = String.format("%.08f", hossz);

Geocoder geocoder = new Geocoder(Festivale.this,
Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(szel, hossz,
1);
city = addresses.get(0).getLocality();
// Toast.makeText(getApplicationContext(), city,
// Toast.LENGTH_SHORT)
// .show();
} catch (IOException e) {
e.printStackTrace();
}

ContentValues gps_values = new ContentValues();

gps_values.put("Latitude", test);
gps_values.put("Longitude", test2);
gps_values.put("Horizontal_Accuracy", horiAcc);
gps_values.put("Altitude", Altitude);
gps_values.put("City", city);

SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());

gps_values.put("cur_timestamp", dateFormat.format(date));

try {
db.beginTransaction();
db.insert("GPS_Values", null, gps_values);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}

// String Text = "My current location is: " + "Latitude = "
// + loc.getLatitude() + "\nLongitude = " + loc.getLongitude();

// Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
// .show();

}

protected void onPause() {
// super.onPause();
globalconstant.mlocManager
.removeUpdates(globalconstant.mlocListener);

}

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(
Festivale.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

private void addListenerAcc() {
// TODO Auto-generated method stub
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// add listener
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}

// Accelerometer
public void onSensorChanged(SensorEvent event) {

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

// assign directions
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];

// Toast.makeText(Festivale.this,
// "X: " + x + "\nY: " + y + "\nZ: " + z, Toast.LENGTH_LONG)
// .show();

ContentValues x_values = new ContentValues();

x_values.put("X_Coordinate", x);
x_values.put("Y_Coordinate", y);
x_values.put("Z_Coordinate", z);

SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());

x_values.put("cur_timestamp", dateFormat.format(date));

db.insert("Accelerometer", null, x_values);
}
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}
}

谢谢!

最佳答案

试试这个来注销你的加速度计怎么样(对我有用):

/**
* <b><i>public void useAccelerometer(boolean use)</i></b>
* <br>
* Since: API 1
* <br>
* <br>
* Set if you would like to enable the use of the accelerometer.
*
* @param use
* <br>
* True will enable the use of the accelerometer.
* <br>
* False will disable the use of the accelerometer.
*
*/

public void useAccelerometer(boolean use) {
if(use == true) {
manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
}
else {
manager.unregisterListener(this, accelerometer);
}
}

关于Android,加速度计不会停止不知道为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11772505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com