启动后无法关闭ShakeService.java

时间:2019-06-28 19:51:07

标签: java android

当开关打开时,ShakeService.java将启动并检测电话的晃动,但是当开关关闭时,我如何杀死该活动或停止该检测?

MainActivity.java

if (OnOffSwitch.isChecked()) {

                    //start shake detection
                    Intent intent = new Intent(MainActivity.this, ShakeService.class);
                    //Start Service
                    startService(intent);

                    OnOffSwitch.setText("Service is On");
                    Toast.makeText(MainActivity.this, "Service starts", Toast.LENGTH_SHORT).show();

                } else {
                    OnOffSwitch.setText("Service is Off");
                    Toast.makeText(MainActivity.this, "Service stopped", Toast.LENGTH_SHORT).show();
                }

ShakeService.java

package com.example.android.safetyapp;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import java.util.Random;

public class ShakeService extends Service implements SensorEventListener {

    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private float mAccel; // acceleration apart from gravity
    private float mAccelCurrent; // current acceleration including gravity
    private float mAccelLast; // last acceleration including gravity

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mAccelerometer,
                SensorManager.SENSOR_DELAY_UI, new Handler());
        return START_STICKY;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
        mAccelLast = mAccelCurrent;
        mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
        float delta = mAccelCurrent - mAccelLast;
        mAccel = mAccel * 0.9f + delta;
        // perform low-cut filter
        if (mAccel > 36) {
            Toast.makeText(ShakeService.this, "Shaking detected", Toast.LENGTH_SHORT).show();
            mAccel = 0;
        }
    }
}

我尝试通过在ShakeService.java中声明静态变量,然后通过在MainActivity中使用finish命令将其杀死来解决问题,但这对我不起作用,因此请指导我执行此操作。

0 个答案:

没有答案