import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.os.Vibrator; import android.support.v4.app.NotificationCompat; import android.widget.TextView; import android.widget.Toast; public class AndroidAccelerometerExample extends Activity { private float lastX, lastY, lastZ; private float deltaX_timer, deltaY_timer, deltaZ_timer; private int num=5*60*1000; // 5 minutes private SensorManager sensorManager; private Sensor accelerometer; private float deltaXMax = 0; private float deltaYMax = 0; private float deltaZMax = 0; private float deltaX = 0; private float deltaY = 0; private float deltaZ = 0; private String s; private TextView currentX, currentY, currentZ, maxX, maxY, maxZ,stop_txt,run_txt; public Vibrator v; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stop_txt=(TextView)findViewById(R.id.textView_stop); run_txt=(TextView)findViewById(R.id.textView_run); currentX = (TextView) findViewById(R.id.currentX); currentY = (TextView) findViewById(R.id.currentY); currentZ = (TextView) findViewById(R.id.currentZ); maxX = (TextView) findViewById(R.id.maxX); maxY = (TextView) findViewById(R.id.maxY); maxZ = (TextView) findViewById(R.id.maxZ); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //counter down final Handler handler = new Handler(); handler.post(new Runnable() { @Override public void run() { int seconds = (int) (num / 1000) % 60 ; int minutes = (int) ((num / (1000*60)) % 60); stop_txt.setText(Integer.toString(minutes) + " : " + Integer.toString(seconds)); if(num == 0){ stop_txt.setText("00 : 00"); handler.removeCallbacks(this); } num -= 1000; handler.postDelayed(this, 1000); stop_or_run(); s=(num+""); notification(); } }); } //onResume() register the accelerometer for listening the events protected void onResume() { super.onResume(); sensorManager.registerListener(mAccListener, accelerometer, SensorManager.SENSOR_DELAY_UI); Toast.makeText(getApplicationContext(), s+" onResume", Toast.LENGTH_SHORT).show(); } //onPause() unregister the accelerometer for stop listening the events protected void onPause() { super.onPause(); sensorManager.registerListener(mAccListener, accelerometer, SensorManager.SENSOR_DELAY_UI); stop_or_run(); Toast.makeText(getApplicationContext(), s+" onPuase", Toast.LENGTH_SHORT).show(); } protected void onDestroy() { super.onDestroy(); sensorManager.registerListener(mAccListener, accelerometer, SensorManager.SENSOR_DELAY_UI); stop_or_run(); s=(deltaX_timer+""); Toast.makeText(getApplicationContext(), s+" onDestroy", Toast.LENGTH_SHORT).show(); } public void notification(){ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) // notification icon .setContentTitle("Notification!") // title for notification .setContentText(s) // message for notification .setAutoCancel(true); // clear notification after click Intent intent = new Intent(this, AndroidAccelerometerExample.class); PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK); mBuilder.setContentIntent(pi); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mBuilder.setOngoing(true); mNotificationManager.notify(0, mBuilder.build()); } public void stop_or_run(){ // تشخيص اينکه گوشي ثابت است يا متحرک if(deltaX_timer==deltaX & deltaY_timer==deltaY & deltaZ_timer==deltaY ){ run_txt.setText("Stop Mobil"); s="stop "; }else{ run_txt.setText("Run Mobil"); num=5*60*1000; // 5 minutes s="run "; } return; } private SensorEventListener mAccListener = new SensorEventListener() { @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { // clean current values currentX.setText("0.0"); currentY.setText("0.0"); currentZ.setText("0.0"); // display the current x,y,z accelerometer values currentX.setText(Float.toString(deltaX)); currentY.setText(Float.toString(deltaY)); currentZ.setText(Float.toString(deltaZ)); // display the max x,y,z accelerometer values if (deltaX > deltaXMax) { deltaXMax = deltaX; maxX.setText(Float.toString(deltaXMax)); } if (deltaY > deltaYMax) { deltaYMax = deltaY; maxY.setText(Float.toString(deltaYMax)); } if (deltaZ > deltaZMax) { deltaZMax = deltaZ; maxZ.setText(Float.toString(deltaZMax)); } // get the change of the x,y,z values of the accelerometer deltaX = Math.abs(lastX - event.values[0]); deltaY = Math.abs(lastY - event.values[1]); deltaZ = Math.abs(lastZ - event.values[2]); // if the change is below 2, it is just plain noise if (deltaX < 0.7) deltaX = 0; if (deltaY < 0.7) deltaY = 0; if (deltaZ < 0.7) deltaZ = 0; // set the last know values of x,y,z lastX = event.values[0]; lastY = event.values[1]; lastZ = event.values[2]; // get the change of the x,y,z values of the accelerometer deltaX_timer = Math.abs(lastX - event.values[0]); deltaY_timer = Math.abs(lastY - event.values[1]); deltaZ_timer = Math.abs(lastZ - event.values[2]); stop_or_run(); } }; }