I'm a newbie to this field of android development. I'm developing an app which I need to be launched by shaking the device. How can I get this thing done? I read many threads and tried out several codes. but non of them worked. Please be kind enough to come up with the full code (from top to bottom) of particular file(or files). So that I'll be able to understand where exactly that I need to change in my code. Thank you!
Asked
Active
Viewed 707 times
-2
-
1what have you done? post some code here – Divyesh Patel Apr 27 '17 at 11:13
-
1If I did it, I'd implement a service, which was working all the time on the device, launched by BOOT_COMPLETE, and in the service I'd listen to accelometer data, determine the *shake*. Then just starting Activity when it is determined – Vladyslav Matviienko Apr 27 '17 at 11:15
-
"I read many threads and tried out several codes. but non of them worked" -- then provide a [mcve] demonstrating exactly what you tried and explain exactly what did not work. "Please be kind enough to come up with the full code (from top to bottom) of particular file(or files)" -- that is not how Stack Overflow works. If you want custom code written for you, hire somebody. – CommonsWare Apr 27 '17 at 11:39
-
@CommonsWare I'm sorry You have misunderstood me completely. As I said earlier I'm a newbie to this field of Android developement. There are times when people come up with answers saying different phrases. For example if u take a look at the first answer below, It says that "Now use onSensorChanged to get the reading". My knowledge is not good enough to understand where EXACTLY I need put this thing in my code. Whether I should place it in MainActivity or ShakeService? If it's MainActivity, then where exactly in it? Inside of OnCreate() method or after that? I dont get any of those. Im stil – Sankha Rathnayake Apr 27 '17 at 11:54
-
"My knowledge is not good enough to understand where EXACTLY I need put this thing in my code" -- that is fine. However, the proper way to handle this is to post a comment on a question, asking for clarification, if and when an answer gets posted that you do not completely understand. – CommonsWare Apr 27 '17 at 11:56
-
@CommonsWare I'm sorry You have misunderstood me completely. As I said earlier I'm a newbie to this field of Android developement. There are times when people come up with answers saying different phrases. For example if u take a look at the first answer below, It says that "Now use onSensorChanged to get the reading". My knowledge is not good enough to understand where EXACTLY I need put this thing in my code. Whether I should place it in MainActivity or ShakeService? If it's MainActivity, Where exactly in main activity?. Im still learning all these. That's why i'm asking for the full code – Sankha Rathnayake Apr 27 '17 at 11:57
-
"That's why I'm asking to come up with the full code" -- that is not our job. That is your job. Or, you can hire somebody to "write the full code". We are volunteers, here to answer questions. If this project is too complicated for you, start with some other project, and return to this one in the future. – CommonsWare Apr 27 '17 at 11:59
1 Answers
2
Try this, First create your Service
public class ShakeService extends Service implements SensorEventListener {
private SensorManager sensorMgr;
private Sensor acc;
private long lastUpdate = -1;
private float x, y, z;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 1100;
@Override
public void onCreate() {
Toast.makeText(this,
"Service Started", Toast.LENGTH_SHORT).show();
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
acc=sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
boolean accelSupported= sensorMgr.registerListener((SensorEventListener) this, acc, SensorManager.SENSOR_DELAY_GAME);
long curTime11 = System.currentTimeMillis();
if (!accelSupported) {
// on accelerometer on this device
sensorMgr.unregisterListener((SensorEventListener) this,acc);
}
super.onCreate();
}
protected void onPause() {
if (sensorMgr != null) {
sensorMgr.unregisterListener((SensorEventListener) this,acc);
sensorMgr = null;
}
return;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
if (sensorMgr != null) {
sensorMgr.unregisterListener((SensorEventListener) this,acc);
sensorMgr = null;
}
stopSelf();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
// only allow one update every 100ms.
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
x = sensorEvent.values[SensorManager.DATA_X];
y = sensorEvent.values[SensorManager.DATA_Y];
z = sensorEvent.values[SensorManager.DATA_Z];
float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;
if (speed > SHAKE_THRESHOLD) {
Log.d("sensor", "shake detected w/ speed: " + speed);
Toast.makeText(this, "shake detected w/ speed: " + speed, Toast.LENGTH_SHORT).show();
Intent myIntent= new Intent(this, MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
/ startActivity(myIntent);
////Here start your activity and your application will be started
}
last_x = x;
last_y = y;
last_z = z;
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
And make sure to declare your service in manifest
<service
android:name=".ShakeService"
android:enabled="true"
android:exported="true"></service>
Now in your activity start service as
startService(new Intent(MainActivity.this,ShakeService.class));

Abdul Kawee
- 2,687
- 1
- 14
- 26
-
`ShakeActivity extends Service` looks strange, but in general answer is good enough – Vladyslav Matviienko Apr 27 '17 at 11:26
-
Actually i had code with activity , for question just changed it for service so name can be ignored :) – Abdul Kawee Apr 27 '17 at 11:28
-
1
-
you will place it out of onStartCommand() method ,in Service class – Abdul Kawee Apr 27 '17 at 12:13
-
@AbdulKawee As I'm a newbie to this, Can you please tell me where I need to put this "onSensorChanged" method?. In what file? In between what lines? – Sankha Rathnayake Apr 27 '17 at 12:15
-
Just told you in above comment, After the onStartCommand method of service – Abdul Kawee Apr 27 '17 at 12:17
-
"//Here start your activity and your application will be started." How do I start the activity? What's the code?? – Sankha Rathnayake Apr 27 '17 at 12:17
-
Now check I have edited my answer, You have to setFlags for starting activity from service – Abdul Kawee Apr 27 '17 at 12:20
-
I get many errors. These are the ones that have become red - sensorMgr, lastUpdate,z,last_x, last_y, last_z. Im so confused. Can u please post the whole Service class here? – Sankha Rathnayake Apr 27 '17 at 12:33
-
If this answer helped you, dont forget to accept it and rate it up – Abdul Kawee Apr 27 '17 at 12:53
-
There's a red line under "public class ShakeService extends Service implements SensorListener". error is "Class "ShakeService" must either be declared abstract or implement abstract method'onBind(intent) in "service" ' What should I do for this? – Sankha Rathnayake Apr 27 '17 at 13:22
-
They say that the SENSOR_ACCELEROMETER, registerListener, DATA_X,DATA_Y and DATA_Z are depricated. onStartCommand remains unused. how do i fix these? – Sankha Rathnayake Apr 27 '17 at 13:25
-
-
@SankhaRathnayake Check i have edited my answer and posted a complete code, its working fine, if it helped dont forget to accept the asnwer and rate it up – Abdul Kawee Apr 28 '17 at 04:39
-
@AbdulKawee Thanks alot for posting the complete code. I really appreciate it. Now they don't show any error. But i cant launch the app by shaking. I have to put that "startService(new Intent(MainActivity.this,ShakeService.class));" in onCreate method right? – Sankha Rathnayake Apr 28 '17 at 15:27
-
-
So what might be the possible reason for app to not get started by shaking? onPause method remains unused anyway – Sankha Rathnayake Apr 29 '17 at 02:42
-
-
Yep. My Current app where I entered all these codes given by u above. – Sankha Rathnayake May 01 '17 at 14:56
-
Yes, it should open your current app,as i have tested this code. It is opening my activity. Make sure your service is runing, and the activity you want to open is declared in manifest – Abdul Kawee May 02 '17 at 04:51