在android中如何获取短信发件人的电话号码?
我制作发送短信的申请但收取费用,所以我可以免费发送短信吗?请告诉我
答案 0 :(得分:8)
只有在您实施任何免费短信网关时,才能发送不带花钱的短信。根据您所在的国家/地区,您可以找到任何免费的短信网关,并尝试查找他们提供的任何网络服务或API。使用它编写代码,您将能够免费发送短信。使 确保这需要在手机上连接互联网。
如果您在此情况下实施BroadCast Receiver for Incoming SMS,则以下代码将跟踪您的即时消息,并将为您提供消息和发件人编号。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = 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();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:1)
rahul代码中的.getOriginatingAddress()方法给发送者电话号码。所以在任何你想要的地方使用它。简单!!
答案 2 :(得分:0)
这是一个很棒的reference。它在下面的代码中有以下教程link。
对我来说很有用!
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String tag = TAG + ".onReceive";
Bundle bundle = intent.getExtras();
if (bundle == null) {
Log.w(tag, "BroadcastReceiver failed, no intent data to process.");
return;
}
if (intent.getAction().equals(SMS_RECEIVED)) {
Log.d(tag, "SMS_RECEIVED");
String smsOriginatingAddress, smsDisplayMessage;
/**
* You have to CHOOSE which code snippet to use NEW (KitKat+), or legacy
* Please comment out the for{} you don't want to use.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// API level 19 (KitKat 4.4) getMessagesFromIntent
for (SmsMessage message : Telephony.Sms.Intents.
getMessagesFromIntent(intent)) {
Log.d(tag, "KitKat or newer");
if (message == null) {
Log.e(tag, "SMS message is null -- ABORT");
break;
}
smsOriginatingAddress = message.getDisplayOriginatingAddress();
//see getMessageBody();
smsDisplayMessage = message.getDisplayMessageBody();
processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
}
} else {
// Processing SMS messages the OLD way, before KitKat,
// this WILL work on KitKat or newer Android
// PDU is a “protocol data unit”, which is the industry
// format for an SMS message
Object[] data = (Object[]) bundle.get("pdus");
for (Object pdu : data) {
Log.d(tag, "legacy SMS implementation (before KitKat)");
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
if (message == null) {
Log.e(tag, "SMS message is null -- ABORT");
break;
}
smsOriginatingAddress = message.getDisplayOriginatingAddress();
// see getMessageBody();
smsDisplayMessage = message.getDisplayMessageBody();
processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
}
}
} // onReceive method