1

我可以从我的 mainActivity 进入我的 SoundActivity 类,这是一个菜单。但是,当我单击设备后退按钮时,它不会返回到我的 mainActivity,而只会关闭应用程序。我已将完成()放在最后,但这似乎没有用。我也尝试过 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>

4

3 回答 3

3
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
    }
}

你的 onPause() 中有finish()。每次调用 SoundActivity 时,MainActivity 都会进入后台并调用其 onPause(),然后调用 finish() 并完成 Activity。

于 2012-04-17T05:33:53.350 回答
2

正如我在上面的评论中指出的那样,问题不在于 SoundActivity,而在于 MainActivity:在启动 SoundActivity 后暂停 MainActivity 时,由于以下原因而finish()结束onPause()

public class PinballShooterActivity extends Activity {

    ...

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        ourSong.release();
        finish();
        }
    }

    ...

}

只需删除即可finish()解决您的问题。

于 2012-04-17T08:34:19.993 回答
0

是的,您可以通过按手机后退按钮在应用程序中的任何位置移动。您只需要使用 onBackPressed() 方法并使用一些代码行,如下所示

public void onBackPressed() {

     Intent start = new Intent(Current activity.this,on which activity you want to move .class);
        startActivity(start);
        finishActivity(0);
        }
于 2012-04-17T05:43:24.380 回答