Android Accelerometer Sensor

心不动则不痛 提交于 2019-12-02 19:01:37

问题


I am trying to work with Accelerometer Sensor. So i tried this example: http://blog.androgames.net/85/android-accelerometer-tutorial/

It work perfectly. But when i change AccelerometerManager activity to a service, it doesn't work and i got an error.

//this is the activity that i want change 
public class Accelerometer extends Activity
        implements AccelerometerListener {

    private static Context CONTEXT;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        CONTEXT = this;
    }

    protected void onResume() {
        super.onResume();
        if (AccelerometerManager.isSupported()) {
            AccelerometerManager.startListening(this);
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        if (AccelerometerManager.isListening()) {
            AccelerometerManager.stopListening();
        }

    }

    public static Context getContext() {
        return CONTEXT;
    }

    /**
     * onShake callback
     */
    public void onShake(float force) {
        Toast.makeText(this, "Phone shaked : " + force, 1000).show();
    }

    /**
     * onAccelerationChanged callback
     */
    public void onAccelerationChanged(float x, float y, float z) {
        ((TextView) findViewById(R.id.x)).setText(String.valueOf(x));
        ((TextView) findViewById(R.id.y)).setText(String.valueOf(y));
        ((TextView) findViewById(R.id.z)).setText(String.valueOf(z));
    }

}

//this is my service when i change it, my error is hir public

class Accelerometer extends Service implements AccelerometerListener{ private static Context CONTEXT;

@Override
public IBinder onBind(Intent intent) {
// TODO Put your code here
return null;
}

@Override
public void onCreate() {
System.out.println(”start listening”);
// if (AccelerometerManager.isSupported()) { AccelerometerManager.startListening(this);

// }
}

@Override
public void onDestroy() {
System.out.println(”start listening”);
// if (AccelerometerManager.isListening()) { AccelerometerManager.stopListening();
// }
}

public static Context getContext() {
return CONTEXT;
}

/**
* onShake callback
*/
public void onShake(float force) {
Toast.makeText(this, “Phone shaked niktilha omha ya 3ammi el7ag: ” + force, 1000).show(); }

/**
* onAccelerationChanged callback
*/
public void onAccelerationChanged(float x, float y, float z) { System.out.println(”x = “+x+” y = “+y+” z = “+z); }

}

Thanks for help.


回答1:


For CONTEXT try initializing it as

this.getApplicationContext()



回答2:


The Above code had NULLPointerException in case of CONTEXT. Thats why the application was crashing. While showing toast done use this. Use getApplicationContext(). Hope this will solve your problem.

Modified Code:

class Accelerometer extends Service implements AccelerometerListener{ 

@Override
public IBinder onBind(Intent intent) {
// TODO Put your code here
return null;
}

@Override
public void onCreate() {
System.out.println(”start listening”);
// if (AccelerometerManager.isSupported()) { AccelerometerManager.startListening(this);

// }
}

@Override
public void onDestroy() {
System.out.println(”stop listening”);
// if (AccelerometerManager.isListening()) { AccelerometerManager.stopListening();
// }
}

/**
* onShake callback
*/
public void onShake(float force) {
Toast.makeText(getApplicationContext(), “Phone shaked niktilha omha ya 3ammi el7ag: ” + String.valueOf(force), 1000).show(); }

/**
* onAccelerationChanged callback
*/
public void onAccelerationChanged(float x, float y, float z) { System.out.println(”x = “+x+” y = “+y+” z = “+z); }

}


来源:https://stackoverflow.com/questions/6608372/android-accelerometer-sensor

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