我正在开发一个Android应用程序,它具有虽然如果我收到一条新的短信需要使用文字转语音读取该消息的内容的应用程序正在运行的功能。 我知道如何使用文字转语音读取文本。 我已经在我的应用程序写入两班。 一个是从MainActivity活动延伸,另一种是从SmsReceiver的BroadcastReceiver延伸。 当在MainActivity运行时收到短信我想用SmsReceiver获得SMS消息的内容并把它传递到MainActivity,然后用文字转语音读取它里面MainActivity。 我怎样才能短信从SmsReceiver内容传递给我的MainActivity。 我SmsReceiver类的副本波纹管粘贴。
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();
}
}
}