为什么播放声音的活动会干扰用户界面? [英] Why does an activity playing a sound disturb the user interface?

查看:77
本文介绍了为什么播放声音的活动会干扰用户界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击该通知时,我想显示一个通知并播放声音.正如我在自己对这个问题的回答中所写的那样,当我使用活动来播放声音时,它会以某种方式起作用:

这就是我点击声音通知时的样子(这些部分不见了):

谁能解释为什么会这样?使用活动播放声音是错误的方法吗?但是当我使用服务时在这里不起作用,我什么也听不到!该怎么解决?

解决方案

根据Android开发者参考,几乎所有活动都与用户互动,因此Activity类负责为您创建一个窗口,您可以在其中创建一个窗口用setContentView(View)放置您的UI".

我没有这样做.因此,显示了一个新窗口,但没有任何内容.

播放声音的更好解决方案是使用 PlaySoundService (服务!!)代替活动.它包含与活动几乎相同的代码,但是挂起的意图是使用 PendingIntent.getService(...)而不是 PendingIntent.getActivity(...)创建的>.使用错误的方法不会导致明显的错误消息,但是该服务将无法正常工作.

I would like to show a notification and play a sound when the user taps onto that notification. It works somehow when I use an activity to play the sound, as I have written in my own answer to this question: How can I create a notification that plays an audio file when being tapped? (in this question and answer there is also the source code showing how I create the notification and how my PlaySoundActivity looks like.

Yet, I have realized, that while the sound is playing, the appearance of my main application changes and it will not be restored without closing the application.

I have created my application from the "Tabbed Activity" project template.

This is how it looks after being started:

And this is how it looks when I have tapped onto the sound notification (the sections are gone):

Can anyone explain why this happens? Is it a wrong approach to play sound using an activity? But it does not work here when I use a service, I hear nothing! How to solve that?

解决方案

According to the Android developer reference, "almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)".

I had not done this. Therefore a new window was displayed, but there was no content.

The better solution for playing a sound is to use a PlaySoundService (service!!!) instead of an activity. It contains almost the same code as an activity would do, but the pending intent is created with PendingIntent.getService(...) instead of PendingIntent.getActivity(...). Using the wrong method does not result in an obvious error message, but the service won't work as expected.

这篇关于为什么播放声音的活动会干扰用户界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆