Java 在Android中通过活动使用震动播放随机歌曲

Java 在Android中通过活动使用震动播放随机歌曲,java,android,android-activity,shake,Java,Android,Android Activity,Shake,我有一个震动监听器。在我的代码中有一组活动(歌曲),当检测到震动时,我想调用这些活动(歌曲)。我尝试从onShake方法调用intent/activity,但无法让它们工作。这是我的密码 package com.mellowbluestuff; import java.util.List; import com.example.android.searchabledict.R; import android.app.Activity; import android.app.Ale

我有一个震动监听器。在我的代码中有一组活动(歌曲),当检测到震动时,我想调用这些活动(歌曲)。我尝试从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);  
    };
}

“无法让它们工作”
非常模糊。具体来说,您遇到的问题是什么?当从onShake方法调用活动时,它会导致应用程序崩溃。我已经尝试了许多变化,使其功能正常,但他们已经造成崩溃。我知道onShake正在工作,因为我可以举杯祝酒,以显示我尝试调用的活动是独立的音乐播放器,在代码中标记为a到H。因此,如果播放一首歌曲,并且设备受到震动,那么该歌曲需要停止,另一首需要自己开始固定,无论如何,谢谢