将数据从广播接收器传递到另一个活动 [英] Passing Data from Broadcast Receiver to another Activity

查看:70
本文介绍了将数据从广播接收器传递到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在与广播接收器发生问题,并将信息传递给另一个活动.我正在尝试创建一个应用程序,该应用程序将捕获传入的SMS消息,在文本中查找一个网站,然后弹出一个警告"对话框,询问用户是否要访问该网站.

Hi I've been having an issue with Broadcast Receivers and passing information to another activity. I'm trying to create an application that will capture incoming SMS messages, look for a website in the text, then pop up an Alert Dialog box asking if the user wants to go to the website.

public class TextReceiver extends BroadcastReceiver{

public void onReceive(Context context, Intent intent)
{
    // .. other code that
    // sets received SMS into message

    Toast toast = Toast.makeText(context,
            "Received Text: " + message.getMessageBody(), Toast.LENGTH_LONG);
            toast.show();
 }

为使该代码正常工作,请接收文本,并随消息弹出祝酒词.烤面包没用,但显示接收器有效.但是我想与一个活动进行通信以显示警报对话框"并启动一个webView.我已经编写了代码,该代码将对网站进行字符串搜索并打开webView.是否可以从广播接收器中获取字符串并执行类似的操作?:

So that code works fine, receive a text it pops up a toast with the message. The toast is useless but it shows the receiver works. But I want to communicate with an activity to show an Alert Dialog and start up a webView. I already programmed the code that will take a string search for the website and open the webView. Is it possible to get the string from the broadcast receiver and do something like this?:

    public class ReceiveText extends Activity{
public void onCreate(Bundle savedInstanceState) {

// Somehow pass the string from the receiver into this activity, 
//stored in variable messages

findOpen(messages);

// is that possible?

}
public class findOpen(string messages){
// do stuff ... open alert...open site if OK
}

所以基本上,我只想将字符串从广播接收器传递到另一个使用该字符串的活动.剩下的代码基本上就位了,我只需要那个字符串...我对Java和Java都是陌生的,任何帮助将不胜感激.谢谢

So basically I just want to pass a string from a Broadcast Receiver to another activity that will use that string. The rest of the code is basically in place all I need is that string... I'm new to this and Java and any help would be much appreciated. Thanks

推荐答案

如果您的活动名为ReceiveText,则在BroadcastReceiver中,您应该执行以下操作:

If you have your activity named ReceiveText, then in your BroadcastReceiver, you should do the following:

Intent i = new Intent(context, ReceiveText.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("message", message.getMessageBody());
context.startActivity(i);

然后,在您的活动中,您将需要像这样getExtra:

Then, in your activity, you will need to getExtra as so:

Intent intent = getIntent();
String message = intent.getStringExtra("message");

然后您将根据需要使用message.

And then you will use message as you need.

如果仅希望ReceiveText活动将消息显示为对话框,请在清单中为ReceiveText声明<activity android:theme="@android:style/Theme.Dialog" />,然后将消息设置为活动中的textview.

If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme.Dialog" /> in your manifest for ReceiveText and then set the message to a textview in the activity.

让我知道是否需要添加其他内容.

Let me know if I need to add anything else.

这篇关于将数据从广播接收器传递到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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