Android加速计传感器

时间:2011-07-07 09:19:55

标签: android accelerometer sensor

我正在尝试使用Accelerometer传感器。所以我试过这个 例: http://blog.androgames.net/85/android-accelerometer-tutorial/

完美无缺。 但是当我将AccelerometerManager活动更改为服务时,它不起作用,我收到了错误。

//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));
    }

}

//当我更改它时,这是我的服务,我的错误是公开的

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); }

}

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

对于CONTEXT,请尝试将其初始化为

this.getApplicationContext()

答案 1 :(得分:0)

如果是CONTEXT,则上面的代码有NULLPointerException。这就是应用程序崩溃的原因。虽然显示吐司完成使用此。使用getApplicationContext()。希望这能解决你的问题。

修改后的代码:

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); }

}