应该在启动过程中被称为

应该在启动过程中被称为

本文介绍了Android的生命周期:是onResume()应该在启动过程中被称为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想从Android应用开发为例傻瓜,这是一个简单的应用程序,切换手机的振铃模式。在code如下。

I'm trying an example from Android Application Development for Dummies, which is a simple app that toggles the ringing mode of the phone. The code is below.

    public class SilentModeToggleActivity extends Activity {

private AudioManager mAudioManager;
private boolean mPhoneIsSilent;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    checkIfPhoneIsSilent();
    setButtonClickListener();
}

@Override
public void onResume() {
    super.onResume();
    checkIfPhoneIsSilent();
    toggleUi();
}

private void checkIfPhoneIsSilent() {
    int ringerMode = mAudioManager.getRingerMode();
    if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
        mPhoneIsSilent = true;
    } else {
        mPhoneIsSilent = false;
    }
}

private void setButtonClickListener() {
    Button toggleButton = (Button) findViewById(R.id.toggleButton);
    toggleButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (mPhoneIsSilent) {
                mAudioManager .setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                mPhoneIsSilent = false;
            } else {
                mAudioManager
                        .setRingerMode(AudioManager.RINGER_MODE_SILENT);
                mPhoneIsSilent = true;
            }
            toggleUi();
        }
    });
}

private void toggleUi() {
    ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
    Drawable newPhoneImage;
    if (mPhoneIsSilent) {
        newPhoneImage = getResources().getDrawable(R.drawable.phone_silent);
    } else {
        newPhoneImage = getResources().getDrawable(R.drawable.phone_on);
    }
    imageView.setImageDrawable(newPhoneImage);
}

}

我的问题是,因为我只有重写的onCreate(显示在默认情况下,正常模式图像)和onResume,它的预期,图像可变为沉默如果我更改手机模式为静音应用外(在onResume( )我检查当前状态,并更新用户界面),但为什么它仍然显示正确的图像,即使我杀的过程,然后在手机模式更改为沉默?我希望应用程序重新加载,并显示默认的图像,这是正常的...不是正确的但令人困惑的沉默形象。

My question is since I only override onCreate (shows the "normal" mode image at default) and onResume, it's expected that the image changes to "silent" if I change the phone mode to silent outside the app (in onResume() I check the current state and update the ui), but why it still shows the correct image even if I KILL the process and then change the phone mode to silent? I would expect the app to reload and shows the default image, which is normal...not the correct yet confusing silent image.

推荐答案

onResume()被称为一个活动正在恢复前景输入的任何时间。这包括:

onResume() is called any time an activity is regaining the foreground input. This includes:


  • 当它返回到屏幕后,别的东西有前景(例如,设置),以及

  • When it is returning to the screen after something else had the foreground (e.g., Settings), and

当它被用于在此过程中第一次(包括所需的任何新的过程,因为你杀了从DDMS旧)创建

When it is being created for the first time in this process (which includes any new process required because you killed the old one from DDMS)

因此​​,你的code将检查在这两种情况下的振铃模式的状态,将在任一情况下使用适当的图像。

Hence, your code will examine the state of the ringer mode in either case and will use the proper image in either case.

这篇关于Android的生命周期:是onResume()应该在启动过程中被称为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:25