Android 停止计数器并销毁媒体播放器

发布于 2024-09-05 01:32:18 字数 258 浏览 5 评论 0原文

我正在开发一个每秒发出蜂鸣声的应用程序。当我按下主页按钮时,我希望它关闭程序并停止发出蜂鸣声。现在它关闭了程序但继续发出蜂鸣声。

我做错了什么?

        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        isdone = true;
        mp.release();
        counter.cancel();
        finish();
    }

I am working on an app that beeps every second. When I hit the home button I want it to close the program and stop beeping. Right now it closes the program but continues to beep.

What am I doing wrong?

        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        isdone = true;
        mp.release();
        counter.cancel();
        finish();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲影 2024-09-12 01:32:18

当我按下主页按钮时我想要它
关闭程序并停止发出蜂鸣声。

您无法从应用程序代码中拦截 HOME 按钮 - 抱歉!

When I hit the home button I want it
to close the program and stop beeping.

You cannot intercept the HOME button from your application code -- sorry!

风追烟花雨 2024-09-12 01:32:18

您无法直接拦截 HOME 按钮,尽管解决方法是使您的应用程序启动活动成为 HOME 意图/默认主页活动;在您的 xml 清单中,它会是这样的:

<intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <category android:name="android.intent.category.HOME"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

因此,当用户按下主页按钮时,您将引导用户进入应用程序的启动活动,从这里您可以将他重定向到任何其他先前的活动,即通过保存预先了解用户的状态。
在您上面提到的活动中,当您发出蜂鸣声时,您应该将停止命令放在 onPause 或 onWindowFocusChanged 中。

上述方法仅在某些情况下有意义,并且取决于您的应用程序逻辑/业务案例以及您要部署它的环境。

我们在应用程序中使用了这种方法(处理主页按钮),我们的应用程序将始终与设备捆绑和分发。所以无论如何我们都可以完全控制设备设置。当您将自己的 Activity 设置为默认 Home Activity 时,用户最初会看到一个选项屏幕,其中询问哪个 Activity 应该处理 Home Intent(默认 Android Home 或您的应用程序 Activity)。然后,用户可以选择,也可以选中复选框以将此活动用作将来的默认活动,而无需再次询问。

对于公共市场的一般应用程序来说,这可能不是最好的方法,因为它会有点不方便,而且也不是一个好的屏幕流程/用户体验。
对于您可以完全控制的应用程序,这可能是一个合适的解决方法。

如果你真的想捕获HOME按钮,你需要修改android操作系统,它位于android.policy.jar中的PhoneWindowManager.java类中。
但是,只有当您完全控制设备并将您的应用程序与设备捆绑在一起,或者它是一个内部应用程序(您将设备交付给用户并控制您部署的 ROM)时,这才有意义。

还有一个有用的讨论,其中包含有关 HOME 按钮的更多链接:
http://www.anddev.org/home_menu_button-t10232.html

You cannot intercept the HOME button directly, although a workaround would be to make your application start activity a HOME intent / default home activity; in your xml manifest, it would be like this:

<intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <category android:name="android.intent.category.HOME"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

So when the user presses the home button, you'd lead the user to the start activity of your app, from here you could redirect him to any other previous activity, i.e. by saving the user's state beforehand.
In the activity that you mentioned above, where you have the beeping, you should place the stop command in onPause or onWindowFocusChanged.

This above approach only makes sense in certain cases though and depends on your application logic / business case and the environment you are going to deploy it.

We used this approach in an application (handling the home button) where our app will always be bundled and distributed with a device. So we're in full control of the device setup anyway. When you set your own activity as the default home activity the user would initially get an option screen with the question which activity should handle the home intent (the default android home or your app activity). Then the user can choose and also can check a checkbox to use this activity as the default activity in the future without being asked again.

For general apps for the public market, this might not be the best way though, as it would be little inconvenient and it's not a good screen flow / user experience either.
For apps where you're in full control, this might be a suitable workaround.

If you really want to capture the HOME button, you would need to modify the android OS, it's in class PhoneWindowManager.java which comes in android.policy.jar.
But this also only makes sense if you're in full control of the devices and i.e. bundle your app with a device or if it's an internal application where you deliver the devices to the users and are in control of which ROM you deploy.

There's also a useful discussion with further links about the HOME button on:
http://www.anddev.org/home_menu_button-t10232.html

赠我空喜 2024-09-12 01:32:18

当您点击主页按钮时,您的 Activity 的 onPause() 方法是否会被调用?

你可以杀死那里的柜台。不要自己终止活动,系统会在适当的时候为您执行此操作。

Isn't your activity's onPause() method called when you hit the home button?

You can kill the counter there. Don't kill the activity by yourself, the system will do that for you when it's appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文