我寫了一個發送和接收短信成功的android。如何從特定電話號碼接收短信?
我想我的程序能夠從一個特殊號碼(「+ 9856874236」)接收短信。但是,如果短信來自任何其他號碼,它應該轉到電話的消息收件箱,而不是我的應用程序。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class SmsReceiver extends BroadcastReceiver {
public String str = "";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage[] msgs = null;
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
String msg_from="";
for (int n = 0; n < messages.length; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
msg_from += msgs[n].getOriginatingAddress();
}
String receivedMessage = smsMessage[0].getMessageBody().toString().toUpperCase();
if(msg_from .equals("+989124236870"))
{
for (int n = 0; n < messages.length; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) pdus[n]);
str += "SMS from " + msgs[n].getOriginatingAddress();
str += " :";
//str += "sms az shomare makhsus";
str += msgs[n].getMessageBody().toString();
str += "\n";
abortBroadcast();
}
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", str);
context.startActivity(act);
}
}
}
}
看來你有4個類似的問題打開。 1. http://stackoverflow.com/questions/13108337/how-to-receive-sms-only-from-one-specific-number-other-smss-go-to-phones-inbox 2. HTTP:/ /stackoverflow.com/questions/13097704/how-to-received-sms-from-special-number 3. http://stackoverflow.com/questions/13107161/filtering-received-sms-in-android 4.本一。 不酷。請不要這樣做。 –
因爲我沒有得到我的問題的答案!!!!!!!!我沒有任何辦法!你能解決我的問題嗎? – Pariya