onBack pressed杀死一个活动中的处理器 - 机器人机器人、处理器、活动中、onBack

2023-09-12 05:35:15 作者:久溺深海心会凉

正如你所看到的,我有在同一时间显示图像和音频此PlayLesson_01活动。这个活动有两个处理器,一个用于控制图像和声音,而其他人会完成课程并回到previous活动(教训菜单)。

As you can see, I have this PlayLesson_01 activity which displays images and audio at the same time. This activity has two handlers, one to control images and audio, while the other one will finish the lesson and goes back to previous activity (Lessons Menu).

目标:点击很难后退按钮返回到previous活动,并停止播放图像和音频

Goal: Click on hard Back button to go back to previous activity and stop playing images and audio.

问题:当我点击硬后退我能够回到previous活动,但声音依然playing.I'm不能停止或杀在活动中推出的处理器。

Problem: When I click on hard "Back" I'm able to go back to the previous activity, but the audio is still playing.I'm not able to stop or kill the handler that launched within the activity.

问题:我怎么能杀死或完全阻止那些处理程序

Question: How can I kill or stop those handlers completely?

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

}

}

推荐答案

现在有几个在code错误的东西:

Now there are couple of wrong things in your code:

如果你想回到你的previous活动,您不使用 startActivity 成为Android的默认行为是它重新启动它保持一叠previous活动。关于preSS 返回将默认回到previous活动(除非使用一些标志推出的子活动。)

If you want to go back to your previous activity, you do not relaunch it using an startActivity as the default behaviour of android is that it maintains a stack of previous activities. On press of back it will by default go back to the previous activity (unless you have launched the child activity using some flags.)

为什么要使用 System.exit(0)背preSS?你只需要调用完成()来完成当前的活动。

Why are you using a System.exit(0); on back press? You just have to call the finish() to finish the current activity.

从处理程序中删除回调,最好的方法是使用作为参数。你可以试试下面的code:

The best way to remove callbacks from a handler is using a null as a parameter. You may try the following code:

mHandlerNextfile.removeCallbacksAndMessages(空); mHandlerWholeLesson.removeCallbacksAndMessages(空);

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

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

since this would remove all callbacks. Check this link to know more: http://developer.android.com/reference/android/os/Handler.html#removeCallbacksAndMessages(java.lang.Object)