从传递的BroadcastReceiver类数据到Android的活动 [英] Pass data from BroadcastReceiver class to a Android Activity

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

问题描述

我开发一个Android应用程序,它具有同时应用程序运行时,如果我收到一个新的SMS消息,消息的内容需要使用TextToSpeech读取的功能。我知道如何使用TextToSpeech读取文本。我已经写了两个班在我的应用程序。一个是MainActivity从活动延伸,而另一个是从SmsReceiver的BroadcastReceiver延伸。当MainActivity运行时接收到一个短信我想用SmsReceiver得到短信的内容,并将它传递给MainActivity,然后使用TextToSpeech内MainActivity阅读。我怎样才能通过短信从SmsReceiver的内容,我MainActivity。我SmsReceiver类的副本粘贴波纹管。

I'm developing an Android application and it has a functionality that while the application is running if I receive an new SMS message the content of that message need to be read using TextToSpeech. I know how to read a text using TextToSpeech. I have written two classes on my application. one is MainActivity extends from Activity and the other one is SmsReceiver extends from BroadcastReceiver. When a Sms is received while the MainActivity is running I want get the content of the sms message using SmsReceiver and pass it to MainActivity and then read it inside MainActivity using TextToSpeech. How can I pass the content of the sms from SmsReceiver to my MainActivity. A copy of my SmsReceiver class is pasted bellow.

public class SmsReceiver extends BroadcastReceiver{


@Override
public void onReceive(Context context, Intent intent) {
    //this stops notifications to others
    this.abortBroadcast();

    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();   
    SmsMessage[] msgs = null;
    String from = null;
    String msg= null;
    String str = "";            
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();
            from = msgs[i].getOriginatingAddress();
            str += " :";
            str += msgs[i].getMessageBody().toString();
            msg = msgs[i].getMessageBody().toString();
            str += "\n"; 
        }
        System.out.println("from "+from);
        System.out.println("msg "+msg);
        Toast.makeText(context, "SMS Received : ",Toast.LENGTH_LONG).show();

        //continue the normal process of sms and will get alert and reaches inbox
            this.clearAbortBroadcast();
        }
}

}

推荐答案

使用的接口是为我工作的解决方案。

Using an interface is a solution that worked for me.

在你的BroadcastReceiver把下面的

In your BroadcastReceiver put the following

public interface OnSmsReceivedListener {
    public void onSmsReceived(String sender, String message);
}

然后添加监听器

then add the listener

private OnSmsReceivedListener listener = null;

然后添加方法的BroadcastReceiver ..

then add a method to the BroadcastReceiver..

public void setOnSmsReceivedListener(Context context) {
    this.listener = (OnSmsReceivedListener) context;
}

然后在您的onReceive方法,你可以做这样的事情...

then in your onReceive method you can do something like this...

if (listener != null) {
    listener.onSmsReceived(sender, msg);
}

在创建并通过实施OnSmsReceivedListener接口注册的BroadcastReceiver开始的活动,然后执行以下

in the activity that creates and registers the BroadcastReceiver start by implementing the OnSmsReceivedListener interface, then do the following

public class SomeActivity extends Activity implements OnSmsReceivedListener.....

private MissedCallActivity receiver;

在onCreate方法添加

in the onCreate method add

receiver = new MissedCallActivity();
receiver.setOnSmsListener(this);

然后覆盖所使用的接口的方法。

then override the method used by the interface.

@Override
public void onSmsReceived(String sender, String message) {
    //Insert your text to speech code here
}

这是应该做的伎俩。 另外,不要忘记注册您的接收器,在onResume()方法,并注销其在的onPause()方法。另外,怎么看,从以蓝色高亮显示?这意味着它是Java语法的一部分,并且不能被用作变量。

That should do the trick. Also, don't forget to register your receiver in the onResume() method and unregister it in the onPause() method. Also, see how "from" is highlighted in blue? That means it's part of the Java syntax and cannot be used as a variable.

这篇关于从传递的BroadcastReceiver类数据到Android的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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