ユーザー名とパスワードを必要とするAndroidのアプリケーションがあり、サーバーはユーザー名とパスワードをSMS経由でモバイルに送信します。
アプリケーションがSMSを読み取り、設定で手動で構成するのではなく、設定でユーザー名とパスワードを使用して自動的に構成できる方法を探していました。
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<receiver android:name=".Receiver" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
Receiver.java
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
public class Receiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
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]);
checkSMS(context, msgs[i]);
}
}
}
void checkSMS(Context context, SmsMessage sms)
{
String msg = sms.getMessageBody().toString();
}
}
優先度の高いSMSブロードキャストリスナーを使用し、rcvで、サーバーがSMSの送信に使用している番号からのものかどうかを確認します。はいの場合は、SMSテキストから情報を抽出し、それに応じてアプリの構成を更新します。また、abortBroadキャストを使用することもできます。 SMSをネイティブSMSアプリに表示しないようにする場合の方法