我有两个活动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) {
}
};
StopAlarm.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();
}
}
答案 0 :(得分: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();
}
答案 1 :(得分:0)
试试这个: in = new Intent(getApplicationContext(),StopAlarm.class); in.putExtra(“tone”,selectedTone); in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); in.setAction(Intent.ACTION_VIEW); context.startActivity(IN);