我正在尝试从 Android 应用程序发送短信。我正在使用 PendingIntent 以便我可以使用 Broadcast Receiver 检查它是否发送正常。由于 sendTextMessage 调用将按 SMS 完成,我需要发送一些“额外”数据来识别实际的 SMS,以便我的广播接收可以对特定的 SMS 消息做一些工作。
这是我的发送代码:
String SENT = "SMS_SENT";
Intent sentIntent = new Intent(SENT);
sentIntent.putExtra("foo", "BAR");
PendingIntent sentPI = PendingIntent.getBroadcast(baseContext, 0,
sentIntent, 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(baseContext, 0,
new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(smsMessage.getNumber(), null, smsMessage.getText(), sentPI, deliveredPI);
问题是在我的 BroadCast 接收器中,它似乎无法读取我的额外“foo”:
public class SMSSentBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
String smsID = intent.getStringExtra("foo");
......
}
}
smsID 变为空。
我的广播接收器是这样注册的:
baseContext.registerReceiver(new SMSSentBroadcastReceiver(), new IntentFilter("SMS_SENT"));
鉴于 Intent 过滤器正在运行,我做错了什么?为什么不发送额外的内容?
感谢任何帮助。谢谢
最佳答案
我怀疑发生了什么(至少,它发生在我身上)是你之前在其他地方使用了 SMS_SENT Intent ,那时,那个 Intent 在额外的地方没有“foo”。使用相同的 Intent 和请求代码调用 PendingIntent.getBroadcast 将返回原始的待定 Intent 。
如果它实际上意味着相同的 Intent ,您需要使用类似 PendingIntent.FLAG_UPDATE_CURRENT 的东西用新的附加功能更新 Intent 。如果它不是同一个 Intent ,它应该有一个不同的请求代码,这样它就不会匹配另一个 Intent (这就是为什么输入一个唯一的请求代码有效)。
关于Android PendingIntent 额外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9705899/
我定义了一个方法:defmethod(one:1,two:2)[one,two]end当我这样调用它时:methodone:'one',three:'three'我得到:ArgumentError:unknownkeyword:three我不想从散列中一个一个地提取所需的键或排除额外的键。除了像这样定义方法之外,有没有办法规避这种行为:defmethod(one:1,two:2,**other)[one,two,other]end 最佳答案 如果不想写**other中的other,可以省略。defmethod(one:1,two:2
我有以下模型用户has_many:users_contactshas_many:contacts,through::users_contactsaccepts_nested_attributes_for:contacts,allow_destroy:true联系方式has_many:users_contactshas_many:users,through::users_contactsaccepts_nested_attributes_for:users_contacts,allow_destroy:true用户联系belongs_to:usersbelongs_to:contacts
我有一个自定义表单生成器,使用此自定义生成器的原因之一是对于每个表单,我都需要包含一些额外的参数,我不想在每个表单中使用隐藏字段标签显式放入这些参数写。for_for(@foo,:builder=>MyBuilder)do|f|#stuffIshouldn'thavetoworryabout#thisshouldbeputinallthetimewithoutmehavingtodoithidden_field_tag('extra','myextrainfo')#normalthingsIwouldputinf.text_field(:bar)end我必须在我的自定义表单构建器中做什
我试图在我的一个HamlView中的If/Else语句中放置一些(未呈现的)注释,但它似乎会导致问题。我想要以下代码:-#Stufflike______activatestheifstatement-if@condition(Somecode)-#Stufflike_____activatestheelsestatement-else(Someothercode)不幸的是,Rails向我抛出这个错误:Got"else"withnopreceding"if"如果我删除“其他”注释,即-#Stufflike______activatestheifstatement-if@condition
我无法在这里或其他地方找到任何涵盖限制资源路由和在Rails3中添加其他非RESTful路由的内容。这可能非常简单,但我遇到的每个示例或解释都只解决了一个案例不能同时。这是我在Rails2中所做的一个例子:map.resources:sessions,:only=>[:new,:create,:destroy],:member=>{:recovery=>:get}非常简单,我们只需要7条RESTful路由中的3条,因为其他路由对该资源没有任何意义,但我们还想添加另一条用于帐户恢复的路由。现在据我所知,做这些事情中的任何一件也非常简单:resources:sessions,:only=>
规范:beforedoLogger.should_receive(:write).with'Logmessage1'endit'works'doget'/'endSinatra应用程序:get'/'Logger.write('Logmessage1')Logger.write('Logmessage2')end由于“日志消息2”,此规范失败。如何告诉RSpec忽略任何其他消息,只测试预期的消息? 最佳答案 您需要在消息预期之前对将接收消息的方法进行stub。#RSpecstubmethodisdeprecatedinRSpec3,而
我已被添加为现有项目的贡献者,该项目具有附加到gem的签名证书。我将推出下一个版本,所以我需要将我的证书添加到gem中。当我按照http://guides.rubygems.org/security/#building-gems的说明进行操作时一切都很好,直到我到达第4点并运行gembuild...出现以下错误:ERROR:Whileexecutinggem...(Gem::Security::Exception)invalidsigningchain:certificate(mydetails)wasnotissuedby(existingcertowner'sdetails)我从来
我和一位同事在共享某些模型的不同项目中工作。因此,我们通过git子模块共享模型。此外,我们还希望能够共享迁移:这样,我同事的迁移将在我项目的文件夹db/migrate/other_db中。如何配置Rails迁移以在这个额外的文件夹中运行迁移? 最佳答案 在您的配置文件中(config/application.rb用于所有环境或config/environments/$(environment).rb仅用于特定环境)添加此行:config.paths['db/migrate']+='db/migrate/other_db'如果你想改变
我正在尝试编写一个方法,该方法充当setter并在分配的值之外采用一些额外的参数。愚蠢的例子:classWordGeneratordef[]=(letter,position,allowed)puts"#{letter}#{allowed?'now':'nolonger'}allowedat#{position}"enddefallow=(letter,position,allowed)#...endend将它写成索引器是可行的,我可以这样调用它:gen=WordGenerator.newgen['a',1]=true#orexplicitly:gen.[]=('a',1,true)但
我需要将额外的参数传递给factorygirl以在回调中使用。像这样(但实际上更复杂):Factory.define:blogdo|blog|blog.name"Blah"blog.after_createdo|blog|blog.posts+=sample_postsblog.save!endend然后用这样的东西创建它:Factory.create(:blog,:sample_posts=>[post1,post2])有什么办法吗? 最佳答案 由于transient属性(seecommentonissue#49),这现在可以在没