From some days i am trying but still all in vain.
i created accelerometer in service and i want to store the acceleromete values for sometime and then unregister.
so i used pendingintent so that will call again.
so therefor i request that how to store the values for at least 10 second and then unregiester.
the follwing is the java;
public class MyService extends Service implements LocationListener
,SensorEventListener {
SensorManager sensor;
Sensor accelerometer;
// private FileWriter writer;
StringBuilder sr;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
sensor = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sensor.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensor.registerListener(
this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
return START_STICKY;
}
@Override
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
try {
float x = event.values[0];
float y = event.values[1];
double z = event.values[2] - 9.8;
sr.append("" + x).toString();
Toast.makeText(getBaseContext(), sr, Toast.LENGTH_LONG).show();
wait(10000);
} catch (Exception e) {
//sensor.unregisterListener(this);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
i used synchronized but cann't work . please any once can help; I stuck in this if this solve then my final year project will complet.
There are many ways to handle timer events in android, but I recommend using HANDLER with RUNNABLE.
`
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
/* Any code you put here will run after 10 seconds, So you can unregister the sensor here */
handler.postDelayed(runnable, 10000);// 10000 milliseconds = 10 seconds.
}
};
`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.