0

我有两个活动 MainActivity 和 StopAlarm 并且在移动设备上我想启动 StopAlarm 活动。但是当我的 MainActivity 处于最小化模式(背景)并且我摇动手机时,StopAlarm 活动也以最小化状态启动。我想在最大化开始时启动 StopAlarm 活动。(活动应该在屏幕上)

    import android.app.Activity;
    import android.app.Application;
    import android.content.Context;
    import android.content.Intent;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.Spinner;
    import android.widget.Toast;

public class MainActivity extends Activity {


    Intent in;
    private Button activate;
    private SensorManager mSensorManager;
    private float mAccel; 
    private float mAccelCurrent; 
    private float mAccelLast; 
    Spinner sspinner, tspinner;
    String selectedTone, selectedTime;
    String ab;
    public int settime;
    public static int i;
    int act = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] sdata = { "Beep", "Police Siren", "Dog Bark" };
        String[] tdata = { "05 sec", "10 sec", "15 sec" };
    //  final activate activteprocess = new activate();

        sspinner = (Spinner) findViewById(R.id.soundspinner);
        sspinner.setAdapter(new ArrayAdapter<String>(this,
                R.layout.customspinner, R.id.textView1, sdata));

        tspinner = (Spinner) findViewById(R.id.timespinner);
        tspinner.setAdapter(new ArrayAdapter<String>(this,
                R.layout.customspinner, R.id.textView1, tdata));
        // ///////////////////////////////////////////////////////////////////////////////////////
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;
        activate = (Button) findViewById(R.id.button1);
        activate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                selectedTime = tspinner.getSelectedItem().toString();
                ab = selectedTime.substring(0, 2).trim();
                settime = Integer.parseInt(ab);
                Toast.makeText(getApplicationContext(), "" + settime, 1500)
                        .show();
                selectedTone = sspinner.getSelectedItem().toString();
                act = 1;
            //  activteprocess.start();

            }
        });
        // /////////////////////////////////////////////////////////////////////////////////////////////////////////

    }

    public final SensorEventListener mSensorListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent se) {

            float x = se.values[0];
            float y = se.values[1];
            float z = se.values[2];
            mAccelLast = mAccelCurrent;
            mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
            float delta = mAccelCurrent - mAccelLast;

            if (delta > 2 && act == 1) {
                mAccel = mAccel * 0.9f + delta;
                if (i == 0) {
                    i++;
                    in = new Intent(getApplicationContext(),
                            StopAlarm.class);
                    in.putExtra("tone", selectedTone);
                //   
                        startActivity(in);
                    //finish();
                }
            }
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

停止警报.java

public class StopAlarm extends Activity {

    String tonetoplay;
    MediaPlayer mp;
    int[] ab = { R.raw.dog, R.raw.police_siren, R.raw.thief_alarm };
    Button stp;
    int done = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stopalarm);
        Intent abc = getIntent();
        tonetoplay = abc.getStringExtra("tone");

        startPlaying();
        stp = (Button) findViewById(R.id.stopalaram);
        stp.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.stop();
            }
        });

    }

    private void startPlaying() {

        if (tonetoplay.equals("Beep"))
            mp = MediaPlayer.create(getApplicationContext(), ab[2]);
        if (tonetoplay.equals("Police Siren"))
            mp = MediaPlayer.create(getApplicationContext(), ab[1]);
        if (tonetoplay.equals("Dog Bark"))
            mp = MediaPlayer.create(getApplicationContext(), ab[0]);
        mp.setLooping(true);
        mp.start();
    }

    @Override
    protected void onResume() {
        Toast.makeText(getApplicationContext(), "" + tonetoplay,Toast.LENGTH_SHORT).show();
        super.onResume();
    }
}
4

2 回答 2

0

您可以取消注册 SensorEventListener 在

@Override
protected void onPause() {
 //Unregister the SensorEventListener
}

你可以再次注册

@Override
protected void onResume() {
  mSensorManager.registerListener(
        mSensorListener,
        mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
        SensorManager.SENSOR_DELAY_NORMAL);
  super.onResume();

}

于 2013-02-04T10:37:23.250 回答
0

试试这个: in = new Intent(getApplicationContext(), StopAlarm.class); in.putExtra("音调", selectedTone); in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); in.setAction(Intent.ACTION_VIEW); context.startActivity(in);

于 2013-02-04T10:48:17.920 回答