馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
本帖最後由 wind703235 於 2013-6-19 10:05 編輯
想寫一個搖動手機 然後能跳出今日運勢的程式
可是我搖動沒反應...
求解 目前程式碼如下
package com.example.sensor;
import java.util.ArrayList;
import java.util.List;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.WindowManager;
import android.util.FloatMath;
public class MainActivity extends Activity implements SensorEventListener{
private String god;
private int rnd;
private long initTime = 0;
private long lastTime = 0;
private long curTime = 0;
private long duration = 0;
private float last_x = 0.0f,last_y = 0.0f,last_z = 0.0f;
private float shake = 0.0f;
private int TimeInterval = 100;
private boolean isRecoding = false;
private int shakeThreshold = 3000;
private SensorManager sensorManager;
private ArrayList<OnShakeListener> mListeners;
public void ShakeInterface(Context context){
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mListeners = new ArrayList<OnShakeListener>();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public interface OnShakeListener {
void onShake();
}
public void registerOnShakeListener(OnShakeListener listener) {
if (mListeners.contains(listener))
return;
mListeners.add(listener);
}
public void unregisterOnShakeListener(OnShakeListener listener) {
mListeners.remove(listener);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@SuppressLint("FloatMath")
public void onSensorChanged(SensorEvent event) {
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
curTime = System.currentTimeMillis();
if(!isRecoding){
if ((curTime - lastTime) > TimeInterval) {
duration = (curTime - lastTime);
if (last_x == 0.0f && last_y == 0.0f && last_z == 0.0f) {
initTime = System.currentTimeMillis();
} else {
shake = FloatMath.sqrt((x - last_x)*(x - last_x)+(y - last_y)*(y - last_y)+(z - last_z)*(z - last_z))/duration*10000;
}
System.out.println(shake);
if(shake >= shakeThreshold){
this.notifyListeners();
}
last_x = x;
last_y = y;
last_z = z;
lastTime = curTime;
}
}
}
private void notifyListeners() {
rnd = (int)(Math.random() * 5);
if(rnd==0){
god="大吉。\n今日會事事順心喔!";
}else if (rnd==1){
god="中吉。\n今天偏財運或許不錯喔!";
}else if (rnd==2){
god="小吉。\n今日會是美好的一天。";
}else if (rnd==3){
god="小凶。\n今天也許會有不少挫折,加油!";
}else if (rnd==4){
god="中凶。\n今日切記小心行事。";
}else if (rnd==5){
god="大凶。\n今天要注意血光之災喔!";
}
new AlertDialog.Builder(MainActivity.this)
.setTitle("結果")
.setMessage(god)
.setNegativeButton("再試一次", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
isRecoding = false;
}
})
.setPositiveButton("離開", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
})
.show();
isRecoding = true;
}
}
不知道哪裡有問題... |