简体   繁体   English

Android加速度传感器

[英]Android Accelerometer Sensor

I am trying to work with Accelerometer Sensor.我正在尝试使用加速度计传感器。 So i tried this example: http://blog.androgames.net/85/android-accelerometer-tutorial/所以我尝试了这个例子: 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.但是当我将 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));
    }

}

//this is my service when i change it, my error is hir public //这是我更改时的服务,我的错误是 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.感谢帮助。

The Above code had NULLPointerException in case of CONTEXT.在 CONTEXT 的情况下,上面的代码有NULLPointerException Thats why the application was crashing.这就是应用程序崩溃的原因。 While showing toast done use this.在显示吐司完成时使用这个。 Use getApplicationContext() .使用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); }

}

For CONTEXT try initializing it as对于 CONTEXT 尝试将其初始化为

this.getApplicationContext()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM