Unable to resolve getApplicationContext and startActivity methods in shake event listener: Android Studio

血红的双手。 提交于 2019-12-24 17:21:43

问题


I am trying to implement a shake event listener and below is my code. It is Android Studio environment if it matters. It just says cannot resolve method getApplicationContext or even startActivity. Can anyone help me find my mistake?

package com.shaivaldesai.shake;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

/**
 * Created by Shaival Desai on 7/3/14.
 */
public class ShakeEventListener implements SensorEventListener {


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

    }

    // Low pass filter
    private float calcMaxAcceleration(SensorEvent sensorEvent) {

        final float alpha = 0.8f;

        float[] gravity = {-9.81f};

        gravity[0] = alpha * gravity[0] + (1 - alpha) * sensorEvent.values[0];
        gravity[1] = alpha * gravity[1] + (1 - alpha) * sensorEvent.values[1];
        gravity[2] = alpha * gravity[2] + (1 - alpha) * sensorEvent.values[2];

        float accX = sensorEvent.values[0] - gravity[0];
        float accY = sensorEvent.values[1] - gravity[1];
        float accZ = sensorEvent.values[2] - gravity[2];

        float max1 = Math.max(accX, accY);
        return Math.max(max1, accZ);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        //handleShake(sensorEvent);}
    float maxAcc = calcMaxAcceleration(sensorEvent);
       // void handleShake(sensorEvent){
        //float maxAcc = calcMaxAcceleration(sensorEvent);
        if(maxAcc>=15) {

        Intent intent = new Intent(getApplicationContext(),Main.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }
}

回答1:


Those methods don't exist in SensorEventListener. You must have copied this from a class that extends Activity. You'll need to call these methods on a Context object. You should define a custom constructor on your ShakeEventListener that takes a Context object:

public class ShakeEventListener implements SensorEventListener {
    private Context mContext;

    public ShakeEventListener(Context context) {
        mContext = context;
    }

    /** omitting unchanged methods **/

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        //handleShake(sensorEvent);}
        float maxAcc = calcMaxAcceleration(sensorEvent);
        // void handleShake(sensorEvent){
        //float maxAcc = calcMaxAcceleration(sensorEvent);
        if(maxAcc>=15) {

            Intent intent = new Intent(mContext, Main.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        }
    }
}


来源:https://stackoverflow.com/questions/24660068/unable-to-resolve-getapplicationcontext-and-startactivity-methods-in-shake-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!