15

如您所见,我有这个 PlayLesson_01 活动,它同时显示图像和音频。此活动有两个处理程序,一个用于控制图像和音频,而另一个将完成课程并返回上一个活动(课程菜单)。

目标:单击硬后退按钮返回上一个活动并停止播放图像和音频。

问题:当我单击硬“返回”时,我能够返回到上一个活动,但音频仍在播放。我无法停止终止在活动中启动的处理程序。

问题:我怎样才能完全杀死或停止这些处理程序?

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

public class PlayLesson_01 extends Activity implements OnItemSelectedListener,
    ViewFactory, Runnable {

// Setting up images
// Keeping all Images in array references to our images
public Integer[] mThumbIds = { R.drawable.nouraniyah,
        R.drawable.back_angle, R.drawable.back_fox, R.drawable.back_apple,
        R.drawable.back_twitter, R.drawable.back_thunderbird };
// setting up music
int[] myMusic = { R.raw.button_3, R.raw.button_3, R.raw.button_3,
        R.raw.button_3, R.raw.button_3, R.raw.button_3 };
int mNext;
private ImageSwitcher mSwitcher;
MediaPlayer mp;
Handler mHandlerWholeLesson = new Handler();
Runnable mRunnableWholelesson = new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        startActivity(new Intent("com.example.AmazingGame.LESSONONE"));
    }
};

Handler mHandlerNextfile = new Handler();
Runnable mRunnableNextFile = new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        mp.setOnCompletionListener(mListener);
        mp.start();
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.image_switcher); // it could be lesson one
                                                // layout.

    mSwitcher = (ImageSwitcher) findViewById(R.id.imgswitcher);
    mSwitcher.setFactory(this);
    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in));
    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));

    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemSelectedListener(this);
    // usual onCreate stuff here, then...
    // either here or whenever you want to start the sequence
    mNext = 0;
    startNextFile();

    // Is this good !! Yes it is. count the time for all audio.
    mHandlerWholeLesson.postDelayed(mRunnableWholelesson, 25000);

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent("com.example.AmazingGame.LESSONONE"));
    System.exit(0);
    mHandlerNextfile
            .removeCallbacks(mRunnableNextFile, PlayLesson_01.class);
    mHandlerWholeLesson.removeCallbacks(mRunnableWholelesson,
            PlayLesson_01.class);

    PlayLesson_01.this.finish();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    // mHandlerNextFile.removeCallbacksAndMesssages(PlayLesson_01.class);
}

public void onItemSelected(AdapterView<?> parent, View v,
        final int position, long id) {

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

@Override
public View makeView() {
    ImageView i = new ImageView(this);
    i.setBackgroundColor(0xFF000000);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new ImageSwitcher.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    return i;
}

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(mThumbIds[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        i.setBackgroundResource(R.drawable.picture_frame);
        return i;
    }

    private Context mContext;

}

OnCompletionListener mListener = new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.release();
        startNextFile();
    }
};

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

}

public void startNextFile() {

    if (mNext < myMusic.length) {

        mp = MediaPlayer.create(this, myMusic[mNext]);
        mSwitcher.setImageResource(mThumbIds[mNext++]);
        mHandlerNextfile.postDelayed(mRunnableNextFile, 3000);
    }

}

@Override
public void run() {
    // TODO Auto-generated method stub

}

}

4

2 回答 2

52

现在您的代码中有几个错误的地方:

  1. 如果你想回到你以前的活动,你不要重新启动它,startActivity因为 android 的默认行为是它维护一堆以前的活动。默认情况下,按下back它会返回到上一个活动(除非您使用某些标志启动了子活动。)

  2. 你为什么用System.exit(0);背压机?您只需调用finish()即可完成当前活动。

  3. 从处理程序中删除回调的最佳方法是使用 anull作为参数。您可以尝试以下代码:

    mHandlerNextfile.removeCallbacksAndMessages(null);
    mHandlerWholeLesson.removeCallbacksAndMessages(null);
    

    因为这将删除所有回调。检查此链接以了解更多信息:http: //developer.android.com/reference/android/os/Handler.html#removeCallbacksAndMessages(java.lang.Object)

于 2012-07-02T19:09:10.213 回答
0

您的处理程序没有“运行”,您的媒体播放器是。打个电话就行mp.stop()onBackPressed()

此外,阿伦乔治所说的。

于 2012-07-02T19:13:33.157 回答