本文介绍了向后兼容onBackPressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Android 1.5上使用onBackPressed(),我的应用程序将崩溃.如果在Android 1.5设备上运行,是否有可能停用此功能?

If I use onBackPressed() on Android 1.5, my application crashes. Is there any possibility to deactivate this function if running on an Android 1.5 device?

那里的代码不是绝对必要的,而是真正的拥有",因此我想将其保留在较新的设备上,而仅将其放置在较旧的设备上.

The code there is not absolute necessary but a real "nice to have", so I would like to keep it on newer devices and just drop it on older ones.

这可能吗?

我想我是用旧方法找到的:

edit: I think I found it, just the old way:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
   if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
       // do something on back.
       return true;
   }

return super.onKeyDown(keyCode, event);
}

推荐答案

您可以尝试使用应用程序并根据准备不同分支的情况来检测运行时哪个版本的SDK.喜欢:

You can try to detect runtime which version of SDK using your application and depending on that prepare different branches. Like:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(Build.VERSION.SDK_INT==Build.VERSION_CODES.CUPCAKE) //if it's 1.5
    {
       if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
       {        // do something on back.
          return true;
       }
    }
    return super.onKeyDown(keyCode, event);
}

这篇关于向后兼容onBackPressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 12:04