我想知道是否有人知道在Android平台上以编程方式从手机的SIM卡上获取短信的信息。我想编写一个程序,使您可以将单个消息或整个线程保存到sd卡,但是四处寻找后,我发现Google决定从当前的android sdk中删除该api。我在一些地方看到有隐藏的api,但是没人知道它们是什么或如何使用它们。
这里是一个代码段,可让您阅读消息。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.telephony.gsm.SmsManager;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Bundle myBundle = intent.getExtras();
SmsMessage [] messages = null;
String from= "";
String body="";
boolean inSIM=false;
if (myBundle != null)
{
Object [] pdus = (Object[]) myBundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
from = ""+messages[i].getOriginatingAddress();
body = ""+messages[i].getMessageBody();
inSIM = (messages[i].getIndexOnSim() != -1);
if (inSIM)
{
int status = messages[i].getStatusOnSim();
if (status == SmsManager.STATUS_ON_SIM_UNREAD)
{
Toast.makeText(context, "Message Unread on SIM: "+from+": "+body, Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
另一种简单的方法是
class SmsReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val messages = Telephony.Sms.Intents.getMessagesFromIntent(intent)
}
}