1#
安卓开发qq群273843464欢迎您的加入~~

这里实现一个接收短信并通过toast显示的例子(例子可去群共享下载)

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        Bundle bundle = intent.getExtras();
        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[messages.length];
        for (int n = 0; n < messages.length; n++)
        {
            smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }
        
        Toast toast = Toast.makeText(context, "aaa: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);

        //toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 200);

        toast.show();
    }
}

然后在manifest中注册 
 
          
              
          
    [
 最后设置权限