好吧,我已经和我斗争了一个星期左右,但我确信这是一个简单的解决方案。我是Android开发的新手,并且正在使用相当多的共享代码,所以我可能会遗漏一些东西。这是一个简单的应用程序,可以解析RealClearPolitics.com的新闻标题。它在一个按钮上执行就好了,所以我知道解析后端,新文本显示工作正常。但是,当我尝试使用相同类型的设置进行摇动时,它无法正常工作。我相信问题出在我的MainActivity类和ShakeEventListener之间的握手中,或者我是如何执行ShakeEventListener的。我尽可能地遵循在线指示,但我被卡住了。有什么指针吗?这是我的完整代码供参考,怀疑问题被------。
阻止MainActivity:
package com.example.gregoriovasquez.rcpparse;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
import android.widget.TextView;
import static android.view.View.*;
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button button_name;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeEventListener mShakeEventListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//button properties
button_name= (Button) findViewById(R.id.button1);
button_name.setOnClickListener(MainActivity.this);
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
** --------------------------------------------- -------------------------------
* // ShakeDetector初始化
mSensorManager =(SensorManager)getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mShakeEventListener = new ShakeEventListener();
mShakeEventListener.setOnShakeListener(new ShakeEventListener.OnShakeListener(){
@Override
public void onShake(int count) {
handleShakeEvent(count)
});*
}
public void handleShakeEvent(int count){
TextView rootView = (TextView) findViewById(R.id.mytextView);
rootView.setText(TextMatcher.PatternReturn());
}
** --------------------------------------------- ------------------------------
@覆盖
public void onClick(查看v){
TextView rootView =(TextView)findViewById(R.id.mytextView);
rootView.setText(TextMatcher.PatternReturn());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mShakeEventListener, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(mShakeEventListener);
super.onPause();
}
}
ShakeEventListener:
package com.example.gregoriovasquez.rcpparse;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.FloatMath;
public class ShakeEventListener implements SensorEventListener {
private static final float SHAKE_THRESHOLD_GRAVITY = 2.7F;
private static final int SHAKE_SLOP_TIME_MS = 500;
private static final int SHAKE_COUNT_RESET_TIME_MS = 3000;
private OnShakeListener mListener;
private long mShakeTimestamp;
private int mShakeCount;
public void setOnShakeListener(OnShakeListener listener) {
this.mListener = listener;
}
public interface OnShakeListener {
public void onShake(int count);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// ignore
}
@Override
public void onSensorChanged(SensorEvent event) {
if (mListener != null) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
float gX = x / SensorManager.GRAVITY_EARTH;
float gY = y / SensorManager.GRAVITY_EARTH;
float gZ = z / SensorManager.GRAVITY_EARTH;
// gForce will be close to 1 when there is no movement.
float gForce = FloatMath.sqrt(gX * gX + gY * gY + gZ * gZ);
if (gForce > SHAKE_THRESHOLD_GRAVITY) {
final long now = System.currentTimeMillis();
// ignore shake events close to each other
if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) {
return;
}
// reset the shake count after no shakes
if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) {
mShakeCount = 0;
}
mShakeTimestamp = now;
mShakeCount++;
mListener.onShake(mShakeCount);
}
}
}
}
以下在我的清单中:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />