我实施了一个震撼听众。我的代码中有一组活动(歌曲),我想在检测到震动时调用它们。我尝试从onShake方法调用intent / activity但无法让它们工作。这是我的代码......
package com.mellowbluestuff;
import java.util.List;
import com.example.android.searchabledict.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import java.util.List;
import com.mellowbluestuff.*;
import com.mellowbluestuff.ShakeDetector.OnShakeListener;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.FloatMath;
public class Music extends Activity {
// The following are used for the shake detection
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeDetector mShakeDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music);
// ShakeDetector initialization
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mShakeDetector = new ShakeDetector();
mShakeDetector.setOnShakeListener(new OnShakeListener() {
@Override
public void onShake(int count) {
// Create notification if the device is shaken by user
Toast.makeText(getApplicationContext(), "You've shooken the fookin phone", Toast.LENGTH_LONG).show();
// play a random song from the music players below
}
});
}
@Override
public void onResume() {
super.onResume();
// Add the following line to register the Session Manager Listener onResume
mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onPause() {
// Add the following line to unregister the Sensor Manager onPause
mSensorManager.unregisterListener(mShakeDetector);
super.onPause();
}
// MUSIC PLAYERS //
// begin MusicPlayerA by calling as an intent
public void MusicPlayer1 (View view) {
Intent m = new Intent (this, MusicPlayerA.class);
startActivity(m);
}
// begin MusicPlayerB by calling as an intent
public void MusicPlayer2 (View view) {
Intent m2 = new Intent (this, MusicPlayerB.class);
startActivity(m2);
}
// begin MusicPlayerC by calling as an intent
public void MusicPlayer3 (View view) {
Intent m3 = new Intent (this, MusicPlayerC.class);
startActivity(m3);
}
// begin MusicPlayerD by calling as an intent
public void MusicPlayer4 (View view) {
Intent m4 = new Intent (this, MusicPlayerD.class);
startActivity(m4);
}
// begin MusicPlayerE by calling as an intent
public void MusicPlayer5 (View view) {
Intent m5 = new Intent (this, MusicPlayerE.class);
startActivity(m5);
}
// begin MusicPlayerF by calling as an intent
public void MusicPlayer6 (View view) {
Intent m6 = new Intent (this, MusicPlayerF.class);
startActivity(m6);
}
// begin MusicPlayerG by calling as an intent
public void MusicPlayer7 (View view) {
Intent m7 = new Intent (this, MusicPlayerG.class);
startActivity(m7);
}
// begin MusicPlayerH by calling as an intent
public void MusicPlayer8 (View view) {
Intent m8 = new Intent (this, MusicPlayerH.class);
startActivity(m8);
}
//handler for back button
public void BackButton (View view) {
MediaPlayer mMediaPlayer = MediaPlayer.create(this, R.raw.soundbackbutton) ;
mMediaPlayer.start();
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(200);
Intent s = new Intent (this, Main.class);
startActivity(s);
}
@Override
public void onBackPressed() {
MediaPlayer mMediaPlayer = MediaPlayer.create(this, R.raw.soundbackbutton) ;
mMediaPlayer.start();
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(200);
Intent btm = new Intent (this, Main.class);
startActivity(btm);
};
}