0

So I am trying to create an App in Android Studios which vibrates depending on how far you have traveled.

I want to be able to start the app, and start the service, then it run in the background and still vibrate after 500 meters traveled, whether the screen is on or off.

I also want to be able to update the UI on the distance traveled.

The main thing that i want to figure out first is how to get it to vibrate when the app is not the main focus.

I have tried a Service, but I have seen there are restrictions on API 29 onwards which could potentially be the cause? (https://developer.android.com/guide/components/activities/background-starts)

When I run it on my phone, as soon as i minimize the app, or turn the screen off, the vibrating stops.

I am hoping someone can point me in the right direction on how to achieve vibration even after i turn the screen off.

This is a simplified code of the vibration problem

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button start, stop;

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

        start = findViewById(R.id.start);
        stop = findViewById(R.id.stop);

        start.setOnClickListener(this);
        stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view.equals(start)){
            startService(new Intent(this, BackGroundService.class));
        }else if (view.equals(stop)){
            stopService(new Intent(this, BackGroundService.class));
        }
    }
}

and the service

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.VibrationEffect;
import android.os.Vibrator;


import org.jetbrains.annotations.Nullable;

public class BackGroundService extends Service {
    private Vibrator vibrator;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        super.onStartCommand(intent,flags,startId);
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
         vibrator.vibrate(VibrationEffect.createOneShot(30000, VibrationEffect.DEFAULT_AMPLITUDE));
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

  • You can use Maps SDK for Android Utility Library and use SphericalUtil.computeDistanceBetween(lt_long_startPoint, lt_long_toPoint); You will get the distance and then use android.os.Vibrator to vibrate your phone. – Prashant.J Apr 11 '20 at 19:37
  • There are background service limitations in android 29 but you need to have a service running in the background which will calculate this distance. use foreground sticky notification for informing the user. – Prashant.J Apr 11 '20 at 19:51
  • Does this mean there is no way to vibrate the phone if if screen is off or the app is not in focus? I had set this all up working in the MainActivity, but when I created my Service, the vibrations would only occur if the app was the main focus – BilstroBraggins Apr 11 '20 at 21:13
  • You can make use of AlarmManager to wake your app after say some kms traveled from your service and then start vibrates service. – Prashant.J Apr 11 '20 at 21:25
  • 1
    What restrictions are you running into, specifically? To enable others to help you, describe the exact behavior you expected, as well as how that behavior differs from what is happening with your current implementation. Include the exact text of any error messages, (including, for any exceptions, the full [stack trace](https://stackoverflow.com/a/23353174) and which line of code is producing it). Please see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Ryan M Apr 12 '20 at 02:41
  • @RyanM I have edited the question with the example of the code i am having issues with and what i want to achieve. I don't seem to be getting any errors, so i think i may just be going about it all wrong, i am fairly new to android development and this is my first service i am trying to create. – BilstroBraggins Apr 12 '20 at 08:03
  • See https://stackoverflow.com/questions/47110489/background-service-for-android-oreo – Ryan M Apr 12 '20 at 08:30

0 Answers0