我想知道是否有人知道在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();
}
}
}
}
}
}