我可以从mainActivity转到我的SoundActivity类,这是一个菜单。但是,当我单击设备后退按钮时,它不会返回到我的mainActivity,而只是关闭应用程序。我把finish()放在最后,但似乎没有用。我也尝试过super.backPressed(),但也没有用。 这是声音类的代码
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ToggleButton;
public class SoundActivity extends Activity
{
MediaPlayer ourSong;
private SeekBar volumeSeekbar = null;
private ToggleButton muteButton = null;
private AudioManager audioManager = null;
private AudioManager mAm;
private boolean mIsMute;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.soundmenu);
initControls();
ourSong = MediaPlayer.create(SoundActivity.this, R.raw.beat2);
ourSong.start();
}
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.sbVolumeBar);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar arg0)
{
}
public void onStartTrackingTouch(SeekBar arg0)
{
}
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
muteButton = (ToggleButton)findViewById(R.id.toggleButton1);
muteButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
if(muteButton.isChecked())
{
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
else
{
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
如果有人能帮助我,我会非常感激。目前困惑
确定启动声音活动的mainActivity
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
import android.os.Bundle;
public class PinballShooterActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer ourSong;
private ToggleButton muteButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ourSong = MediaPlayer.create(PinballShooterActivity.this, R.raw.beat2);
ourSong.start();
Button soundBtn = (Button) findViewById(R.id.sound);
soundBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(PinballShooterActivity.this, SoundActivity.class));
}
});
Button highScrBtn = (Button) findViewById(R.id.highscores);
highScrBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(PinballShooterActivity.this, HighScoreActivity.class));
}
});
Button gameBtn = (Button) findViewById(R.id.startgame);
gameBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(PinballShooterActivity.this, GameActivity.class));
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
这里是android清单
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SoundActivity" />
<activity
android:name=".HighScoreActivity" />
<activity
android:name=".GameActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.pinball.shooter.GAMEACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PinballShooterActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.pinball.shooter.faiz.PINBALLSHOOTERACTIVITY"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:3)
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
你的onPause()中有finish()。每次调用SoundActivity时,MainActivity都会进入后台并调用onPause(),然后调用finish()并完成Activity。
答案 1 :(得分:2)
正如我在上面的评论中所指出的,问题不在于SoundActivity,而是在MainActivity中:当MainActivity在启动SoundActivity后暂停时,由于finish()
中的onPause()
而结束:
public class PinballShooterActivity extends Activity {
...
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
...
}
只需删除finish()
即可解决您的问题。
答案 2 :(得分:0)
是的,您可以通过按后退按钮移动应用程序中的任何位置。你必须使用onBackPressed()方法并使用一些代码行,如我在下面显示
public void onBackPressed() {
Intent start = new Intent(Current activity.this,on which activity you want to move .class);
startActivity(start);
finishActivity(0);
}