Guest User

Untitled

a guest
Jan 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. import android.app.Service;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.hardware.Sensor;
  5. import android.hardware.SensorEvent;
  6. import android.hardware.SensorEventListener;
  7. import android.hardware.SensorManager;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10. import android.widget.Toast;
  11.  
  12. public class GMeter extends Service implements SensorEventListener {
  13.  
  14. private float mLastX, mLastY, mLastZ;
  15. private boolean mInitialized;
  16. private SensorManager mSensorManager;
  17. private Sensor mAccelerometer;
  18. private final float NOISE = (float) 2.0;
  19.  
  20. /** Called when the activity is first created. */
  21. public void onCreate(Bundle savedInstanceState) {
  22. mInitialized = false;
  23. mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  24. mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  25. mSensorManager.registerListener(this, mAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
  26. Toast.makeText(this, "Starting Service", Toast.LENGTH_LONG).show();
  27. }
  28.  
  29. protected void onResume() {
  30. mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
  31. }
  32.  
  33. protected void onPause() {
  34. mSensorManager.unregisterListener(this);
  35. }
  36.  
  37. @Override
  38. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  39. // can be safely ignored for this demo
  40. }
  41.  
  42. @Override
  43. public void onSensorChanged(SensorEvent event) {
  44. float x = event.values[0];
  45. float y = event.values[1];
  46. float z = event.values[2];
  47. double a = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  48. double currentG = (a/ SensorManager.STANDARD_GRAVITY);
  49. Toast.makeText(this, "Calculating G's", Toast.LENGTH_LONG).show();
  50. if (!mInitialized && currentG > 1.0) {
  51. mLastX = x;
  52. mLastY = y;
  53. mLastZ = z;
  54. mInitialized = true;
  55. //Start Second Activity
  56. Intent intent = new Intent(getBaseContext(), thirdClass.class);
  57. intent.setAction(Intent.ACTION_VIEW);
  58. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  59. intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  60. getApplication().startActivity(intent);
  61. }else {
  62. float deltaX = Math.abs(mLastX - x);
  63. float deltaY = Math.abs(mLastY - y);
  64. float deltaZ = Math.abs(mLastZ - z);
  65. if (deltaX < NOISE) deltaX = (float)0.0;
  66. if (deltaY < NOISE) deltaY = (float)0.0;
  67. if (deltaZ < NOISE) deltaZ = (float)0.0;
  68. mLastX = x;
  69. mLastY = y;
  70. mLastZ = z;
  71. }
  72. }
  73.  
  74. @Override
  75. public IBinder onBind(Intent intent) {
  76. // TODO Auto-generated method stub
  77. return null;
  78. }
  79. }
  80.  
  81. @Override
  82. public int onStartCommand(Intent intent, int flags, int startId) {
  83. return START_STICKY;
  84. }
  85.  
  86. public void onCreate()
  87.  
  88. public class abc extends Service {
  89. Context mContext;
  90. @Override
  91. public void onCreate()
  92. {
  93. mContext=getApplicationContext();
  94. super.onCreate();
  95. }
  96.  
  97. @Override
  98. public int onStartCommand(Intent intent, int flags, int startId)
  99. {
  100.  
  101. return super.onStartCommand(intent, flags, startId);
  102. }
  103.  
  104. @Override
  105. public IBinder onBind(Intent intent)
  106. {
  107. // TODO Auto-generated method stub
  108. return null;
  109. }
Add Comment
Please, Sign In to add comment