私は Android アプリケーションを開発していますが、アプリケーションの実行中に新しい SMS メッセージを受信した場合、そのメッセージの内容を TextToSpeech を使用して読み取る必要があるという機能があります。TextToSpeech を使用してテキストを読む方法を知っています。アプリケーションに 2 つのクラスを作成しました。1 つは Activity から拡張された MainActivity で、もう 1 つは BroadcastReceiver から拡張された SmsReceiver です。MainActivity の実行中に Sms を受信すると、SmsReceiver を使用して SMS メッセージの内容を取得し、それを MainActivity に渡し、TextToSpeech を使用して MainActivity 内でそれを読み取ります。SMS の内容を 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();
}
}
}