我想知道是否有人知道在Android平台上以编程方式从手机的SIM卡中获取短信。我想编写一个程序,允许你将单个消息或整个线程保存到SD卡,但在环顾四周之后,我发现google决定从当前的android sdk中取出那个api。我在一些地方看到有隐藏的api,但没有人知道它们是什么或如何使用它们。
答案 0 :(得分:3)
这是一个允许您阅读邮件的代码段。
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();
}
}
}
}
}
}
答案 1 :(得分:0)
另一种简单的方法是
class SmsReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val messages = Telephony.Sms.Intents.getMessagesFromIntent(intent)
}
}