Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3606442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:14:56+00:00 2026-05-18T21:14:56+00:00

This is a pretty strange problem. I have a book that I have been

  • 0

This is a pretty strange problem. I have a book that I have been going through (The Android Developer’s Cookbook)

I am making a pretty basic app (at least, I think it is basic)

The problem I am having is this. When the service I create is stopped, the activity does the service’s work.

I want to monitor the Z Axis reading on my phone (in the background, always)

Here is my project structure:
TiltMonitorActivity
TiltMonitorService

From TiltMonitorActivity:

@Override
        public void onClick(View v) {

            boolean error = false;
            if (tbService.isChecked()) {                    
                try {
                    startService(backgroundService);
                }

                catch (Exception e) {                          
                       error = true;
                }
                finally {
                    if (error)
                    {
                        errorToast();
                    }
                }
            }
            else if (!tbService.isChecked()) {
                try {
                    stopService(backgroundService);
                }

                catch (Exception e){                           
                       error = true;
                }
                finally {
                    if (error)
                    {
                        errorToast();
                    }
                }

            }
        }
});

I also put this in onBackPressed(), onPause, onDestroy

finish();

The intent I create when starting/stopping service

final Intent backgroundService = new Intent(this, TiltMonitorService.class);

From TiltMonitorService:

  import android.app.Service;
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.IBinder;
import android.os.Vibrator;
import android.widget.Toast;

public class TiltMonitorService extends Service {

    private SensorManager sensorManager = null;

    private Vibrator vibrator = null;

    private float[] accData = new float[3];

    // These are for manual config
    private float FORWARD_THRESHOLD = 5f;
    private float BACKWARD_THRESHOLD = -5f;

    // These are for auto config

    // The phone will face forward, so a positive Z Axis means user is leaning backwards
    // and that a negative Z Axis the user is leaning forward
    private float AUTO_FORWARD_THRESHOLD = -1.5f;
    private float AUTO_BACKWARD_THRESHOLD = 3.5f;

    public static TiltMonitorActivity MAIN_ACTIVITY = null;

    public static void setMainActivity(TiltMonitorActivity activity)
    {
        MAIN_ACTIVITY = activity;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);

        sensorManager.registerListener( tiltListener, 
                                        sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
                                        SensorManager.SENSOR_DELAY_NORMAL);

        Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
    }

    private SensorEventListener tiltListener = new SensorEventListener() {

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            accData[0] = event.values[0];
            accData[1] = event.values[1];
            accData[2] = event.values[2];

            checkData(accData);

        }

    };

    private void checkData(float[] accData)
    {
        if ((accData[2] < AUTO_FORWARD_THRESHOLD) || (accData[2] > AUTO_BACKWARD_THRESHOLD))
        {
            vibrator.vibrate(100);
        }

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

    }



}

Again, I get the Toast that the service has been started and stopped when hitting the ToggleButton in the activity.

But the task still happens. (Vibrates when below or above set thresholds)

When I open up a task killer, the service isn’t running. Only the activity is (and killing it kills the vibration)

I’m not sure what words to search for, I couldn’t find any one else with the same problem. I tried to post only relevant code in the Activity to avoid clutter. The service is posted in its entirety. If more is needed, I will put it up promptly.

Thanks you guys for any insight given

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:14:56+00:00Added an answer on May 18, 2026 at 9:14 pm

    at a guess, because you register the listener in onCreate without unregistering it in onDestroy

    in the service, that is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is pretty weird. I have my Profiler open and it obviously shows that
So this seems pretty basic but I can't get it to work. I have
I'm facing this strange problem. I'm trying to read a file that is located
I've got a strange problem here. Assume that I have a class with some
I have a pretty strange 'assembly' of an HTML file going on, with <script>
This is pretty trivial, but I noticed on SO that instead of an offset
I think this is pretty typical, you have the same website project with an
This is a pretty straight forward attempt. I haven't been using python for too
Hallo everybody, I'm dealing with a pretty strange situation here. I have developed a
I have strange problem with the delay function here using the HTML function with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.