android - 我将如何跟踪 Android 中的传入短信?

标签 android

我正在开发一个应用程序并希望跟踪传入的 SMS 消息。我需要可以使用的示例代码或例程。

最佳答案

如果您在这种情况下为传入的 SMS 实现 BroadCast 接收器,则以下代码将跟踪您的传入 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();
        }                         
    }
}

关于android - 我将如何跟踪 Android 中的传入短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5692659/

相关文章:

android - 为什么我无法为特定角创建圆形边框?

Java 构建路径错误 - 无法读取(项目)中的(库)或者不是 Eclipse 中的有效 zip 文件

java - firebase 访问 token 是否自动刷新?

android - OkHttp SSLProtocolException : SSL handshake aborted on android 4

android - 如何在android中连续录制后台音频/语音?

android - 获取 ListView 行 onContextItemSelected

android - 更改 Espresso 的注入(inject)模块

android - 获取SD卡路径

android - 从首选项标题开始 Activity

android - 我应该把与 ImageButton 相关的 PNG 文件放在哪里?