2012-08-02 37 views
1

任何人都可以幫助我爲什麼不加速度計nőt停止? 有unregisterListener行,但沒有。Android,加速度計不停止不知道爲什麼?

下面是代碼:

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: [email protected]\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 

} 
} 

THX!

+0

後這裏的相關代碼。沒有人會下載一個潛在的惡意文件,並通過整個事情。 – 2012-08-02 07:02:01

+0

這就是我所說的...我在移動不能插入代碼...如果你可以編輯我我會很高興 – David 2012-08-02 07:03:33

+0

等到你能夠自己編輯它。沒有冒犯,但我不打算下載文件。 – 2012-08-02 07:06:55

回答

1

如何嘗試這種加速度計註銷你(對我的作品):

/** 
    * <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); 
     } 
    } 
+0

好的明天嘗試它,但今天即時工作,直到晚上8點你的 – David 2012-08-02 07:28:24

+0

你在哪裏使用虛假或真實? – David 2012-08-02 19:09:39

+0

True將註冊偵聽器。 False將取消註冊偵聽器。 – 2012-08-02 19:11:56