处理 SMS-onReceive 以触发另一个活动/类中的事件
所以,我想做的是更改一些在我的主要活动中扩展的 ui 元素,并且我希望这些更改由 onReceive 触发,该更改来自在不同的单独类中扩展的广播接收器。
我想
public class SmsReceiver extends BroadcastReceiver {
public void onReceive(context, intent){
MainActivity main = new MainActivity(); //this is my... main activity :)
main.button.setBackgroundColor(Color.green);
}//end onReceive
}//end class
在我的 MainActivity 中做这样的事情,我已经将值设置为 GUI 按钮元素,如下所示:
public class mainactivity extends activity... implements onclick... bla bla (){
Button button;
onCreate....{
button = (Button)findViewById(R.id.button);
所以我想知道当 onReceive 被激活时,我是否可以在另一个活动中编辑小部件的状态通过实例化该活动并调用其 setter 方法?
so, what i'm trying to do is change some ui elements which are expanded in my main activity- and i want the changes to be triggered by the onReceive, which is from broadcastreceiver extended in a different separate class.
I'd like to do something like this
public class SmsReceiver extends BroadcastReceiver {
public void onReceive(context, intent){
MainActivity main = new MainActivity(); //this is my... main activity :)
main.button.setBackgroundColor(Color.green);
}//end onReceive
}//end class
in my MainActivity, i've set values to the GUI button element like this:
public class mainactivity extends activity... implements onclick... bla bla (){
Button button;
onCreate....{
button = (Button)findViewById(R.id.button);
so i'd like to know if when onReceive is activated, can i edit the state of a widget in ANOTHER activity by instantiating that activity and calling a setter method on it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法将不起作用,因为您正在创建 MainActivity 的新实例。这不会引用您当前活跃的主要活动。您可以尝试的一种解决方案是向您的活动发送另一个意图并实现 onNewIntent(Intent newIntent) 在您的活动中。这样您就可以更新该函数中的主要活动,尝试使用您收到的新信息传递额外内容。
Your method will not work because you are creating a new instance of MainActivity. This will not reference your currently active main activity. One solution you may try is sending another intent to your activity and implementing onNewIntent(Intent newIntent) in your activity. That way you can update your main activity in that function, try passing extras in with the new information you have received.