在我的应用程序中存在两个活动和一个按钮。当用户触摸activity1中的按钮时,应用程序转到activity2,连续播放三幅图像和声音,并重复,直到用户触摸返回按钮。当用户触摸后退按钮应用程序时,转到activity1。问题是,当按下后退按钮时,它进入第二个活动,但没有显示图像,广播声音没有中断,并且不断重复,节目循环没有出现。我不知道程序逻辑哪里出错了!请告诉我我的代码是:
{
public MediaPlayer mp = new MediaPlayer();
Button button;
ImageView im;
Context context;
int current_img = 0;
int current_aud = 0;
boolean flag= false;
int[] sounds={R.raw.anar,R.raw.ab,R.raw.abi};
int[] images = { R.drawable.anar, R.drawable.water , R.drawable.abi};
String[] texts={"انار","آب","آبی"};
AnimationDrawable animation = new AnimationDrawable();
@Override
public void onBackPressed() {
if (mp != null && mp.isPlaying()) {
Toast.makeText(MainActivity.this, "in backpress", Toast.LENGTH_LONG).show();
mp.stop();
mp.release();
mp = null;
super.onBackPressed();
this.finish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView=findViewById(R.id.textView);
im = findViewById(R.id.imageView);
im.setImageResource(R.drawable.anar);
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i = 0;
int j = 0;
public void run()
{
textView.setText(texts[i]);
im.setImageResource(images[i]);
i++;
mp = MediaPlayer.create(MainActivity.this, sounds[j]);
Toast.makeText(MainActivity.this, "in while", Toast.LENGTH_LONG).show();
mp.start();
j++;
if (j > sounds.length - 1) {
j = 0;
}
if (i > images.length - 1) {
i = 0;
}
handler.postDelayed(this, 5000); //for interval...
}
};
}
}
1条答案
按热度按时间mrwjdhj31#
将处理程序移动到类中,并在onbackpressed()中释放它
尝试更改onbackpressed():