-2

i want to make an app to shake a device and it will detect it . there is a shakedetecter filoe and also service file but it work only when the app opened can u please help me

public class Main extends AppCompatActivity {

    // The following are used for the shake detection
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.praclay);

        Intent intent = new Intent(Praclass.this, ShakeService.class);
        startService(intent);

        // ShakeDetector initialization
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mShakeDetector = new ShakeDetector();
        mShakeDetector.setOnShakeListener(new ShakeDetector.OnShakeListener() {

            @Override
            public void onShake(int count) {
                Toast.makeText(getApplicationContext(), "shake",Toast.LENGTH_SHORT).show();
            }
        });
    }
tommybee
  • 2,409
  • 1
  • 20
  • 23
user3629
  • 57
  • 8

2 Answers2

0

As you have mentioned your shake detection is working when app is in Foreground, Also it seems that you are aware that to run process or task when app is in background you have to write a Service.

Be sure you are not stopping your service when app goes in background.

One very important thing start your service as Foreground service, Go through https://developer.android.com/guide/components/services.html#Foreground

This will be required for service communication in case of device lock because Doze mode of latest devices.

0

It should be the the same problem as mention in this post. unable to detect shake event when my phones screen is off in android Or you have to Check this post for some reading about your issue. Android accelerometer not working when screen is turned off it might be the same issue with your sensor. You can use wakeLock so that device screen not off but its battery drainage process.

Here is the official link for making foreground service https://developer.android.com/guide/components/services.html#Foreground

Complete tutorial for starting foreground service in android http://www.truiton.com/2014/10/android-foreground-service-example/

Sardar Khan
  • 845
  • 6
  • 16
  • actually i refer this https://stackoverflow.com/questions/38882313/android-sensor-shake-in-the-background. check out the last comment of julia . she got its solution but i do not understand . can check out her last comment and explain that. – user3629 Oct 04 '17 at 15:06
  • She says that she register her listener in the service class that service runs always in the background for listen the shake of the device..you can register the listener in service and listen for shake.. – Sardar Khan Oct 05 '17 at 04:08
  • i tried it i works but not properly .mean it works only that condition when first i open my app and than off than locked my phone than shaking my phone it detect the shaking event but after spending some time it does not work , not detect shaking event . means . – user3629 Oct 06 '17 at 08:45
  • have you checked that second time your service is running or not..?? – Sardar Khan Oct 06 '17 at 09:42
  • why service is not running have you make it foreground service..? – Sardar Khan Oct 06 '17 at 10:31
  • check that answer is updated with how to start foreground service that continously on background.. – Sardar Khan Oct 06 '17 at 10:39
  • is this answer helpful to you.? – Sardar Khan Oct 17 '17 at 13:09