我想制作一个可以通过摇动手机掷骰子的Android应用程序。 我已经在android studio中设置了加速计传感器。 问题是,如何通过摇动手机使应用程序随机输入骰子,直到达到我之前设定的限制。
这是MainActivity.java
package com.example.pc.accelerometer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends Activity implements SensorEventListener{
private TextView xText, yText, zText;
private Sensor mySensor;
private SensorManager SM;
int ab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create our Sensor Manager
SM = (SensorManager)getSystemService(SENSOR_SERVICE);
// Accelerometer Sensor
mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// Register sensor Listener
SM.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);
// Assign TextView
xText = (TextView)findViewById(R.id.xText);
yText = (TextView)findViewById(R.id.yText);
zText = (TextView)findViewById(R.id.zText);
// yText.setText(ab);
}
@Override
protected void onResume() {
super.onResume();
SM.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Not in use
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.values[0]>3){
final int[] proper_noun = {1,2,3,4,5,6};
Random random = new Random();
int index = random.nextInt(proper_noun.length);
ab = proper_noun[index];
Intent i = new Intent(MainActivity.this, ShakeActivity.class);
Bundle b = new Bundle();
b.putInt("angka",ab);
i.putExtras(b);
startActivity(i);
SM.unregisterListener(this);
}
xText.setText("X: " + event.values[0]);
// yText.setText("Y: " + event.values[1]);
// zText.setText("Z: " + event.values[2]);
}
@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);
}
}
这是ShakeActivity.java
package com.example.pc.accelerometer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by PC-Parinussa on 6/26/2017.
*/
public class ShakeActivity extends Activity {
TextView namaTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shake_layout);
Bundle b = getIntent().getExtras();
int angka = b.getInt("angka");
namaTv = (TextView) findViewById(R.id.shakeID);
namaTv.setText("Random angka : "+angka);
}
}