如何从传入的短信中接收信息?

时间:2014-07-28 07:50:39

标签: android messaging

我需要来自传入短信,发件人和邮件正文的2个变量。我如何访问它们?

1 个答案:

答案 0 :(得分:1)

以下是如何在android

中读取传入短信的分步说明

您需要使用广播接收器

  1. 在AndroidManifest中声明接收器

    <receiver android:name=".IncomingSms">   
     <intent-filter>
         <action android:name="android.provider.Telephony.SMS_RECEIVED" />
     </intent-filter>
    

  2. 在AndroidManifest中提供阅读短信权限

    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    
  3. AndroidManifest.xml文件的完整代码:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.androidexample.broadcastreceiver"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.androidexample.broadcastreceiver.BroadcastNewSms"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <receiver android:name="com.androidexample.broadcastreceiver.IncomingSms">   
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
    
        </application>
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    
    </manifest>
    

    IncomingSms.java文件详细信息:     我在这个文件中制作了广播事件接收器      1.使用扩展的BroadcastReceiver类创建类IncomingSms

    public class IncomingSms extends BroadcastReceiver
    
    
      2. Get the object of SmsManager to find out received sms details
    

    //获取SmsManager的对象

    final SmsManager sms = SmsManager.getDefault();
    
    
      3.  Create method receiver()
    
    
    public void onReceive(Context context, Intent intent)
    
    
        4. Get / Read current Incomming SMS data
    

    //从意图中检索扩展数据的地图。

    final Bundle bundle = intent.getExtras();
    
    try {
    
        if (bundle != null) {
    
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
    
            for (int i = 0; i < pdusObj.length; i++) {
    
                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();
    
                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();
    
                Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
    
    
               // Show alert
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                toast.show();
    
            } // end for loop
          } // bundle is null
    
    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);
    
    }
    

    =============================================== ==================================================      IncomingSms.java文件的完整代码:

    public class IncomingSms extends BroadcastReceiver {
    
        // Get the object of SmsManager
        final SmsManager sms = SmsManager.getDefault();
    
        public void onReceive(Context context, Intent intent) {
    
            // Retrieves a map of extended data from the intent.
            final Bundle bundle = intent.getExtras();
    
            try {
    
                if (bundle != null) {
    
                    final Object[] pdusObj = (Object[]) bundle.get("pdus");
    
                    for (int i = 0; i < pdusObj.length; i++) {
                         //here you will get currentMsg body phoneNmber and senderNumber
    
                        SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                        String phoneNumber = currentMessage.getDisplayOriginatingAddress();
    
                        String senderNum = phoneNumber;
                        String message = currentMessage.getDisplayMessageBody();
    
                        Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
    
    
                       // Show Alert
                        int duration = Toast.LENGTH_LONG;
                        Toast toast = Toast.makeText(context, 
                                     "senderNum: "+ senderNum + ", message: " + message, duration);
                        toast.show();
    
                    } // end for loop
                  } // bundle is null
    
            } catch (Exception e) {
                Log.e("SmsReceiver", "Exception smsReceiver" +e);
    
            }
        }    
    }