在Android中我将如何跟踪接收短信? [英] How would i track Incoming SMS in Android?

查看:108
本文介绍了在Android中我将如何跟踪接收短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,并要跟踪的收到的短信。我需要一个样品code或程序,我可以使用。

I am developing an Application and want to track the incoming SMS messages. I need a sample code or routine which I can use.

推荐答案

如果您实现广播接收器接收到的SMS在下面这种情况下是code将跟踪imcoming短信给你的留言和发件人编号。

If you implement a BroadCast Receiver for Incoming SMS in that case following is the code which will track your imcoming SMS and will give you the Message and Sender Number.

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中我将如何跟踪接收短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆