如何通过通知中的按钮控制音乐播放器,以及如何从通知中收听按钮 Action
最佳答案
您应该使用 RemoteViews 的 setOnClickPendingIntent() 来监听通知中的按钮操作。
setOnClickPendingIntent()方法会绑定(bind)一个PendingIntent来响应按钮点击事件。
示例代码是这样的:
private void showControllerInNotification() {
PendingIntent pendingIntent = null;
Intent intent = null;
//Inflate a remote view with a layout which you want to display in the notification bar.
if (mRemoteViews == null) {
mRemoteViews = new RemoteViews(getPackageName(),
R.layout.notification_control_bar);
}
//Define what you want to do after clicked the button in notification.
//Here we launcher a service by an action named "ACTION_STOP" which will stop the music play.
intent = new Intent(ACTION_STOP);
pendingIntent = PendingIntent.getService(getApplicationContext(),
REQUEST_CODE_STOP, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//In R.layout.notification_control_bar,there is a button view identified by bar_btn_stop
//We bind a pendingIntent with this button view so when user click the button,it will excute the intent action.
mRemoteViews.setOnClickPendingIntent(R.id.bar_btn_stop,
pendingIntent);
//Create the notification instance.
mNotification = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher).setOngoing(true)
.setWhen(System.currentTimeMillis())
.setContent(mRemoteViews)
.build();
//Show the notification in the notification bar.
mNotifiManager.notify(NOTIFICATION_ID, mNotification);
}
响应 Action 的服务是这样的:
public class MediaPlayerService extends Service {
public static final String ACTION_STOP="xxx.yyy.zzz.ACTION_STOP";
public static final String ACTION_PLAY="xxx.yyy.zzz.ACTION_PLAY";
public static final String ACTION_PAUSE="xxx.yyy.zzz.ACTION_PAUSE";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();
if (!TextUtils.isEmpty(action)) {
if (action.equals(ACTION_PLAY)) {
startPlay();
}else if(action.equals(ACTION_PAUSE)) {
pausePlay();
}else if(action.equals(ACTION_STOP)) {
stopPlay();
}
}
}
return super.onStartCommand(intent, flags, startId);
}
private void stopPlay(){
// do the play work here
}
private void stopPause(){
// do the pause work here
}
private void stopPlay(){
// do the stop work here
}
在 list 中注册服务:
<service
android:name="xxx.yyy.zzz.MusicPlayService"
android:exported="false" >
<intent-filter>
<action android:name="xxx.yyy.zzz.ACTION_PLAY" />
<action android:name="xxx.yyy.zzz.ACTION_PAUSE" />
<action android:name="xxx.yyy.zzz.ACTION_STOP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
对于音乐播放控件,您可以从here中复制一些有用的代码 fragment 。 .
关于android - 安卓音乐播放器的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21872022/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我们正在开发一个需要推送通知的WP8应用程序。为了测试它,我们使用CURL命令行运行推送通知POST请求,确保它实际连接,使用客户端SSL证书进行身份验证并发送正确的数据。我们确实知道,当我们收到对设备的推送时,这项工作是有效的。这是我们一直用于测试目的的CURL命令:curl--certclient_cert.pem-v-H"Content-Type:text/xml"-H"X-WindowsPhone-Target:Toast"-H"X-NotificationClass:2"-XPOST-d"MytitleMysubtitle"https://db3.notify.live.ne
我在nginx+unicorn后面运行一系列Rails/Sinatra应用程序,零停机部署。我喜欢这个设置,但Unicorn需要一段时间才能完成重新启动,所以我想在完成时发送某种通知。我能在Unicorn文档中找到的唯一回调与workerfork相关,但我认为这些回调对此不起作用。这是我从赏金中寻找的东西:老unicorn主人启动新主人,然后新主人开始它的worker,然后旧主人停止它的worker并让新主人接管。我想在交接完成后执行一些ruby代码。理想情况下,我不想为此实现任何复杂的流程监控。如果这是唯一的方法,那就这样吧。但在走那条路之前,我正在寻找更简单的选择。
我一直试图在这里寻找答案,但我找不到任何有用的东西。我已经对我的Rails应用程序实现了:success和:dangerflash通知。它工作得很好,即:success是绿色的,:danger是红色的,有一个关闭按钮等等,但是自从添加了一些邮件文件后,我的:success现在显示为红色?application.html.erb摘录:×contact_mailer.rbclassContactMailercontacts_controller.rbclassContactsController还有,contact_email.html.erbNewMessagefromHoo
我已更新到Rails2.3.10、Rack1.2.1,现在我的所有即时消息都没有显示。我发现在重定向期间,通知是这样传递的redirect_to(@user,:notice=>"Sorrytherewasanerror")在我看来闪存哈希是空的!map:ActionController::Flash::FlashHash{}但是您可以在Controller中看到该消息。是什么原因?session{:home_zip=>"94108",:session_id=>"xxx",:flash=>{:notice=>"Sorrytherewasanerror"},:user_credential
我正在尝试使用ruby中的seleniumwebdriver从gmail桌面通知中获取数据 最佳答案 开箱即用的想法,用Selenium截屏并用OCR处理图像?https://github.com/suyesh/ocr_space我假设Selenium只允许您与页面数据交互。 关于ruby-如何在seleniumwebdriver-ruby中自动化桌面通知,我们在StackOverflow上找到一个类似的问题: https://stackoverflo
OpenSSL::SSL::SSLError(SSL_connectSYSCALLreturned=5errno=0state=SSLv3readserversessionticketA):为苹果推送通知Houstongem集成库。自上两个月以来,它运行顺利,但现在在应用程序中出现错误。尝试多种解决方案来解决问题。也尝试使用新的证书pem文件,但遇到相同的错误..有时它可以工作请帮忙解决问题。 最佳答案 错误严格来说是您使用了错误的APNS证书。它可能已过期,或者它只是一个旧证书的类型(在2015年12月之前创建)。一年前,Appl
我正在开发一个应用程序,用户可以在其中添加他们的Gmail帐户,我会对他们的电子邮件进行一些分类工作。我想在任何注册帐户收到新电子邮件时收到通知。一个解决方案是通过IMAP不断轮询帐户并保存我获取的最后一封电子邮件日期以检查是否有新邮件,但这有很多开销。知道如何监控Gmail并在收到新电子邮件时收到通知并将其与Rails应用集成吗?例如,是否有扩展程序可以执行此操作并将发布请求发送到我的Rails应用程序? 最佳答案 我很确定IMAP是这里唯一的答案。您可能想看看IDLE是否有效——我读过相互矛盾的答案。如果是这样,它比轮询响应更快